Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10389

Re: please help using NDSolve and FindFit

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: please help using NDSolve and FindFit
Date 2012-06-08 07:34 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jqs9ur$dbv$1@smc.vnet.net> (permalink)
References <201206070918.FAA02436@smc.vnet.net>

Show all headers | View raw


texp={0.5,1.1,1.5,2.1,2.3,3.1};
fexp={6.2,8.1,8.8,8.3,6.8,2.9};
data=Transpose[{texp,fexp}];

model1[p_?NumericQ,q_?NumericQ]:=f/.
NDSolve[{f'[t]==If[t<p,10-f[t],-f[t]],
f[0]==q},f,{t,0,5}][[1]];

Note the use of NumericQ rather than NumberQ. NumberQ does not
consider numeric constants (symbols) such as Pi or E to be numbers.

#/@{2,E,Pi}&/@{NumberQ,NumericQ}

{{True,False,False},{True,True,True}}

Your FindFit arguments should be written as follows

fit1=FindFit[data,model1[p,q][t],
{{p,2},{q,4}},t]

FindFit::sszero: The step size in the search has become less than the
tolerance prescribed by the PrecisionGoal option, but the gradient is
larger than the tolerance specified by the AccuracyGoal option. There
is a possibility that the method has stalled at a point that is not a
local minimum. >>

{p->1.99802,q->3.99734}

Plot[Evaluate[model1[p,q][t]/.fit1],{t,0,5},
Epilog->{Red,AbsolutePointSize[3],Point[data]}]

This can also be done with DSolve

model2[p_,q_]=f/.
DSolve[{f'[t]==If[t<p,10-f[t],-f[t]],
f[0]==q},f,t][[1]];

fit2=FindFit[data,model2[p,q][t],{{p,2},{q,4}},t]

{p->1.99613,q->3.93527}

Plot[Evaluate[model2[p,q][t]/.fit2],{t,0,5},
Epilog->{Red,AbsolutePointSize[3],Point[data]}]


Bob Hanlon


On Thu, Jun 7, 2012 at 5:18 AM, Lee <lee787@comcast.net> wrote:
> As reading tutorials by Mathew Smith, I am trying to fit six (t,f) data points in range t=[0,5] using a conditional differential equation.
>
> texp = {0.5, 1.1, 1.5, 2.1, 2.3, 3.1};
> fexp = {6.2, 8.1, 8.8, 8.3, 6.8, 2.9};
> data = Transpose[{texp, fexp}];
> model[p_?NumberQ, q_?NumberQ] := First[f /. NDSolve[{f'[t] == If[t < p, 10 - f[t], -f[t]], f[0] == q}, f, {t, 0, 5}]];
> fit = FindFit[data, model, {{p, 2}, {q, 4}}, f];
>
> Mathematica gave the following error message:
>
> FindFit::nrlnum: The function value {-6.2+model, -8.1+model, -8.8+model, -8.3+model, -6.8+model, -2.9+model} is not a list of real numbers with dimensions {6} at {p,q} = {2.,4.}.
>
> The answer should have been around:
>
> {p->1,9962, q->3.93581}
>
> Does anyone know how to correct it?
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: please help using NDSolve and FindFit Bob Hanlon <hanlonr357@gmail.com> - 2012-06-08 07:34 +0000

csiph-web