MathGroup Archive 2000

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

Search the Archive

Re: Question: Fitting numerical solutions of ord. diff. equ.

  • To: mathgroup at
  • Subject: [mg23381] Re: Question: Fitting numerical solutions of ord. diff. equ.
  • From: "Atul Sharma" <atulksharma at>
  • Date: Fri, 5 May 2000 02:07:14 -0400 (EDT)
  • References: <8er9r7$>
  • Sender: owner-wri-mathgroup at

I've found the timing to be quite variable depending on the quality of the
initial estimates, how many equations, how well behaved they are etc. The
largest system I've used this way contained 13 coupled equations with 4
unknowns, but "actual mileage may vary".

I include an excerpt from the relevant FAQ on the WRI web site. This example
uses FindMinimum, but I agree with you that there is an advantage to using
NonlinearFit to simplify the goodness of fit evaluation. Once you've defined
the error function, it may be used by NonlinearFit in an analogous fashion.
There is also a FAQ on using penalty functions to constrain the parameter
values, which you may find helpful.

A. Sharma


How can I fit data to a model that is defined using NDSolve?


You can in principle fit any data to any model by constructing an expression
for the difference between the data and the model, and using FindMinimum to
minimize that expression with respect to the parameters of the model. The
example below shows one way to do this when the model is computed by
NDSolve, and the difference to be minimized is the sum of the squared
differences between the data and the model at each data point (least-squares

If the model is given by a function f, the sum of the squared differences
between the data and the model can be constructed using

In[1]:= data = {{0.5, 6.2}, {1.1, 8.1}, {1.5, 8.8},
                       {2.1, 8.3}, {2.3, 6.8}, {3.1, 2.9}} ;

In[2]:= Plus @@ Apply[(f[#1] - #2)^2 &, data, {1}]

                       2                  2                  2
Out[2]= (-6.2 + f[0.5])  + (-8.1 + f[1.1])  + (-8.8 + f[1.5])  +

                    2                  2                  2
>    (-8.3 + f[2.1])  + (-6.8 + f[2.3])  + (-2.9 + f[3.1])

In this example the function f is the solution of a differential equation

    {f'[x] == If[x <  p, 10 - f[x], -f[x]], f[0] == q}

where p and q are parameters of the solution.
A function sse[p, q] to compute the sum of the squared differences between
the data and the model in this example can be written as

In[3]:= sse[p_?NumberQ, q_?NumberQ] := Block[{sol, f},
            sol = NDSolve[{f'[x] == If[x <  p, 10 - f[x], -f[x]], f[0] ==
                     f, {x, 0, 5}][[1]];
            Plus @@ Apply[(f[#1]  - #2)^2  &, data, {1}] /. sol

This expression can be minimized using FindMinimum to obtain the best fit
values for p and q.

In[4]:= FindMinimum[sse[p, q], {p, 2, 3}, {q, 4, 5}]

Out[4]= {0.0743759, {p -> 1.99611, q -> 3.93593}}

These calculations can be computationally and mathematically quite
demanding. In addition to all of the usual difficulties with non-linear
fitting, solving a differential equation is a non-trivial task, and when the
model is defined by a differential equation, that differential equation must
be solved (with new parameters) at each step in the minimization process.

"Marc Datz" <datz at> wrote in message
news:8er9r7$hj2 at
> Hi,
> for my diploma thesis I have to optimize the parameters of a only
> numerical solvable System of ordinary differential equations, so that it
> fits optimal to my measured curve.
> I have 6 implicit ordinary differential eqautions with derivatives to
> time which I have to solve numerically. The parameters of the equations
> have to be optimized in a way, that the chi^2 value of the calculated
> NF(t) and  my measured data is minimized (see atached File).
> [get the file by contacting the author ---moderator]
> I work on a solution in Pascal, but I heard it would be easy to solve
> this problem with Mathematica. Unfortunately I dont know anybody who
> could help me. The advantage would be that I could get the standard
> errors of the parameters (alpha, beta...) and see very fast which are
> significant.
> I never worked with Mathematica before and I want to know, how I have to
> proceed and how much time you think it will take.
> Many thanks
>                Marc
Atul Sharma MD, FRCP(C)
Pediatric Nephrologist,
McGill University/Montreal Children's Hospital

email: atulksharma at

  • Prev by Date: Re: Symbols & Legend Fonts in MultipleListPlot...
  • Next by Date: results of pair creation contest
  • Previous by thread: Question: Fitting numerical solutions of ord. diff. equ.
  • Next by thread: RE: Problem with Plot and Ticks-function