I am trying to write a code for bisection method using functions but they keep showing me errors in line 24.
显示 更早的评论
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 个评论
Kevin Chng
2018-10-12
编辑:Kevin Chng
2018-10-12
which line of code and what is the error?
Rafin Khan
2018-10-12
Rafin Khan
2018-10-12
Kevin Chng
2018-10-12
编辑:Kevin Chng
2018-10-12
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
2018-10-12
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!