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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by