Solving system of differential equations using ode45

1 view (last 30 days)
I'm required to solve this sytem of odes by using ode 45. Any help to nudge me in the right direction would be appreciated.
I know that I'm supposed to create a function containing the odes to put in to Ode45. However, I'm having a bit of trouble with that. For example :
function c=NewOde(t,y)
c(1,1)=(-6.9*((y(1)*y(3))/y(1)+y(2)+y(3)+y(4)+y(5)))-0*y(1);
c(2,1)=(6.9*((y(1)*y(3))/y(1)+y(2)+y(3)+y(4)+y(5)))-0.9*y(2);
Is this the correct way to start putting all the odes into the function to pass it on to ode45?
Thanks.

Answers (1)

Steven Lord
Steven Lord on 17 Mar 2020
It is, but instead of substituting values for your parameters inline you might want to define a few helper variables at the start of your function. That would make your function look more like your mathematical equations, making it easier to understand when or if you need to review or change your code days, weeks, months, or years from now.
function c = NewOde(t, y)
S = y(1);
E = y(2);
I = y(3);
R = y(4);
D = y(5);
N = S + E + I + R;
beta = 6.9; % How often a contact results in an exposure
% etc.
dSdt = -beta*S*I/N - ...
...
c = [dSdt; ...]
end
  1 Comment
Md Latiful Sunny Shounok
Md Latiful Sunny Shounok on 17 Mar 2020
Hi,
Thank you so much for your answer. I have finished writing my code and I was just wondering if the trend is supposed to look like this. I'm getting very high numbers ( it is an epidemic model after all) however just wondering if you got the same as well.
Again, thank you so much for your answe!

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!