필터 지우기
필터 지우기

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일
편집: 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.
  댓글 수: 1
Abdul Akif Jabir
Abdul Akif Jabir 2018년 12월 2일
Hi, I had already initalised vectors for error and w, is this an appropriate way?
w = [];
error = [];
theta = [];

Georgios Pyrgiotakis
Georgios Pyrgiotakis 2018년 12월 3일
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