Using Euler's method to solve the system of ODEs

16 visualizzazioni (ultimi 30 giorni)
Jiwon Park
Jiwon Park il 28 Mag 2022
Risposto: Alan Stevens il 28 Mag 2022
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 Commento
Sam Chak
Sam Chak il 28 Mag 2022
Modificato: Sam Chak il 28 Mag 2022
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.

Accedi per commentare.

Risposte (1)

Alan Stevens
Alan Stevens il 28 Mag 2022
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')

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by