Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
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

Antworten (2)

Georgios Pyrgiotakis
Georgios Pyrgiotakis am 30 Nov. 2018
Bearbeitet: Georgios Pyrgiotakis am 30 Nov. 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 am 3 Dez. 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.

Diese Frage ist geschlossen.

Tags

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by