Please help to rectify an error......(Error in while loop)

b=5; x1=0; x2=0.5; x3=1; dx=0.5; % Initial values
syms x
f=inline('(x^2)+(54/x)'); % Function
while x3<=b %Conditions
if f(x1)>=f(x2) && f(x2)<=f(x3) %Conditions
disp('minima lies between x1 and x3')
else
x1=x2; x2=x3; x3=x2+dx; % Modified values of x1 x2 & x3 if above condition is not satisfied.
% from these values it should goto start point until conditions are satisfied
end
end

 채택된 답변

Walter Roberson
Walter Roberson 2015년 8월 29일

0 개 추천

You have not defined dx.
You are not using "x" so "syms x" is not needed.

댓글 수: 3

dx=0.5; i forgot to write in question
As soon as the condition
if f(x1)>=f(x2) && f(x2)<=f(x3)
becomes true, it stays true because you do not alter any values in the body of the "if", only in the body of the "else". As you do not make any change to x3 when the condition holds, your "while" remains true, the if succeeds again, nothing changes, the while stays true....
You might want a "break".
Thank you Walter Roberson break is working...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

태그

질문:

2015년 8월 29일

댓글:

2015년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by