Matlab - program won't stop calculating

My program I created is using a simple bisection method to calculate a root, but it just keeps running. Any thoughts on how to fix my code to stop this?
myFunction = @(x) 9*(x^4) + 18*(x^3) + 38*(x^2) - 57*x +14;
format long
a = 0; % lower limit
b = 1; % upper limit
c = (a + b) / 2;
while abs(myFunction(c)) > 0.01
if (myFunction(a)*myFunction(c)) < 0
a = c;
else
b = c;
end
c = (a + b) / 2;
end
fprint('The root is %g\n', c)

 채택된 답변

Matt J
Matt J 2013년 9월 24일
편집: Matt J 2013년 9월 24일

1 개 추천

Install a maximum number of iterations. You should be able to reach 2^(-N) precision in N steps. Additionally,
if (myFunction(a)*myFunction(c)) < 0
b = c;
elseif (myFunction(b)*myFunction(c)) < 0
a = c;
else
....something
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2013년 9월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by