Using Euler's method to solve the system of ODEs

조회 수: 17 (최근 30일)
Jiwon Park
Jiwon Park 2022년 5월 28일
답변: Alan Stevens 2022년 5월 28일
I am trying to solve system of ode using Euler's method.
dv/dt = rv pvx - qvz
dx/dt = cv bx
dz/dt = kv bz
The system of ODE is show above.
The code below is what I have gotten so far, but I don't think I have a good understanding of ODES and Euler's method.
Could someone show how I could implement Euler's method to solve this ODE?
r=2.5;
p=2;
c=0.1;
b=0.1;
q=1;
k=0.1;
h = 0.1;
x = 0:h:365;
y = zeros(size(x));
y(1) = 0.01;
n = numel(y);
% The loop to solve the DE
for i=1:n-1
f = ODE_eq(y,r,p,c,b,q,k);
y(i+1) = y(i) + h * f;
end
%
function dydt=ODE_eq(y,r,p,c,b,q,k)
dydt=zeros(3,1);
dydt(1)=r.*y(1)-p.*y(1).*y(2)-q.*y(1).*y(3);
dydt(2)=c.*y(1).*y(2)-b.*y(2);
dydt(3)=k.*y(1)-b.*y(3);
end
  댓글 수: 1
Sam Chak
Sam Chak 2022년 5월 28일
편집: Sam Chak 2022년 5월 28일
If I remember correctly, it has something to do with the concept of iterations. Is it taught in the class?
Try finding an image to attach here. It probably helps to explain why Euler method works this way.

댓글을 달려면 로그인하십시오.

답변 (1개)

Alan Stevens
Alan Stevens 2022년 5월 28일
Probably easier to keep tabs on what is happening if you simplify as follows:
r=2.5;
p=2;
c=0.1;
b=0.1;
q=1;
k=0.1;
h = 0.1;
t = 0:h:365; % I think this should be t not x
n = numel(t);
v = zeros(n,1);
x = zeros(n,1);
z = zeros(n,1);
% Set initial values as desired
v(1) = 1;
x(1) = 0.5;
z(1) = 0;
% The loop to solve the DE
for i=1:n-1
v(i+1) = v(i) + v(i)*(r - p*x(i) - q*z(i))*h;
x(i+1) = x(i) + (c*v(i) - b*x(i))*h;
z(i+1) = z(i) + (k*v(i) - b*z(i))*h;
end
plot(t,v,t,x,t,z),grid
xlabel('t'), ylabel('v,x,z')
legend('v','x','z')

카테고리

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