forward euler for a system of equations

1 visualización (últimos 30 días)
Karl Campos
Karl Campos el 17 de Nov. de 2020
Editada: James Tursa el 18 de Nov. de 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 comentario
John D'Errico
John D'Errico el 17 de Nov. de 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.

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
James Tursa el 17 de Nov. de 2020
Editada: James Tursa el 17 de Nov. de 2020
I'm assuming this
for j=1:t1:dt
was meant to be something like this
for j=1:t1/dt
  2 comentarios
Karl Campos
Karl Campos el 18 de Nov. de 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 el 18 de Nov. de 2020
Editada: James Tursa el 18 de Nov. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by