Multiple conditions inside WHILE LOOP

조회 수: 1 (최근 30일)
QUAN MAI
QUAN MAI 2015년 9월 14일
답변: Walter Roberson 2015년 9월 14일
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