Multiple conditions inside WHILE LOOP

2 Ansichten (letzte 30 Tage)
QUAN MAI
QUAN MAI am 14 Sep. 2015
Beantwortet: Walter Roberson am 14 Sep. 2015
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

Antworten (1)

Walter Roberson
Walter Roberson am 14 Sep. 2015
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".

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by