ODE SYSTEM OF EQUATION
1 view (last 30 days)
Show older comments
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
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)
Answers (1)
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-
- Inside your ‘RK2’ function there is no defined value of ‘xn’.
- Inside ‘RK2’ matlabFunction function converts Symbolic expression to function handle. So, the first ODE becomes 3 Variable function.
- 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.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!