I am trying to write a code for bisection method using functions but they keep showing me errors in line 24.
Afficher commentaires plus anciens
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 commentaires
Kevin Chng
le 12 Oct 2018
Modifié(e) : Kevin Chng
le 12 Oct 2018
which line of code and what is the error?
Rafin Khan
le 12 Oct 2018
Rafin Khan
le 12 Oct 2018
Kevin Chng
le 12 Oct 2018
Modifié(e) : Kevin Chng
le 12 Oct 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.
Rafin Khan
le 12 Oct 2018
Réponses (0)
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!