How to solve a system of five linear differential equations

5 views (last 30 days)
I want to solve a set of differential equations in Matlab It's a kinetic model of a reaction network. I've been searching for similar questions and I found the following script which I tried to make suitable for my case:
k1 = 0.04;
k2 = 0.02;
k3 = 0.01;
k4 = 0.005;
k5 = 0.004;
k6 = 0;
tspan = [0 15000];
function dy = odefun(t,y)
dy = zeros(5,1);
dy(1) = -(k1+k2)*y(1);
dy(2) = -(k3+k4)*y(2);
dy(3) = k1*y(1)+k3*y(2)-k5*y(3);
dy(4) = k2*y(1)+k4*y(2)-k6*y(4);
dy(5) = k5*y(3)+k6*y(4);
[t,y] = ode45(@odefun,tspan,[40 40 0 0 0]);
plot(t,y(:,1));
end
However at the moment it does not work while it does not give errors when I run the script. Could someone help me to solve this problem?
  1 Comment
John D'Errico
John D'Errico on 25 Apr 2017
I would STRONGLY suggest that you read the examples shown in the docs for ODE45. Try them out. Think about why it might be a bad idea to have a function odefun that calls ode45, which then calls odefun, which then calls ode45, ad infinitum.

Sign in to comment.

Accepted Answer

Sudarshan Kolar
Sudarshan Kolar on 25 Apr 2017
Hello Ruben,
Please use Matlab debugger and step through the code to see the issue. You can also observe values as you debug through the code.
https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
https://www.mathworks.com/help/matlab/matlab_prog/examine-values.html

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!