Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
adding files
adding files
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{:}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{:}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{:}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |