Euler's Method system of odes

Hello, I'm new to MATLAB and I'm stuck. I am trying to solve two first order ode using the Forward Eulers method. I thought my code was correct, but I think the first plots arewrong. Can someone please take a look at this?
%parametrs
A= 5;
B=50;
C=0.1;
D=1e-5;
h=0.0001;
t_final=10;
N=t_final/h;
t(1)=0;
x(1)=0;
y(1)=0;
% equations
% dx/dt = 1/D*x
% dy/dt=(1/C)*(A-(B*y)-x)
for i=1:N
t(i+1)=t(i)+h;
x(i+1)=(1./D)*x(i);
y(i+1)=(1./C)*(A-(B*y(i))-x(i));
end
figure(1);clf(1)
plot(t,x)
figure(2);clf(2)
plot(t,y);

Antworten (1)

Jan
Jan am 24 Apr. 2021
Bearbeitet: Jan am 24 Apr. 2021

1 Stimme

You determine the derivatives:
% dx/dt = 1/D*x
% dy/dt=(1/C)*(A-(B*y)-x)
Now x(i+1) and y(i+1) are not the values of these derivatives at the point x(i), y(i), but:
x(i+1) = x(i) + (1./D)*x(i) * h;
y(i+1) = y(i) + (1./C)*(A-(B*y(i))-x(i)) * h;

3 Kommentare

Karol T
Karol T am 24 Apr. 2021
Right, my fault. But still the drawing is one straight f = 0
Karol T
Karol T am 24 Apr. 2021
Drawing of (t,x)
Jan
Jan am 25 Apr. 2021
If x starts at 0, it must sty at 0 according to the formula. y is not a straight line:
plot(t(1:100), y(1:100))

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 24 Apr. 2021

Kommentiert:

Jan
am 25 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by