A Few MATLAB Functions, Implementing Root Finding Methods


function orbit=bisection(func,left,right,iterations)
% Shows the points obtained by the Bisection Method when
% solving the equation func(x)=0. The two initial points
% are supplied, along with the number of required iterations.
% It is assumed that the initial interval is "good".
% Of course, in practice one should not set beforehand the
% number of iterations, but rather stop when reaching a
% sufficiently small neighbourhood of the root.

orbit=[];
for i=1:iterations
middle=(left+right)/2;
if feval(func,left)*feval(func,middle)>0 left=middle;
else right=middle;
end
orbit=[orbit,middle];
end
end;


function orbit=newton(func,func_der,initial,iterations)
% Shows the points obtained by Newton's Method when
% solving the equation func(x)=0. The derivative
% func-der must be supplied, along with the initial
% point and number of required iterations.

point=initial; orbit=point;
for i=1:iterations
point=point-feval(func,point)/feval(func_der,point);
orbit=[orbit,point];
end
end;


function orbit=secant(func,current_1,current_2,iterations)
% Shows the points obtained by the Secant Method when
% solving the equation func(x)=0. The two initial
% points are supplied, along with the number of
% required iterations.

orbit=[current_1,current_2];
for i=1:iterations
slope=(feval(func,current_2)-feval(func,current_1))/(current_2-current_1);
current_1=current_2;
current_2=current_2-feval(func,current_2)/slope;
orbit=[orbit,current_2];
end
end;