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);

Answers (1)

Jan
Jan on 24 Apr 2021
Edited: Jan on 24 Apr 2021
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 Comments

Right, my fault. But still the drawing is one straight f = 0
Drawing of (t,x)
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))

Sign in to comment.

Asked:

on 24 Apr 2021

Commented:

Jan
on 25 Apr 2021

Community Treasure Hunt

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

Start Hunting!