Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding files
adding files
  • Loading branch information
tjc14008 committed Oct 5, 2017
1 parent 2bdf30d commit 8728f8c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bisect.m
@@ -0,0 +1,37 @@
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
% 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{:});
39 changes: 39 additions & 0 deletions falsepos.m
@@ -0,0 +1,39 @@
function [root,fx,ea,iter]=falsepos(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
% 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;
% xr = (xl + xu)/2; % bisect method
xr=xu - (f_m(xu)*(xl-xu))/(f_m(xl)-f_m(xu)); % false position method
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{:});
36 changes: 36 additions & 0 deletions goldmin.m
@@ -0,0 +1,36 @@
function [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,varargin)
% goldmin: minimization golden section search
% [x,fx,ea,iter]=goldmin(f,xl,xu,es,maxit,p1,p2,...):
% uses golden section search to find the minimum of f
% input:
% f = 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 f
% output:
% x = location of minimum
% fx = minimum function value
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
phi=(1+sqrt(5))/2;
iter=0;
while(1)
d = (phi-1)*(xu - xl);
x1 = xl + d;
x2 = xu - d;
if f(x1,varargin{:}) < f(x2,varargin{:})
xopt = x1;
xl = x2;
else
xopt = x2;
xu = x1;
end
iter=iter+1;
if xopt~=0, ea = (2 - phi) * abs((xu - xl) / xopt) * 100;end
if ea <= es | iter >= maxit,break,end
end
x=xopt;fx=f(xopt,varargin{:});
30 changes: 30 additions & 0 deletions mod_secant.m
@@ -0,0 +1,30 @@
function [root,ea,iter]=mod_secant(func,dx,xr,es,maxit,varargin)
% newtraph: Modified secant root location zeroes
% [root,ea,iter]=mod_secant(func,dfunc,xr,es,maxit,p1,p2,...):
% uses modified secant method to find the root of func
% input:
% func = name of function
% dx = perturbation fraction
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4 || isempty(es),es=0.0001;end
if nargin<5 || isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
dfunc=(func(xr+dx)-func(xr))./dx;
xr = xr - func(xr)/dfunc;
iter = iter + 1;
if xr ~= 0
ea = abs((xr - xrold)/xr) * 100;
end
if ea <= es || iter >= maxit, break, end
end
root = xr;

0 comments on commit 8728f8c

Please sign in to comment.