forward euler for a system of equations
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
John D'Errico
le 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.
Réponses (1)
James Tursa
le 17 Nov 2020
Modifié(e) : James Tursa
le 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 commentaires
James Tursa
le 18 Nov 2020
Modifié(e) : James Tursa
le 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
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!