I'm trying to fit some data into a model using the standard procedure of minimizing the chi-square function using this code:
ClearAll["Global`*"]
Needs["ErrorBarPlots`"];
a0 = 1;
b0 = 2;
sol[a_, b_] :=
NDSolve[{x'[t] == -a*x[t] + Sin[t], y'[t] == a*x[t] - b*y[t]^2,
z'[t] == b*y[t] - Cos[t], x[0] == 1, y[0] == 0, z[0] == 0}, {x, y,
z}, {t, 0, 10}, MaxSteps -> Infinity];
F[t_] := x[t] + y[t]^2 + z[t]^3;
wsol[a_,
b_] := (wsol[a, b] =
NDSolve[{w'[t] - w[t] - ((F[t] /. sol[a, b])[[1]]) == 0,
w[0] == 0}, w, {t, 0, 10}]);
model[t_, a_, b_] := (w[t] /. wsol[a, b])[[1]] // Chop;
SeedRandom[1264645];
rangoT = Range[0, 9, 0.5];
ndat = Length[rangoT];
DATA = Table[{t,
model[t, a0, b0] + RandomVariate[NormalDistribution[0, .1]],
RandomReal[{.05, .1}]}, {t, rangoT}];
chi2[a_, b_] :=
Sum[((DATA[[i, 2]] - model[DATA[[i, 1]], a, b])/DATA[[i, 3]])^2, {i,
1, ndat}]
FindMinimum[chi2[a, b], {w, 0.9}, {b, 1.1}]
But when I run it I get error messages from the FindMinimum command related to NDSolve and ReplaceAll. Everything runs just fine until using that command. I was wondering why's not working and what should be changed.
DATA
doesn't work.NDSolve
throws copious errors at that point. Is that not the case for you? $\endgroup$DATA=...
, please correct the description. 2. Use something rather thant
in theTable
will fix the issue. (Don't forgett
is already used in thoseNDSolve
s and you haven't localize them! ) 3.{w, 0.9}
in last line is obviously wrong. 4. You need_?NumericQ
, see this post for more info: mathematica.stackexchange.com/a/26037/1871 $\endgroup$