Using Euler's method to solve the system of ODEs
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponses (1)
Alan Stevens
le 28 Mai 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')
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!