# non linear Least Squares with model function given by ODE's

I have to fit to experimental data a model function given by a linear combination of functions y1(t) and y2(t)

        a1*y1(t) + a2*y2(t)


with a1 and a2 adjustable parameters.

The functions y1 and y2 are obtained by solving two coupled ODE's :

y1'(t)+y1(t)/t1-f(t)==0 with y1(0)==0

y2'(t)+y2(t)/t2-f(t)-a*y1(t)=0 with y2(0)==0


The function f(t) is known analytically.

Thus, there are 5 adjustable parameters a, a1, a2, t1, t2.

I found the following example in the Mathematica documentation, namely:

model[a_?NumberQ, b_?NumberQ, c_?NumberQ] :=  Module[{y, x},First[y /.NDSolve[{y''[x] + a y[x] == 0, y[0] == b, y'[0] == c},
y, {x, 0, 10}]]]

nlm = NonlinearModelFit[data, model[a, b, c][x], {a, b, c}, x]


How can I generalize the given example to my case?

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 May 14 '15 at 14:22
• – Michael E2 May 14 '15 at 14:24
• Post your data and the function f, please. – Ivan May 14 '15 at 20:47

## 1 Answer

paramSol =
ParametricNDSolve[{y1'[t] + y1[t]/t1 - f[t] == 0 , y1[0] == 0,
y2'[t] + y2[t]/t2 - f[t] - a*y1[t] == 0 , y2[0] == 0}, {y1,
y2}, {t, 0, 10}, {a, t1, t2}];

model = Evaluate[a1 y1[a, t1, t2][t] + a2 y2[a, t1, t2][t] /. paramSol];

nlm = NonlinearModelFit[data, model, {a, a1, a2, t1, t2}, t]