フィルターのクリア

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

2 ビュー (過去 30 日間)
Maruti Patil
Maruti Patil 2015 年 8 月 29 日
コメント済み: Maruti Patil 2015 年 8 月 30 日
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 日
You have not defined dx.
You are not using "x" so "syms x" is not needed.
  3 件のコメント
Walter Roberson
Walter Roberson 2015 年 8 月 30 日
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".
Maruti Patil
Maruti Patil 2015 年 8 月 30 日
Thank you Walter Roberson break is working...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by