I am trying to write a code for bisection method using functions but they keep showing me errors in line 24.

6 Ansichten (letzte 30 Tage)
function [root,ea,iter]=HomeTask2(f,xi,xf,es,maxit)
iter=0;
xm=(xi+xf)/2;
ab=0;
while(ab~=1)
if (f(xi)*f(xf)>0)
disp('Error')
break;
end
if (f(xf)*f(xm)==0) || (f(xi)*f(xm)==0)
ab=ab+1;
end
xmold=xm;
iter=iter+1;
if (f(xi)*f(xm)<0)
xf=xm;
else
xi=xm;
end
if iter>1
ea=abs((xm-xmold)/xm)*100;
end
if (ea<=es)||(iter>=maxit)
break;
end
end
root=xm;
  5 Kommentare
Kevin Chng
Kevin Chng am 12 Okt. 2018
Bearbeitet: Kevin Chng am 12 Okt. 2018
Hi, take a look at your code
if iter>1
ea=abs((xm-xmold)/xm)*100;
end
At first line, you assign 0 to iter, therefore your program does not meet the condition enter this if loop as iter must be bigger than 1.
So when come to line
if (ea<=es)||(iter>=maxit)
There is no value been assigned for ea. That's why return error to you.
So far, you did good work, try again. I guess you might need to take a look at your bisection logic again.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by