I am trying to write a code for bisection method using functions but they keep showing me errors in line 24.
Mostrar comentarios más antiguos
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 comentarios
Kevin Chng
el 12 de Oct. de 2018
Editada: Kevin Chng
el 12 de Oct. de 2018
which line of code and what is the error?
Rafin Khan
el 12 de Oct. de 2018
Rafin Khan
el 12 de Oct. de 2018
Kevin Chng
el 12 de Oct. de 2018
Editada: Kevin Chng
el 12 de Oct. de 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
el 12 de Oct. de 2018
Respuestas (0)
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!