Iteration of two equations with two unknowns

Hello, I have these two equations: where R0 and V0 are the known initial values with the range of t given.
V1=(A/R0)*(t1-t0) + V0
and
R1=V0*(t1-t0) + R0.
The second iteration will be in the form of
V2=(A/R1)*(t2-t1) + V1
and
R2=V1*(t2-t1) + R1 etc...
My challenge is that I want the iteration to terminate at a particular value R which I don't know how to incorporate into a 'for loop' code. Kindly assist.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Abr. de 2018
You do not use a "for" loop for something like that. You use a "while" loop.
while( abs(R(end) - target_R) > tolerance
...
end
However, it is safer to put an upper limit on the iterations in case there is no convergence:
for iter = 1 : 1000
...
if abs(R(end) - target_R) <= tolerance; break; end
end

4 comentarios

Shozeal
Shozeal el 4 de Mayo de 2018
Editada: Walter Roberson el 4 de Mayo de 2018
Thank you, Walter.
Maybe I should type in the code I have written here as I am still not getting the expected result. I will assume values for each known variable.
t_final = 10;
t_delta=t_final/N
A = 200 ;
n = 0;
V_0 = 2e4;
R_0 = 0.2:20:2e4;
hold on
while n<length (R_0)
n = n+1
if k(1)==1
V(k)=V_0
R(k)=R_0
end
for k=1:N;
V(k+1)=(A/R(k))*t_delta+V(k);
R(k+1)=V(k)*t_delta+R(k);
end
hold on
plot(t_delta,R)
end
hold off
R
V
Based on what I wrote up here, I did not get anything reasonable.
Thanks.
Walter Roberson
Walter Roberson el 4 de Mayo de 2018
What is N ?
I do not know what you are trying to do. Maybe something like
N = 100;
t_final = 10;
t_delta = t_final/N;
A = 200 ;
n = 0;
V_0 = 2e4;
R_0 = 0.2;
V=V_0;
R=R_0;
times = linspace(0,t_final,N+1);
for k=1:N
V(k+1)=(A/R(k))*t_delta+V(k);
R(k+1)=V(k)*t_delta+R(k);
end
plot(times,R)
hold off
disp(R(end))
disp(V(end))
Shozeal
Shozeal el 8 de Mayo de 2018
This is so perfect, I really appreciate. I only needed to add the 'while' statement to accommodate my vector values for R and t.
Thanks a lot.

Iniciar sesión para comentar.

Más respuestas (3)

Shozeal
Shozeal el 5 de Mayo de 2018

0 votos

I have attached a copy of what I am trying to achieve.
Thanks for the guidance.

1 comentario

Walter Roberson
Walter Roberson el 5 de Mayo de 2018
The code I posted should do that.
The V values become close to constant in just a few iterations, so the R ends up looking indistinguishable from linear.
It looks V is an acceleration of some sort and R is a velocity.

Iniciar sesión para comentar.

Shozeal
Shozeal el 5 de Mayo de 2018

0 votos

V is the velocity while R is like a position... displacement kind of.
Thanks, I believe what you sent earlier should do that.

Categorías

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

Preguntada:

el 30 de Abr. de 2018

Comentada:

el 8 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by