HTBasic Help
×
Menu
Index
Fit
Fit a polynomial curve to a series of data points.
 
Loading        LOADSUB ALL FROM "FIT.HTS"
or LOADSUB FROM "MATHLIB.HTS"
or LOADSUB Fit FROM "MATHLIB.HTS"
 
Usage                INTEGER M,N
REAL X(*),Y(*),C(*)
CALL Fit(M,X(*),Y(*),N,C(*))
 
Description                
Fit calculates the coefficients of the polynomial of degree n that gives the closest fit to the points whose coordinates are (xk,yk), where k is an index into the m values in the arrays X and Y. The "closest fit" is that which gives the smallest sum of squares of the differences between each point yk and the corresponding p(xk), where p is the polynomial generated from the coefficients returned in C. The first element in C contains the constant or zero-order coefficient, the second element the first-order coefficient, etc. The polynomial described by the coefficients of C can be evaluated by Poly, described in its own entry.
 
N must be between zero and 10, inclusive. M may be any positive integer. The dimensions of X and Y must be at least m. If either X or Y has more than m data points, the extra points are neither used nor modified by Fit. Similarly, C must contain at least n+1 data points; if C has more than n+1 data points, the extra points are not modified by Fit.
 
If n is zero, Fit returns the average value of the elements in Y, which is the zero-order polynomial that most closely approximates the points in X and Y. If N is 1, Fit returns the coefficients of the linear polynomial that most closely approximates the points in x and y, and so on for higher values of n.
 
Note that polynomials higher than degree 2 or 3 tend to have extreme values outside the region defined by the smallest and largest xk, although they give more accurate approximations of the values of yk inside this region. In addition, higher-order polynomials may oscillate between adjacent values of xk. If such oscillations occur, a smaller-degree polynomial would probably give a better approximation than a larger-degree one. Because of this, checking higher-order fitting functions with a graph is advisable.
 
Errors                
Fit causes an HTBasic error if its arguments are not of the types shown in the USAGE section, above, if n is not between 0 and 10, inclusive, if the size of X or Y is smaller than M, or if the size of C is smaller than N+1.
 
Example
The percentage of automobiles in the United States with at least one passenger suffering extreme injury or death in collisions was tabulated in 1970 and categorized by weight of the automobiles involved. The data was distributed as shown in the table below.
 
Weight of Automobile                Percent Injury or Death
1900 lb.                                9.6%
2800                                                6.4
3400                                                5.2
3700                                                4.0
4800                                                3.1
 
 
If w is the weight of the automobile and p is the percent of injury or death, the following BASIC program finds the coefficients that relate p to w assuming the relation has the form
 
p = c1w + c0
 
or
 
p = c1/w + c0.
 
 
10 LOADSUB ALL FROM "FIT.CSB"
20 REAL Weight(1:5),Pct(1:5),C(0:1)
30 READ Weight(*),Pct(*)
40 DATA 1900,2800,3400,3700,4800
50 DATA 9.6,6.4,5.2,4.0,3.1
60 CALL Fit(5,Weight(*),Pct(*),1,C(*)) ! linear fit
70 PRINT USING """1. Rate = "",2D.1D,""% -"",1D.6D,
""%/pound x Weight""";C(0),-C(1)
80 MAT Weight=(1)/Weight
90 CALL Fit(5,Weight(*),Pct(*),1,C(*)) ! inverse fit
100 PRINT USING """2. Rate = "",6D,
""% pound / Weight -"",2D.1D,""%""";C(1),-C(0)
110 END
 
The figure below shows the data from the table and curves drawn from the parameters c1 and c0 computed by the program.
 
Percent Injury or Death
 
 
Weight of Automobile, lb.
 
 
See Also
Ffit, Poly