diff options
Diffstat (limited to 'Functions/falsePosition.m')
| -rw-r--r-- | Functions/falsePosition.m | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Functions/falsePosition.m b/Functions/falsePosition.m new file mode 100644 index 0000000..d2cb776 --- /dev/null +++ b/Functions/falsePosition.m @@ -0,0 +1,45 @@ +function [root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin) +%falsePosition finds the root of a function using false position method +% Syntax: +% [root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin) +% +% Input: +% func = function being evaluated +% xl = lower guess +% xu = upper guess +% es = desired relative error (default 0.0001%) +% maxit = maximum number of iterations (default 200) +% varargin, ...= any additional parameters used by the function +% Output: +% root = estimated root location +% fx = function evaluated at root location +% ea = approximated relative error (%) +% iter = number of iterations performed + +if nargin < 3, error('Too few arguments: at least 3 needed'), end +if nargin < 4 | isempty(es), es = 0.0001; end +if nargin < 5 | isempty(maxit), maxit = 200; end + +iter=0; +ea=100; +xr= xl; + +while(1) + xrold= xr; + iter = iter + 1; + xr=xu-func(xu)*(xl-xu)/(func(xl)-func(xu)); + if xr~=0 + ea = abs((xr-xrold)/xr)*100; + end + if func(xl)*func(xr)<0 + xl=xr; + else + xl=xr; + end + if iter == maxit | ea <= es, break, end +end +root = xr; +fx=func(xr,varargin{:}); +ea=ea; +iter=iter; +end
\ No newline at end of file |
