MATLAB: Create a function that will use the Secant method to try to find a root by modifying the False Position method function written bellow. Because this method may diverge, you should limit the number of time the loop repeats.
function [fproot,zeroerror] = falsepos(func, low, high, accepterror)
% function to find the root of an equation using false position method and
% two guesses.
% input: function, low value for a guess, high value for a guess, and
% acceptable error for root.
% output: The root or an empty matrixif the root is not found.
% processing: Find a possibility for the root by creating a line between
% low value high value. New possibility is where the line
% intersects the x-axis. New possibility may be calculated by
% the equation
% xnew = xhigh - ((f(xhigh)(xlow -xhigh))/(f(xlow) - f(xhigh))
% Then f(xnew) will be calculated. If f(xnew) is equal to 0,
% set root to xnew and set logical variable to false.
% Otherwise compare the sign of f(xnew) is the same as the
% sign of low. If it the same, then set low to xnew,
% set high to xnew. Repeat until root is found or difference
% between xlow and xhigh is less than 0.0000001.
notfound = true;
diff = abs(high - low);
fproot =[];
while (notfound & diff > 0.00000001 )
flow = func(low);
fhigh = func(high);
xnew = high - ((fhigh)*(low - high))/(flow - fhigh);
fnew = func(xnew);
if abs(fnew) < accepterror
fproot = xnew;
notfound = false;
zeroerror = fnew;
elseif sign(fnew) == sign(flow)
low = xnew;
else
high = xnew;
end
diff = abs(high - low);
end
if notfound
disp('No roots were found in this range!')
disp('Try again with new guesses')
end