Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

>> a=input('Enter Function:','s');

f=inline(a);
xl=input('Enter lower guess:') ;
xu=input('Enter upper guess:');
tol=input('Enter tolerance(recommended 0.001):');
for i=2:100
if f(xu)>f(xl)
xr=xu-((f(xu)*(xu-xl))/(f(xu)-f(xl)));
else
xr=xl-((f(xl)*(xl-xu))/(f(xl)-f(xu)));
end
if f(xu)*f(xr)<0
xl=xr;
else
xu=xr;
end
if f(xl)*f(xr)<0
xu=xr;
else
xl=xr;
end
xnew(1)=0;
xnew(i)=xr;
if abs(100*(xnew(i)-xnew(i-1))/xnew(i))<tol,break,end
end
fprintf('Root: %0.9f',xr)

-3*s.^3+19*s.^2-20*s-13

You might also like