forward euler for a system of equations

1 view (last 30 days)
Karl Campos
Karl Campos on 17 Nov 2020
Edited: James Tursa on 18 Nov 2020
Hello,
I am trying to implement a code for a system of three equations and I am trying to use the for loop. importantly I want to plot the three function in the same graph , but I get a weird behaviour !
the time step is dt=0.02(suppose) my functions are phi, ro and om
t1=500;
t0=0;
t(1)=t0;
phi(1)=pi/6;
ro(1)=0;
om(1)=1;
for j=1:t1:dt
t(j+1)=t0+j*dt;
phi(j+1)=(dt*ro(j))+(phi(j));
ro(j+1)=(ro(j)*(1-dt*0.2))(19.6*dt*sin(phi(j)))+(16*(om(j)^2)*sin(phi(j))*cos(phi(j))*dt);
om(j+1)=(om(j))+(dt*0.05*cos(phi(j)))-(0.0125*dt);
end
plot(t,phi,"b--")
hold on
plot (t,ro,"r")
hold on
plot (t,om,"y")
Can you please spot where I slipped up ? in forward euler for a small dt functions should nomaly converge ! but i am getting a constant function om and other are keep on changing !
  1 Comment
John D'Errico
John D'Errico on 17 Nov 2020
But you need to tell people what the equations really should be! Otherwise, we don't know what you did wrong, without extensive guesswork on our part.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 17 Nov 2020
Edited: James Tursa on 17 Nov 2020
I'm assuming this
for j=1:t1:dt
was meant to be something like this
for j=1:t1/dt
  2 Comments
Karl Campos
Karl Campos on 18 Nov 2020
it is the time loop so it is from 1 to t1 with a time-step of dt , so I don't think what you wrote mr james
James Tursa
James Tursa on 18 Nov 2020
Edited: James Tursa on 18 Nov 2020
No. j is an index loop that is used to calculate time via this equation
t(j+1)=t0+j*dt;
j is not, in and of itself, a time loop that goes from 1 to t1 with a time-step of dt. If you were to step j at a rate of dt like you suggest and then use it as an index you would get an error. Thus, I still think you need this:
for j=1:t1/dt

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!