Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
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

Réponses (2)

Georgios Pyrgiotakis
Georgios Pyrgiotakis le 30 Nov 2018
Modifié(e) : Georgios Pyrgiotakis le 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.
  1 commentaire
Abdul Akif Jabir
Abdul Akif Jabir le 2 Déc 2018
Hi, I had already initalised vectors for error and w, is this an appropriate way?
w = [];
error = [];
theta = [];

Georgios Pyrgiotakis
Georgios Pyrgiotakis le 3 Déc 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.

Cette question est clôturée.

Tags

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by