0% found this document useful (0 votes)
27 views2 pages

Function: 'At Least 3 Input Arguments Required'

This MATLAB function uses the bisection method to find the root of a function within a given interval. It takes in the function, lower and upper bounds of the interval, desired error tolerance, and maximum number of iterations. It iteratively calculates a midpoint between the bounds and determines if the function changes sign to narrow the interval containing the root. The root, function value at the root, approximate relative error, and number of iterations are returned. It checks that the function changes signs at the bounds initially and iterates until the error is below the tolerance or the maximum number of iterations is reached.

Uploaded by

Raúl Cipriano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Function: 'At Least 3 Input Arguments Required'

This MATLAB function uses the bisection method to find the root of a function within a given interval. It takes in the function, lower and upper bounds of the interval, desired error tolerance, and maximum number of iterations. It iteratively calculates a midpoint between the bounds and determines if the function changes sign to narrow the interval containing the root. The root, function value at the root, approximate relative error, and number of iterations are returned. It checks that the function changes signs at the bounds initially and iterates until the error is below the tolerance or the maximum number of iterations is reached.

Uploaded by

Raúl Cipriano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

function [root,fx,ea,iter]=bisect_false

position(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes - Bisection Method
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection 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 (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
%--------------------------------------------------------------%
% Input Arguments and check for existing root by change of sign of the
function
%--------------------------------------------------------------%
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
%--------------------------------------------------------------%
% Assigning Initial Values
%--------------------------------------------------------------%
iter = 0; xr = xl; ea = 100;
%--------------------------------------------------------------%
% Root Calculation
%--------------------------------------------------------------%
while (1)
xrold = xr;
xr = (xl + xu)/2;
% xr = xu - func(xu,varargin{:})*(xl-xu)/(func(xl,varargin{:})func(xu,varargin{:}));
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{:});
%-------------------------------------------------------------Error using dbstatus
Error: File: G:\ME-140\bisect_false.m Line: 1 Column: 41
Unexpected MATLAB expression.

Published with MATLAB R2015b

You might also like