Here are the M-files provided to implement the bisection & false position method. Overall the scripts are the same, the only difference is the iterative function used to evaluate the root (as highlighted in each M-file).
Bisection M-file
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
%bisect:root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% varargin = variable number of arguments, can input any number of
% arguments in the func
%input:
% func:name of function
% xl,xu=lower and upper guesses
% es=desired relative error(default=0.0001%)
% maxit=maximum allowable iterations(default=50)
% p1,p2,...=additional parameters used by func
%output:
% root=real root
% fx=function value at root
% ea=approximate relative error(%)
% iter=number of iterations
if nargin<3,error('at least 3 input arguments required'),end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0, error('no sign change'),end
if nargin<4||isempty(es),es=0.0001;end
if nargin<5||isempty(maxit),maxit=50;end
iter=0;xr=xl;ea=100;
while(1)
xrold=xr;
xr=(xl+xu)/2;
iter=iter+1;
if xr~=0,ea=abs((xr-xrold)/xr)*100;end
test=func(xl,varargin{:})*func(xr,varargin{:});
if test<0
xu=xr;
elseif test>0
xl=xr;
else
ea=0;
end
if ea<=es||iter>=maxit,break,end
end
root=xr;fx=func(xr,varargin{:});
False Position M-file
function [root,fx,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin)
%falsepos:root location zeroes
% [root,fx,ea,iter]=falsepo(func,xl,xu,es,maxit,p1,p2,...):
% uses falsepos method to find the root of func
% varargin = variable number of arguments, can input any number of
% arguments in the func
%input:
% func:name of function
% xl,xu=lower and upper guesses
% es=desired relative error(default=0.0001%)
% maxit=maximum allowable iterations(default=50)
% p1,p2,...=additional parameters used by func
%output:
% root=real root
% fx=function value at root
% ea=approximate relative error(%)
% iter=number of iterations
if nargin<3,error('at least 3 input arguments required'),end
% check for number of arg inp, if less than 3, then error message is printed out
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0, error('no sign change'),end
% if upper and lower boundary same sign, error message is printed out
if nargin<4|isempty(es),es=0.0001;end
% if no input for es, then matlab set default value as 0.0001%
if nargin<5|isempty(maxit),maxit=50;end
% if no input for maxit, then matlab set default value as 50 iterations
iter=0;xr=xl;ea=100;
% in first iteration, if f(xl)f(xr)>0,replace xl with xr, approximate relative error = 100%
while(1)
xrold=xr;
% xrold is xr value of previous iteration
xr=xu-(func(xu)*(xl-xu))/(func(xl)-func(xu));
% formula to calculate xr
iter=iter+1;
% to display next iteration
if xr~=0,ea=abs((xr-xrold)/xr)*100;end
% if xr not equal to zero, calculate ea using the formula
test=func(xl,varargin{:})*func(xr,varargin{:});
% evaluate the root by multiplying f(xl) with f(xr)
if test<0
xu=xr;
% if f(xl)f(xr)<0,replace xu with xr
elseif test>0
xl=xr;
% if f(xl)f(xr)>0,replace xl with xr
else
ea=0;
% if f(xl)f(xr)=0,found root and stop iteration
end
if ea<=es|iter>=maxit,break,end
% if ea less than or equal es, then stop iteration
end
root=xr;fx=func(xr,varargin{:});
% put the calculated xr value into the function to check the answer
No comments:
Post a Comment