Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Hi, my while loop stops before meeting its designated conditions and gives me a result. Why is this?

1 visualización (últimos 30 días)
while error(end) <= 0.01
[t,z] = ivpSolver_Assignment_2(t0,z0,dt,tend,theta(n));
w(n) = z(1,end);
error(n) = w(n) - 2.1;
theta(n+1) = (-error(n-1)/((error(n)-error(n-1))/(theta(n)-theta(n-1)))) + theta(n-1);
n = n+1;
end
t and z are vectors

Respuestas (2)

Georgios Pyrgiotakis
Georgios Pyrgiotakis el 30 de Nov. de 2018
Editada: Georgios Pyrgiotakis el 30 de Nov. de 2018
If you have not defined error (i.e. error=ones(1,x) where x is the length of the matrice) the loop will stop imedaitely since W(end) by default is null or zero. instead write:
error=[1];
while error(end) <= 0.01
[t,z] = ivpSolver_Assignment_2(t0,z0,dt,tend,theta(n));
w = z(1,end);
error=[error, w - 2.1;]
theta(n+1) = (-error(n-1)/((error(n)-error(n-1))/(theta(n)-theta(n-1)))) + theta(n-1);
end
If you want to also keep the values for w as well, you need to also define w and append values to it.

Georgios Pyrgiotakis
Georgios Pyrgiotakis el 3 de Dic. de 2018
This is correct in the general context, but since your while depends on it the error null value will terminate the loop before even starts. You need to initiate them with values that will work for the calculations later.
theta = [x]; % You need to find a value that can be used for the next theta calculation
w= [];
error=[1];
while error(end) <= 0.01
[t,z] = ivpSolver_Assignment_2(t0,z0,dt,tend,theta(n));
w=[w,z(1,end)];
error=[error, w(end) - 2.1];
theta_tmp = (-error(end-1)/((error(end)-error(end-1))/(theta(end)-theta(end-1)))) + theta(end-1);
theta=[theta, theta_tmp]
end
That should work.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by