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