error in Euler method

. could you please, check my code I did not get as the following pic.
for i=1:length(T)-1
x1(i+1)=x1(i)+dX1*h;
x2(i+1)=x2(i)+dX2*h;
v1(i+1)=v1(i)+dX3*h;
v2(i+1)=v2(i)+dX4*h;
end

2 comentarios

James Tursa
James Tursa el 17 de Oct. de 2018
Closing your question and deleting your code and comments is not appreciated on this forum since it does not help anyone else later on. Please keep your questions and comments intact for everyone to see. Thanks.
Momo
Momo el 17 de Oct. de 2018
I have not received assistance that could be benefit to me or others. Thanks anyway.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 16 de Oct. de 2018
Editada: James Tursa el 16 de Oct. de 2018

0 votos

You need to recalculate the derivatives at each time step. So all of the stuff that goes into calculating dX needs to be inside of your for-loop. Which means that all of the equations for this calculation need to be indexed. E.g., this
r=x1-x2 ;
should be this
r=x1(i)-x2(i) ;
etc.

4 comentarios

Momo
Momo el 16 de Oct. de 2018
I still get the same result. could please help me with that
James Tursa
James Tursa el 16 de Oct. de 2018
Please post your current code.
James Tursa
James Tursa el 16 de Oct. de 2018
Editada: James Tursa el 16 de Oct. de 2018
So, when I wrote that you needed to have your derivative calculations inside of the for-loop, I was talking about your existing for-loop in your original posted code:
for i=1:length(T)-1
% Put all of your derivative calculations here
x1(i+1)=x1(i)+dX(1)*h;
x2(i+1)=x2(i)+dX(3)*h;
v1(i+1)=v1(i)+dX(2)*h;
v2(i+1)=v2(i)+dX(4)*h;
end
What you have done is create an additional for-loop (not my intention), and changed your original for-loop indexing (also not my intention). Instead, go back to your original for-loop indexing, and put your derivative code where I have the comment. Basically, at each step you need to recalculate the derivatives based on the current state.
James Tursa
James Tursa el 16 de Oct. de 2018
Editada: James Tursa el 16 de Oct. de 2018
Why are you using i=1:N instead of the original i=1:length(T)-1? Why are you overwriting dx1(i) and dx2(i) each step without doing the if-test part?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Oct. de 2018

Abierta de nuevo:

el 22 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by