Multiple print outside of loop

1 回表示 (過去 30 日間)
Christina Mil
Christina Mil 2019 年 12 月 15 日
コメント済み: Christina Mil 2019 年 12 月 15 日
I get multiple print of 'The root of the equation is:' even though it is out of the loop
My code is:
clc;
clear all;
%syms x
f=@(x) 54.*(x.^6) + 45.*(x.^5) - 102.*(x.^4) - 69.*(x.^3) + 35.*(x.^2) + 16.*x - 4;
d=1/2*(10^(-5));
flag = false;
a=-2;
b=0;
i=0;
if f(a)*f(b)>=0
disp('Wrong beginning and end of interval')
else
while abs(a-b)>= d
c=a + (b-a).*rand(100,1);
i=i+1;
flag=true;
x1=c;
x2=a;
if x1.*x2<0
b=c;
else
a=c;
end
end
end
if flag
fprintf('The root of the equation is:%.5f\n',c);
fprintf('Iterations :%d',i);
end

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 15 日
Your c is a vector because of the rand(100, 1)
None of the code is expecting a vector for c.
Note that if you are trying to generate values in the range 1 to 100 you should be using randi(100) for integer random numbers
  1 件のコメント
Christina Mil
Christina Mil 2019 年 12 月 15 日
Thanks you very much! It works.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by