MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NonLinear "Fitting" to an Interpolating Function



Hi Tom,

I don't know too much about this subject, but this is what I discovered.

First, when the NonlinearFit program starts it does something with the
model with nonnumeric values of the parameters. The easiest way to
correct this is to add a condition to your model, such as

 ndsol[x_, a_?NumericQ, b_?NumericQ] :=
   Module[{f, y}, fun[y_] =
      f[y] /.
       Flatten[NDSolve[{Derivative[1][f][
            y] == -b*f[y], f[0] == a}, f[y], {y, 0, 5}]];
        fun[x]
     ]

Next, the default method for doing the minimization associated with the
fit is the LevenbergMarquardt method, and in doing this method one
needs to take derivatives of the model with respect to the parameters.
Since you are passing an interpolation function to the program, it is
unable to take symbolic derivatives and so it fails. There are two ways
around this. In both cases you need to switch to the FindMinimum
method. The easiest way is to add two starting values to your
parameters. Thus,

NonlinearFit[data, ndsol[x,a,b], x, {{a,{2,2.1}},{b,{2,2.1}}}]

should work. Alternatively, you could give a list of the gradient
functions by feeding it the option Gradient, thus

NonlinearFit[data, ndsol[x,a,b], x,
{a,b},Gradient->{ndsol_a[x,a,b],ndsol_b[x,a,b]}]

or something similar. I haven't tried this second approach, and you
would need to write the gradient functions as you did the ndsol
function, but it should work. The first method was quite slow, so you
may want to look at using the Gradient option.

If you want to discuss the above further, please feel free to email me.
Since we are both at the University of Washington, it may be possible
to communicate more directly if you like.

Good luck,

Carl Woll
Dept of Physics
U of Washington

 On Thu, 12 Mar 1998, Tom Marchioro wrote:

> 
> Is it possible to use NonlinearFit (or some other combination of
> functions in Mathematica) to make a fit from some data to a function
> which is defined numerically once its parameters are given?  That is, I
> have a differential equation which cannot be solved numerically, but is
> straightforward to integrate with NDSolve once values are given for a
> couple of parameters, with the result that is returned being an
> InterpolatingFunction object.
> 
> Is it possible for this numerically defined function to be used as the
> "model" in NonlinearFit or
> FindMiinimum to get back a "best guess" as to what values of the
> parameters best fit the data?
> 
> To give a somehat specific example, here is a module which integrates a
> specific differential equation for given parameters (this is just a
> test case, the actual diff eq. will generally be much harder)
> 
> ndsol[x_, a_, b_] :=
>   Module[{f, y}, fun[y_] =
>      f[y] /.
>       Flatten[NDSolve[{Derivative[1][f][
>            y] == -b*f[y], f[0] == a}, f[y], {y, 0, 5}]];
>        fun[x]
>     ]
> 
> 
> We also have some data (which in general will be from experiment, not
> generated analytically)
> 
> data=Table[{x, 2 E^(-2.2 x)}, {x, 1, 4, .25}];
> 
> and I want to do something like
> 
> NonlinearFit[data, ndsol[x,a,b], x, {a,b}]
> 
> which does not work. I **can** put in the model a*E^(-b*X) and get the
> exact answer.  Alternatively, I can define the root mean square error
> 
> rmserror[a_,b_]:=Sqrt[Apply[Plus,
>    (whys-ndsol[exes,a,b])^2]]
> 
> and then use FindMinimum to minimize the error between the data and the
> numerical function
> 
> {exes, whys}=Transpose[Table[{x, 1.3 E^(-3.2 x)}, {x, 1, 4, .25}]];
> 
> FindMinimum[rmserror[a,b],
>  {a, {1, 3}}, {b, {.1, 3}}]
> 
> {1.429919514422274*^-7, {a -> 1.300033428167226, b ->
> 3.200038604006857}}
> 
> which is "okay", but really I want the information you get from doing a
> full regression on the data.
> 
> I've tried various combinations of "Evaluate" and "Hold" on the
> arguments to NonlinearFit and do not seem to be getting anywhere.
> 
> Suggestions?
> 
> Thanks in advance --- Tom
> 
> 
> --
> Dr. Thomas L. Marchioro II
> Department of Chemistry
> University of Washington
> 206-323-9599
> http://borg.chem.washington.edu/~tlm
> 
> 
> 
> 




  • Prev by Date: Re: Eigenvalues
  • Next by Date: Re: file size?
  • Prev by thread: NonLinear "Fitting" to an Interpolating Function
  • Next by thread: Plotting points in 3D