ODE SYSTEM OF EQUATION

1 次查看(过去 30 天)
Faraz Vossoughian
Faraz Vossoughian 2020-4-13
评论: darova 2020-4-17
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 个评论
darova
darova 2020-4-17
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)

请先登录,再进行评论。

回答(1 个)

Guru Mohanty
Guru Mohanty 2020-4-16
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 个评论
Faraz Vossoughian
Faraz Vossoughian 2020-4-16
Hey thanks for your response, do you have a suggestion on how to fix the problem

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by