Академический Документы
Профессиональный Документы
Культура Документы
20020491
Introduction
Given a set of N data points (xi , yi ) with errors i and a function f (x; c) which
depend on the vector of coefficients c = (c1 , c2 , . . . , cn ), least-square fitting is a
problem of finding a choice of coefficients c0 which minimizes the distance between
the function and the datapoints. In this context the distance is defined as:
2
N
X
(1)
i=1
If f (x; c) depends linearly on the ci s the problem can be solved using linear
algabra (see Note 5), but if f (x; c) is nonlinear in some or all of the ci s then
other methods have to be used (see Note 8).
In the present paper a numerical method which can minimize 2 as a function
of c, even if nonlinear ci s are present, is described and an implementation of the
method is presented and tested.
Numerical method
In the most general case 2 has an arbitrary dependence on the ci s, and the problem of finding c0 is thus equivalent to finding a global minimum in n-dimensional
space.
In this paper the downhill simplex method is used to find a lokal minimum of
2 in n-dimensional space. This method is very robust and it requires relatively
few evaluations of the function. On the other hand the method has the drawback,
that an initial simplex has to be specified and that one only finds a local and
thus not necessarily a global minimum.
To mend the problem of finding the global minimum the initial simplex can
be chosen near the global minimum, or in other words one has to be able to guess
the approximate location of the global minimum of 2 in n-dimensional space.
If the user of the method is able to specify such a guess the Downhill simplex
X 2 2
1
dci dcj
(2)
2 (c0 + dc) = 2 (c0 ) +
2 i,j ci cj
c=c0
!1
2 2
dc2i = 2
(3)
c2i
c=c0
More generally the (i, j)th element in the covariance matrix of the fit can be
estimated as:
!1
2 2
dci dcj = 2
(4)
ci cj
c=c0
Implementation
Test of nonlinearfit()
(6)
(xi , yi , i ) data were generated using the subroutine generate_data() which for
a value of xi returns:
yi = f (x; (c1 , c2 , c3 )) + rnd i
where rnd is a random number between -0.5 and 0.5.
(7)
With (c1 , c2 , c3 ) = (1.3, 3.4, 0.3) 19 datapoints with x-values randomly distributed between 0.1 and 11.0 and with = 0.2 has been generated. A fit to the
points has then been performed using nonlinearfit(), and the result is shown
below:
Nonlinear fitting routine using a downhill simplex method.
==================================================================
Function: f(x)=c1*sin(c2*x+c3) (x,y) data is generated as
(x,f(x)+noise) with random x and with
(c1,c2,c3):
1.300000
3.400000
0.3000000
-----------------------------------------------------------------Minimum bounds on coefficients:
1.170000
3.060000
0.2700000
Maximum bounds on coefficients:
1.430000
3.740000
0.3300000
-----------------------------------------------------------------Number of itterations done:
33
Best chi**2 found:
1.376749
-----------------------------------------------------------------cfit
1 =
1.288280
+/- 6.4141616E-02
cfit
2 =
3.394368
+/- 8.7871952E-03
cfit
3 = 0.3091391
+/- 5.0209433E-02
-----------------------------------------------------------------Estimated covariance matrix:
4.1141468E-03 9.6444059E-03 2.8554073E-02
9.6444059E-03 7.7214798E-05 5.2758143E-04
2.8554073E-02 5.2758143E-04 2.5209871E-03
A similar fit to the same data has been performed with Gnuplots built in
nonlinear fitting routine. Here the results are:
After 4 iterations the fit converged.
final sum of squares of residuals : 1.22912
rel. change during last iteration : -1.57168e-14
degrees of freedom (ndf) : 16
rms of residuals
(stdfit) = sqrt(WSSR/ndf)
: 0.277165
variance of residuals (reduced chisquare) = WSSR/ndf : 0.0768202
Final set of parameters
=======================
a
b
c
+/- 0.01803
+/- 0.004638
+/- 0.02694
= 1.29204
= 3.39951
= 0.272031
(1.396%)
(0.1364%)
(9.904%)
Nonlinear fit
2
Data
Nonlinear fit
Gnuplot fit
1.5
0.5
-0.5
-1
-1.5
-2
0
6
x
10
Figur 1: Test of nonlinearfit(). The fit returned from nonlinearfit() to 19 datapoints are compared with the datapoints and with a similar fit by Gnuplot.
When comparing the two results it is seen that Gnuplot and nonlinearfit()
agrees on the coeefficients within the errors. Gnuplot returns a slightly better 2
and the errors on the coefficients are a little smaller, but within the same order
of magnitude as those estimated by nonlinearfit(). Figure 4 shows a plot of
the datapoints and the fits returned by both Gnuplot and nonlinearfit() and
it is seen that both fits agrees very well with the datapoints and with each other.
Conclusion
12