While loop doesn't continue looping!
조회 수: 2 (최근 30일)
이전 댓글 표시
Hey everyone,
In my code for estimating the root using bisection method, while loop only outputs one iteration! yy is user defined function for which the root will be estimated, Xl is the lower bound of the interval (user-defined), Xu is the upper bound of the interval (user-defined), imax is the max number of iterations, I use a function fun to convert the symbolic function into numerical
function [iter, Xr, ea] = Bisect(yy, Xl ,Xu ,es ,imax, iter)
while iter <= imax || ea <= es
if (fun(yy,Xl) * fun(yy,Xu)) < 0 %Verify that the interval [Xl,Xu] contains at least one root by examining the sign
Xr = (Xl + Xu)/2;
if (fun(yy,Xr) * fun(yy,Xl)) < 0
ea = abs((Xr - Xu)/Xr)*100;
Xu = Xr;
else
ea = abs((Xr - Xl)/Xr)*100;
Xl = Xr;
end
break
elseif fun(yy,Xl) == 0
Xr = Xl;
ea = 0;
break
elseif fun(yy,Xu) == 0
Xr = Xu;
ea = 0;
break
elseif (fun(yy,Xl)*fun(yy,Xu)) > 0
disp('ERROR!! Please specify another upper and lower bounds for the interval on which a root might exist.')
break
end
end
iter = iter + 1; %Update the number of iterations
댓글 수: 0
답변 (1개)
Naty S
2013년 2월 26일
Use continue instead of break. But i don't think you really need all those breaks or continues. Try without them. and in the elseif, you should change it from elseif fun(yy,xu)==0 to elseif fun(yy,xu)<Epsilon
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!