MathGroup Archive 2004

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

Search the Archive

Re: Using NDSolve in NonlinearFit

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48810] Re: [mg48771] Using NDSolve in NonlinearFit
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Thu, 17 Jun 2004 04:07:22 -0400 (EDT)
  • References: <200406160854.EAA12153@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 16 Jun 2004, at 17:54, Dr. Ernst H.K. Stelzer wrote:

> Hi,
>
> I have been trying to use NDSolve inside various Fitting routines
> (NonlinearFit, FindFit, ...).  Although the NDSolve works fine both  
> inside
> and outside a Module wrapper once inside a "fit"-function the  
> variables are
> never resolved.  At least the Print inside the function returns names  
> rather
> than numbers??
>
> What is my mistake?
> Has anybody solved this problem differently?
>
> I do not wish to write a new Fit-function and search myslef if it can  
> be
> avoided.  The text below is copied as CellFormat and can be inserted  
> and
> interpreted in any Mathematica Notebook.
>
> Sincerely
>
>        Ernst
>
> --------------------------------------------------------------------
>   Dr. Ernst H.K. Stelzer                 Tel: +49 (6221) 387354
>   Scientific Groupleader                 Fax: +49 (6221) 387306
>   Cell Biology and Biophysics Programme
>   EMBL
>   European Molecular Biology Laboratory
>   Meyerhofstrasse 1                      Postfach 10 22 09
>   69117 Heidelberg                       69012 Heidelberg
>
>   e-mail: mailto:stelzer at embl-heidelberg.de
>   http://www.embl.de/ExternalInfo/stelzer/index.html
>   http://www.embl.de/ExternalInfo/stelzer/frames.html?publications


The problem you are experiencing is due to the fact that when you call

NonlinearFit[data, f[cc, x1, x2, x3, x5], cc,
   {{x1, 1.1}, {x2, 1.2}, {x3, 0.9}, {x5, 9}}]

NonlinearFit attempts to evaluate f[cc, x1, x2, x3, x5] with symbolic  
values, which of course can't be done. One can try to fix it using a  
defintion of the form f[cc_?NumericQ, x1_?NumericQ, x2_?NumericQ,  
x3_?NumericQ, x5_?NumericQ] :=  .... but in any case this looks  
unlikely to work well. However, your problem is very easy to Solve in a  
much simpler way, since actually your differential equation can be  
solved exacly using DSolve. I am including below my modification of  
your code (in InputForm and with all outputs except the last one  
deleted) which differs from your original only in that I use DSolve  
instead of NDSolve, = instead of := in the defintion of f, and    
FindFit instead of NonLinearFit. Of course the latter also would work.


Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/


------------------------------------------------------------------------ 
-----------------------


models = {Derivative[1][n0][c] == (-a00a)*(n0[c] - b0),
     Derivative[1][n0a][c] == a00a*(n0[c] - b0) -
       a01*n0a[c], Derivative[1][n1][c] ==
      a01*n0a[c] - a11*n1[c], n0[0] == n00,
     n0a[0] == n0a0, n1[0] == n10};


parms = {a00a :> 1, b0 :> 1, a01 :> 1, a11 :> 1,
     n00 :> 10, n0a0 :> 5, n10 :> 3};


sol1 = DSolve[Evaluate[models /. parms],
    {n0[c], n0a[c], n1[c]}, c]


Plot[Evaluate[{n0[c], n0a[c], n1[c]} /. sol1],
    {c, 0, 10}, AxesLabel -> {"[c]", "[#]"}];


data = Table[{c, First[Evaluate[n0[c] /. sol1] +
        Random[Real, {-1, 1}]]}, {c, 0, 10, 0.2}];


ListPlot[data]


vars = {a00a -> x1, b0 -> x2, a01 -> x3, n00 -> x5};


values = {a00a :> 1, b0 :> 1, a01 :> 1, a11 :> 1,
     n00 :> 10, n0a0 :> 5, n10 :> 3};


mdls = models /. vars /. values


f[cc_, x1_, x2_, x3_, x5_] = Module[{sol1},
    sol1 = DSolve[{Derivative[1][n0][c] ==
         (-x1)*(-x2 + n0[c]), Derivative[1][n0a][c] ==
         x1*(-x2 + n0[c]) - x3*n0a[c],
        Derivative[1][n1][c] == x3*n0a[c] - n1[c],
        n0[0] == x5, n0a[0] == 5, n1[0] == 3},
       {n0[c], n0a[c], n1[c]}, c];
     First[Evaluate[n0[c] /. sol1]] /. c -> cc]


N[f[1, 1, 1, 1, 10]]


FindFit[data, f[cc, x1, x2, x3, x5],
   {{x1, 1.1}, {x2, 1.2}, {x3, 0.9}, {x5, 9}}, {cc}]

{x1 -> 1.04338, x2 -> 1.07025, x3 -> 0.9, x5 -> 9.81829}


  • Prev by Date: Re: Complexes, Reals, FullSimplify
  • Next by Date: Overlay graphs
  • Previous by thread: Using NDSolve in NonlinearFit
  • Next by thread: Re: Using NDSolve in NonlinearFit