CURVEFIT (also, legacy CURVEFIT1D and CURVEFIT1DT)
CURVEFIT is a built-in procedure that provides the parameters and statistics resulting from a linear regression curve fit to data in any table or file. This procedure is available in EES version 12.303 and it replaces the previous curvefit routines (CURVEFIT1D (for arrays) and CURVEFIT1DT (for Lookup tables). CURVEFIT1D and CURVEFIT1DT have been retained for backward compatibility. CURVEFIT the same capability as the Curve Fit menu item but it may be more convenient in that the data do not first have to be plotted and the curve fit parameters are returned so that they can be used in following equations.
Format of the CURVEFIT Procedure
Call CURVEFIT(TableName$, FitType$, 'XCOL', 'YCOL', Row1, RowN, doUnits: a[0..m], RMS, Bias, R|2, a_stderr[0..m])
Inputs:
TableName$ is a string constant or string variable that provides the name of an existing table or Lookup file (stored on disk). For existing tables, TableName$ consists of two parts separated by a colon(:). The first part is the type of table, which can be "PARAMETRIC", 'LOOKUP', "ARRAYS' or 'INTEGRAL'. The second part is the name shown on the tab in the table window. For Lookup files, Tablename$ is the name of the file stored on disk including the filename extension, which is usually .lkt or .lkt64.
NOTE: The table must exist in order to be used in the CURVEFIT procedure. For this reason, the Call CURVEFIT procedure must be placed with $DOLAST .. $ENDDOLAST directives when used with PARAMETRIC and INTEGRAL tables, as shown in the examples.
FitType$ is a string constant or string variable that can be any of the following:
'LINEAR' {fit is Y[i]=a[0]+a[1]*X[i]}
'POLYNOMIAL1' or 'POLY1' {same as linear}
'POLYNOMIAL2' or 'POLY2' {fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2}
'POLYNOMIAL3' or 'POLY3' {fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3}
'POLYNOMIAL4' or 'POLY4' {fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3+a[4]*X[i]^4}
'POLYNOMIAL5' or 'POLY5' {fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3+a[4]*X[i]^4+a[5]*X[i]^5}
'POLYNOMIAL6' or 'POLY6' {fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3+a[4]*X[i]^4+a[5]*XY[i]^5+a[6]*X[i]^6}
'EXPONENTIAL' or 'EXP' {fit is Y[i]=a[0]*exp(a[1]*X[i])}
'POWER' {fit is Y[i]=a[0]*X[i]^a[1]}
'LOGARITHMIC' or 'LOG' {fit is Y[i]=a[0]+a[1]*ln(X[i])}
'XCOL' is a string constant or string variable provides the name of the column for the independent variable. For the ARRAYS table, the column name should be the parent name of the array followed by [] or [i], e.g., X[].
'YCOL' is a string constant or string variable provides the name of the column for the dependent variable. For the ARRAYS table, the column name should be the parent name of the array followed by [] or [i], e.g., Y[].
Row1 is the first row in the table that is used in the curve fit. It can be an integer or previously defined EES variable.
RowN is the last row in the table that is used in the curve fit. It can be an integer or previously defined EES variable.
doUnits is optional. If it is provided, it should be either false# (0) or true# (1). When the dependent and/or independent variables have units, the curve-fit parameters also have associated units. If doUnits is true#, the units of the parameters will be determined and assigned to the a[0..m] parameters. If doUnits is not provided, it is assumed to be false#
Outputs:
a[0..m] is the array of curve fit parameters returned by the CURVEFIT1D procedures. Note that m is equal to 2 for all cases except for the POLYNOMIAL fit types in which m is equal to one greater than the polynomial order. It is not necessary to use array range notation for the curve fit parameters. For example, a[1..2] can be replaced with a0, a1.
All following outputs are optional.
RMS is root mean square error defined as: sqrt((1/n)*sum[(Y-Y`)^2] where n is the number of data points, and Y` is the estimated value of Y.
BIAS is the bias error defined as: (1/n)*sum[(Y-Y`)].
R|2 is the correlation coefficient between Y and X
a_stderr[0..m] is the standard error of the curve fit parameters defined as the square root of the estimated variance of the parameter
Example 1: {Using Parametric table}
"Create a Parametric table with 10 row and columns for T and P. Then run this example using Solve Table (F3)."
$UnitSystem SI C kPa
$VarInfo T units='C'
$VarInfo P, P` units='kPa'
N=NParametricRows('Table 1')
P=P_sat(Water,T=T)
$DoLast
CALL curvefit('Parametric:Table 1','POLY3','T','P',1,N : a0, a1, a2, rms,bias)
$EndDoLast
{Solution:
a0=-2.55
a1=0.4352
a2=-0.01183
a3=0.0001783
bias=3.833E-18
rms=0.5432 [kPa/C^3]
}
Example 2: {Using Lookup table}
"Create a new Lookup table named 'Lookup 1' with 6 rows and two columns and enter the following data"

T$='Lookup 1'
N=NLookupRows(T$)
CALL CurveFit('Lookup:Lookup 1','Poly2','T','k',1,6,true#:a0,a1,a2,rms)
$VarInfo k_est[] units=W/m-K
$VarInfo T[] units=K
DUPLICATE i=1,N
T[i]=Lookup(T$,i,'T')
k_est[i]=a0+a1*T[i]+a2*T[i]^2
END
$if Professional {plot data}
$CopytoLookup /C T$, 'k_est\W/m-K' 1 k_est[1..N]
$NewPlot Name='k vs T' Table=T$ X=T, Y=k Legend='data'
$OverlayPlot name='k vs T' Table=T$ X=T, Y=k_est Legend='fitted data' LineStyle=0 Symbol=8
$endif
{Solution:
a0=430.1 [W/m-K]
a1=-0.09416 [(W/m-K)/K]
a2=0.00001563 [(W/m-K)/K^2]
rms=1.538}
Example 3: {Using Arrays table}
$tabStops 0.2 3.5
m=2 "order of the polynomial"
n=10 "n is the number of data points"
DUPLICATE i=1,n
X[i]=i/10
Y[i]=i+2.5*X[i]^2+0.002*X[i]^4+Random(0,1)*X[i]^2 {fake data for the example - note use of random}
Y`[i]=a[0]+Sum(a[j]*X[i]^j,j=1,m) {predicted value of Y from the polynomial fits}
END
F$='POLY'||String$(m) "m is other order of the polynomial from the table"
CALL curvefit('Arrays:Main',F$,'X[i]','Y[i]',1,n:a[0..m],rms,bias,R|2) "returns coefficients a[..] and rms"
$If Professional
$NewPlot Name='Plot 1' Table=Arr X=X[i] Y=Y[i] Rows=1..10 LineStyle=1 Symbol=0 legend='data from table'
$OverlayPlot Name='Plot 1' Table=Arr X=X[i] Y=Y`[i] LineStyle=0 Symbol=1 SymbolSize=6 Color=Red legend='fitted data'
$endif
{Solution: RMS=0.1507 and Bias=0, a[0], a[1], a[2] vary due to use of Random}
_____________________________________________________________
CURVEFIT replaces CURVEFIT1D and CURVEFIT1DT. However, their use is documented here for backward compatibility
Format of the CURVEFIT1D Procedure
Call CURVEFIT1D(FitType$, X[1..n], Y[1..n]: a[0..m], RMS, Bias, R|2, a_stderr[1..m])
FitType$ is as defined for CURVEFIT above
X[1..n] is an array of values for the independent variable and Y[1..n] is an array of values for the dependent variables. n can be a numerical constant or a previously defined EES variable. The number of data points in the X and Y arrays must be greater than the number of parameters that are being determined and less than 1,000.
Outputs are the same as for CurveFit. All output following a[0..n] are optional
a[0..m] is the array of curve fit parameters returned by the CURVEFIT1D procedures. Note that m is equal to 2 for all cases except for the POLYNOMIAL fit types in which m is equal to one greater than the polynomial order. It is not necessary to use array range notation for the curve fit parameters. For example, a[1..2] can be replaced with a0, a1.
Format of the CURVEFIT1DT Procedure
Call CURVEFIT1DT(FitType$, 'LookupTableName', 'XCOL', 'YCOL', Row1, RowN, doUnits: a[0..m], RMS, Bias, R|2, a_stderr[1..m])
FitType$ is the same as defined above for the CURVEFIT procedure.
LookupTableName is a string constant or string variable providing the name of an existing Lookup table or Lookup file (stored on disk).
XCOL is a string constant or string variable providing the name of the column in the Lookup table/file that will provide the values of the independent variables.
YCOL is a string constant or string variable providing the name of the column in the Lookup table/file that will provide the values of the dependent variables.
Row1 is the first row in the table that is used in the curve fit. It can be an integer or previously defined EES variable.
RowN is the last row in the table that is used in the curve fit. It can be an integer or previously defined EES variable.
doUnits is optional. If it is provided, it should be either false# (0) or true# (1). When the dependent and/or independent variables have units, the curve-fit parameters also have associated units. If doUnits is true#, the units of the parameters will be determined and assigned to the a[1..m] parameters. If doUnits is not provided, it is assumed to be false#
The outputs are the same as defined above for the CURVEFIT procedure.
See also: Curve fit menu item