0% found this document useful (0 votes)
238 views

False Position M-File

This MATLAB function uses the false position method to find the root of a function within a given interval, by iteratively calculating a new estimate for the root based on the slopes of the function at the interval boundaries. It takes the function, lower and upper bounds of the interval, desired error tolerance, and maximum number of iterations as inputs, and returns the calculated root, function value at the root, approximate relative error, and number of iterations as outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views

False Position M-File

This MATLAB function uses the false position method to find the root of a function within a given interval, by iteratively calculating a new estimate for the root based on the slopes of the function at the interval boundaries. It takes the function, lower and upper bounds of the interval, desired error tolerance, and maximum number of iterations as inputs, and returns the calculated root, function value at the root, approximate relative error, and number of iterations as outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

False-Position Method M-File

function [root, fx, ea, iter] = falsepos(func, xl, xu, es, maxit, varargin)
% falsepos: root location zeroe-=====
% [root, fx, ea, iter] = falsepos(func, xl, xu, es, maxit, p1, p2,...):
% uses false-position method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (defualts = 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;
while(1)
xrold = xr;
fl=func(xl,varargin{:});
fu=func(xu,varargin{:});
xr = xu-fu*(xl-xu)/(fl-fu);
iter = iter + 1;
if xr~= 0,ea = abs((xr - xrold)/xr)*100;end
test = fl*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{:});

You might also like