       • To: mathgroup at smc.vnet.net
• From: Bob Hanlon <hanlonr357 at gmail.com>
• Date: Fri, 8 Jun 2012 03:34:16 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <201206070918.FAA02436@smc.vnet.net>

```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==q},f,{t,0,5}][];

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,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==q},f,t][];

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,Point[data]}]

Bob Hanlon

On Thu, Jun 7, 2012 at 5:18 AM, Lee <lee787 at 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 == 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?
>

```

• Prev by Date: Re: Memory Blowup Issues
• Next by Date: Fitting complex functions or simultaneous fit of functions with identical parameters with Mathematica