i have a code of bi-section method, but it doesn't run right, i think it has some few wrong things and i would appreciate if someone could correct it.
somewhere in the program i will later add a few lines of the newton method.
[code]
%it its the number of iterations, a b are the initial points, err is the error, f the function, tol the tolerance of the error and n the maximum number of iterations
function bi(f,a,b,tol,n)
it=1
u=feval(f,a)
v=feval(f,b)
c=(a+b)/2
err=abs(b-a)/2
disp('_________________')
disp('it,a,b,c,f(c),err')
disp('_________________')
if (u*v<=0)
while (it<n) & (err>tol)
w=feval(f,c)
disp('it,a,b,c,f(c),err')
if(u==0)
disp(['precise solution of x= ',num2str(a)])
elseif v==0
disp(['precise solution of x= ',num2str(b)])
elseif w==0
disp(['precise solution of x= ',num2str(c)])
elseif u*w<0
b=c
v=w
else
a=c
u=w
end
c=(a+b)/2
err=abs(b-a)/2
it=it+1
end
if err<tol
%HERE I WILL ADD THE NEWTONS METHOD%
else
disp('insufficient number of iterations')
end
else
disp('the method cannot be applied')
end
[/code]