ODE SYSTEM OF EQUATION

1 view (last 30 days)
Faraz Vossoughian
Faraz Vossoughian on 13 Apr 2020
Commented: darova on 17 Apr 2020
Hi,
I'm trying to solve a system of ode's using Runge-kutta, i made a function for RK2(f,h,x0,y0,xfinal) and tried to solve the system shown below with specified IC's. Could someone help fix the code as I get errors and code doesnt work.
clear all
clc
beta=1/3;
gamma=1/7;
syms R S I
N=S+I+R;
ode1=-(beta*I*S)/N
ode2=-(beta*I*S)/N-gamma*I
ode3=gamma*I
odes=[ode1,ode2,ode3]
for j=odes
RK2(j,0.2,0,8e6,7)
end
function [xs,ys]=RK2(f,h,x0,y0,xfinal)
f=matlabFunction(f);
fprintf('\n x y ');
o=1;
while x0<=xn
fprintf('\n%4.3f %4.3f ',x0,y0); %values of x and y
xs(o)=x0;
ys(o)=y0;
k1=h*f(x0,y0);
x1=x0+h;
k2=h*f(x1,y0+k1);
y1=y0+(k1+k2)/2;
x0=x1;
y0=y1;
o=o+1;
end
end
  1 Comment
darova
darova on 17 Apr 2020
I'd suggest you to try to solve use euler method first
Something like this:
% constants beta I N
ds = @(s) -beta*I*s/N;
dt = 0.1;
s = 0.1;
for i = 2:100
s(i+1) = s(i) + dt*ds(s(i));
end
plot(s)

Sign in to comment.

Answers (1)

Guru Mohanty
Guru Mohanty on 16 Apr 2020
I understand you are trying to solve system of ODEs using RK iteration. The error occurred in the iteration due to following reasons-
  1. Inside your ‘RK2’ function there is no defined value of ‘xn’.
  2. Inside ‘RK2’ matlabFunction function converts Symbolic expression to function handle. So, the first ODE becomes 3 Variable function.
  3. In the following code
k1=h*f(x0,y0);
The code will error out because the function handle takes 3 inputs and, in the statement, only two inputs are present.
After Resolving these issues, the code should work.
  1 Comment
Faraz Vossoughian
Faraz Vossoughian on 16 Apr 2020
Hey thanks for your response, do you have a suggestion on how to fix the problem

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!