Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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

回答(2 个)

Georgios Pyrgiotakis
编辑:Georgios Pyrgiotakis 2018-11-30
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
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.

此问题已关闭。

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by