Multiple conditions inside WHILE LOOP

1 次查看(过去 30 天)
I try to write code to do Bisection method after get input from user, but I'm stuck right now. Basically, after the WHILE statement, I put multiple conditions in order but matlab doesn't work. This is my code:
if true
% code
c2=input('what is the coefficient for x^2');
c1=input('what is the coefficient for x');
c0=input('What is the last coefficient');
a=input('What is value for a');
b=input('what is value for b');
c=0.5*(a+b);
value=c2*(c^2)+c1*c+c0
while value>=1e-6
(a>0 && c>0) || (a<0 && c<0);
a==c
else b==c
end
I stuck at the WHILE LOOP. I want after I get the "value", matlab will check "value">=1e-6. If that's true, it will set a=c if a and c same sign, otherwise it will set b=c. Then run the equation: value=c2*(c^2)+c1*c+c0 again with update a or b depend on condition until the "value"<1e-6.
Thanks

回答(1 个)

Walter Roberson
Walter Roberson 2015-9-14
a==c is a comparison, not an assignment.
while value>=1e-6
(a>0 && c>0) || (a<0 && c<0);
means to test whether value>=1e-6 . If it is then the value
(a>0 && c>0) || (a<0 && c<0)
is computed and then the value is thrown away because of the ';'.
Then you have an "else" even though you are not inside an "if".

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by