Solving a system of ODE
4 views (last 30 days)
Show older comments
Esraa Abdelkhaleq
on 14 Feb 2017
Commented: Star Strider
on 15 Feb 2017
Hello,
How can I solve a system of two ODE's as follows,
dNcdt= Nc(t)*Kgr- dc*Nc(t)*Ni(t)
dNidt= ai*Nc(t) - di*Ni(t)
To obtain Nc(t) and Ni(t).
Thanks in advance.
0 Comments
Accepted Answer
Star Strider
on 14 Feb 2017
It is easier to let the Symbolic Math Toolbox do the algebra:
syms ai dc di Kgr Nc(t) Ni(t) t Y
Eq1 = diff(Nc) == Nc(t)*Kgr- dc*Nc(t)*Ni(t);
Eq2 = diff(Ni) == ai*Nc(t) - di*Ni(t);
[odesys, vrs] = odeToVectorField(Eq1, Eq2);
odefcn = matlabFunction(odesys, 'Vars',[t Y ai dc di Kgr])
odefcn =
function_handle with value:
@(t,Y,ai,dc,di,Kgr)[ai.*Y(2)-di.*Y(1);Kgr.*Y(2)-dc.*Y(1).*Y(2)]
The rest should be relatively straightforward for you to complete. To solve it numerically, begin with ode45, and if your equation turns out to be ‘stiff’ because of a wide variation in parameter magnitudes, and ode45 has problems, use ode15s or one of the other stiff solvers appropriate to your system.
8 Comments
Star Strider
on 15 Feb 2017
My pleasure.
Out doing stuff for 3 hours (life intrudes) so just got back to this topic.
This code:
syms ai dc di fc fch fi fih Ke Kgr Nc(t) Ni(t) Rc Rch Ri Rih t Y Nc0 Nch0 Nih0 Ni0 qpl(t)
Eq1 = diff(Nc) == Nc(t)*Kgr- dc*Nc(t)*Ni(t);
Eq2 = diff(Ni) == ai*Nc(t) - di*Ni(t);
Eq3 = diff(qpl) == fc*Rc*Nc(t)+ fi*Ri*Ni(t)+fch*Rch*Nch0+ fih*Rih*Nih0-Ke*qpl(t) ;
[odesys, vrs] = odeToVectorField(Eq1, Eq2, Eq3)
odefcn = matlabFunction(odesys, 'Vars',[t Y ai dc di fc fch fi fih Ke Kgr Nc0 Nch0 Nih0 Ni0 Rc Rch Ri Rih])
produces:
odesys =
ai*Y[2] - di*Y[1]
Kgr*Y[2] - dc*Y[1]*Y[2]
Nch0*Rch*fch - Ke*Y[3] + Nih0*Rih*fih + Rc*fc*Y[2] + Ri*fi*Y[1]
vrs =
Ni
Nc
qpl
odefcn = @(t,Y,ai,dc,di,fc,fch,fi,fih,Ke,Kgr,Nc0,Nch0,Nih0,Ni0,Rc,Rch,Ri,Rih) [ai.*Y(2)-di.*Y(1);Kgr.*Y(2)-dc.*Y(1).*Y(2);-Ke.*Y(3)+Nch0.*Rch.*fch+Nih0.*Rih.*fih+Rc.*fc.*Y(2)+Ri.*fi.*Y(1)];
The ‘vrs’ output you can interpret as:
Y(1) = Ni
Y(2) = Nc
Y(3) = qpl
You have to supply all the values for the parameters, so you can then integrate it with ode45 or ode15s (or other solver, depending on your requirements).
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!