The standard approach to fitting a set of data points to a line y=ax+b is to use the method of least squares. The least squares method provides an unambiguous fitting procedure which minimizes the deviation of the data points from the fitted line in the following sense. Let Yᵢ represent an experimental value and let yᵢ be a value from the equation y=axᵢ+b, where xᵢ is a particular value of the variable which is assumed to be free of error. It is desired to obtain the “best” values of a and b such that for eᵢ = Yᵢ-yᵢ=Yᵢ-axᵢ-b, the error E= Σᵢᴺ eᵢ²= Σᵢᴺ(Yᵢ-axᵢ-b)² is a minimum, where N is the number of data points. Since xᵢ and Yᵢ are unaffected by the choices of a and b, the minimizing relations are dE/da=0 and dE/db=0. These result in the two equations:
a Σxᵢ²+b Σxᵢ= ΣxᵢYᵢ
a Σxᵢ + bN = ΣYᵢ
Solving for a and b, these values can be expressed in terms of the input data only:

This is the least-squares method. The approach can be applied to fit data to the curve y=beᵃˣ in the following manner: take the log of both sides to get ln y=ln b+ax, and define y’=ln y and b’=ln b. Now we have y’=ax+b’, so the method outlined above applies.
THE PROGRAM
To execute, this program, enter the “RUN” mode. The first prompt is “INPUT PAIRS OF DATA” and “ENTER POINT 1”. The point is entered as follows: x₁, enter, y₁, enter. The new prompt is “ENTER POINT 2” and point (x₂,y₂) is entered, and soon, the answer then appears with a and b expressed numerically.
EXAMPLE
Fit a straight line to the given data using the least-squares criterion.
x | 1.1 | 2.9 | 4.3 | 6.2 f(x) | 50 | 43 | 28 | 25
After entering the “RUN” mode, we immediately get the prompt “INPUT N-NO. OF DATA”. Since there are 4 points, enter the number 4. In response to the prompt “ENTER POINT 1”, the values 1.1 and 50 are entered. We are then prompted; “ENTER POINT 2”, and the values 2.9 and 43 are input, and so on. The answer is printed out, i.e.,
“THELINE WHICH FITS THE DATA IS:
Y=5.3011618*x + 55.716711 “
10 REM LSTSQR
20 REM LEAST SQUARE FIT TO
30 REM A LINE FOR N DATA
33 DIM X(100)
36 DIM F(100)
40 PRINT "INPUT N-NO. OF DATA"
50 INPUT N
55 CLS
60 PRINT "INPUT PAIRS OF DATA"
70 FOR K=1 TO N
75 INPUT X(K)
80 INPUT F(K)
90 NEXT K
100 LET SUM1=0
110 LET SUM2=0
120 LET SUM3=0
130 LET SUM4=0
140 FOR L=1 TO N
150 LET SUM1=SUM1+X(L)
160 LET SUM2=SUM2+X(L)*X(L)
170 LET SUM3=SUM3+F(L)
180 LET SUM4=SUM4+X(L)*F(L)
190 NEXT L
200 LET D=1/(N*SUM2-SUM1*SUM1)
210 LET A0=(SUM2*SUM3-SUM1*SUM4)*D
220 LET A1=(-SUM1*SUM3+N*SUM4)*D
230 PRINT "THE LINE WHICH FITS THE DATA IS:"
240 PRINT ""
250 PRINT "Y= ";A1;"*X + ";A0
260 STOP
10 REM LSTSQX
20 REM LEAST SQUARE FIT TO
30 REM B*EXP(A*X) FOR N DATA
33 DIM X(100)
36 DIM F(100)
40 PRINT "INPUT N-NO. OF DATA"
50 INPUT N
55 CLS
60 PRINT "INPUT PAIRS OF DATA"
70 FOR K=1 TO N
75 INPUT X(K)
80 INPUT F(K)
82 CLS
85 LET F(K)=LN F(K)
90 NEXT K
100 LET SUM1=0
110 LET SUM2=0
120 LET SUM3=0
130 LET SUM4=0
140 FOR L=1 TO N
150 LET SUM1=SUM1+X(L)
160 LET SUM2=SUM2+X(L)*X(L)
170 LET SUM3=SUM3+F(L)
180 LET SUM4=SUM4+X(L)*F(L)
190 NEXT L
200 LET D=1/(N*SUM2-SUM1*SUM1)
210 LET A0=(SUM2*SUM3-SUM1*SUM4)*D
220 LET A1=(-SUM1*SUM3+N*SUM4)*D
230 PRINT "THE EXP WHICH FITS THE DATA IS:"
240 PRINT ""
250 PRINT "Y= ";EXP (A0);"*EXP ";A1;"*X"
260 STOP
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.