Loading LOADSUB ALL FROM "FFIT.HTS"
or LOADSUB FROM "MATHLIB.HTS"
Usage REAL X(*),Y(*),Tol,P(*),Yt(*)
INTEGER M,N,Status
Status=FNFfit(M,N,F$,X(*),Y(*),Tol,P(*),Yt(*))
Description
FNFfit attempts to find the values of the parameters to the subroutine named in F$ that cause the function computed by the subroutine to best fit the data contained in X and Y. X contains the abscissas of the data points and Y contains the ordinates. M is the number of points in X and Y and N is the number of parameters in P to adjust. Before calling FNFFit, set P to the initial estimates for the parameters and Tol to the tolerance to use in determining when the parameters fit the data. After Ffit runs, Yt contains the values of the function being fit at each of the points in X using the final set of parameters. P contains the set of parameters.
The return value of FNFfit indicates why FNFfit finished searching for a solution. These reasons are summarized below:
Return Value Reason
1 The average square (the L-2 norm) of the differ ences between the values in Yt and those in Y is less than Tol.
2 The average absolute value (the L-1 norm) of the differences between the values in Yt and those in Y is less than Tol.
3 Both the averages mentioned above are less than Tol.
4 The algorithm in FNFfit found values in Yt that caused the solution to stop growing closer to the values in Y.
5 The number of iterations in the algorithm used by FNFfit exceeded 200(N + 1) before the solution was found.
6 or 7 The solution in Yt converged to a value that had both norms described under values 1 and 2, above, greater than Tol. Probably Tol is too small for the data and function used.
Usually, if FNFfit returns a value of 1, 2, or 3, the solution returned in P is considered to be correct and if it returns a value of 4, 5, 6, or 7, the solution is considered to be incorrect.
F$ should contain the name of an HTBasic subroutine. The subroutine should take three REAL parameters. The second parameter is an array and the others are scalars. The subroutine should evaluate the function to be fit using the parameters described in the array at the argument in the third parameter and return its value in the first parameter. For example, if F$ = "Test", then the subroutine Test should begin with the definition line
SUB Test(REAL Y,P(*),X)
where X, P, and Y may be replaced by the names of any REAL variables. The subroutine Test should evaluate the desired function of parameters P at the value X and return the value in Y. When the subroutine is called, P will be the array P mentioned in the description of the FNFfit function, above.
The recommended minimum value for Tol is about 1.5 × 10-8.
FNFfit uses the Levenberg-Marquardt method as modified by Moré to fit the data to the function. FNFfit requires n integers and (m + 5)n + m real values of temporary storage. FNFfit causes an HTBasic error if it cannot allocate this much storage.
Errors
FNFfit causes BASIC Errors if the dimension of X, Y, or Yt is less than m, if the dimension of P is less than n, if Tol < 0, or if it cannot allocate enough memory to run. The subroutine named in F$ may also cause BASIC Errors when called.
Example
An electronic filter circuit is built with an inductor (represented by the symbol L in the drawing), a capacitor (represented by C), and a 15Ω resistor (represented by Rt) in series. The series resistance in the inductor (represented by RL) is measured to be 3Ω . A sinusoidal voltage, Vin with amplitude 10 V (peak-to-peak) is applied to the circuit and the peak-to-peak voltage across Rt, Vout is measured at several frequencies, yielding the data shown in the table below.
Frequency, kHz Vout, V
10 1.48591
20 2.67114
30 4.46957
40 6.79728
50 8.06452
60 5.86735
70 4.49784
80 3.55642
90 2.99302
100 2.60036
110 2.29451
120 2.05200
130 1.87589
The values of L and C are related to the ratio Vout/Vin by the expression
where R is the sum of RL and Rt.
The following program uses FNFfit to estimate the values of L and C from the data in the table. It uses the FNNorm function (described under the Norm topic in this manual) to calculate the average error in the fit. The data in lines 150 - 170 is the ratio Vout/Vin. The initial values for L and C (represented in the program by P(1) and P(2)) are the values marked on the components.
10 LOADSUB ALL FROM "FFIT.HTS"
20 LOADSUB ALL FROM "NORM.HTS"
30 REAL P(1:2),Fvec(1:13)
40 REAL Freq(1:13),Vratio(1:13)
50 INTEGER I,Info
60 FOR I=1 TO 13
70 READ Freq(I)
80 NEXT I
90 FOR I=1 TO 13
100 READ Vratio(I)
110 NEXT I
120 DATA 10000.0,20000.0,30000.0,40000.0,50000.0
130 DATA 60000.0,70000.0,80000.0,90000.0,100000.0
140 DATA 110000.0,120000.0,130000.0
150 DATA 0.148591,0.267114,0.446957,0.679728,0.806452
160 DATA 0.586735,0.449784,0.355642,0.299302,0.260036
170 DATA 0.229451,0.205200,0.187589
180 P(1)=1.0E-4 ! initial value for L
190 P(2)=1.0E-7 ! initial value for C
200 Info=FNFfit(13,2,"Rlc",Freq(*),Vratio(*),1.5E-8, P(*),Fvec(*))
210 PRINT "Final average error "; FNNorm(Fvec(*))/SQR(13.0)
220 PRINT "Exit parameter ";Info
230 PRINT "L =";PROUND(P(1)*1.0E+6,0);CHR$(230);"Hy"
240 PRINT "C =";PROUND(P(2)*1.0E+6,-3);CHR$(230);"Fd"
250 END
260 SUB Rlc(REAL Y,P(*),X)
270 REAL Omega
280 INTEGER I
290 Omega=6.28318530717959*X
300 Y=(15.0/18.0)/ABS(CMPLX(1.0,Omega*P(1)/18.0 1.0/(Omega*18.0*P(2))))
310 SUBEND
When run, the program produces the output
Final average error .0226436510548
Exit parameter 1
L = 112 µHy
C = .105 µFd.
The value of Vout is plotted below over a range of frequencies for the calculated values L and C. The symbols on the plot are the measured values.
Vout
Frequency, kHz
|