Skip to content

Commit

Permalink
HW2
Browse files Browse the repository at this point in the history
  • Loading branch information
Terrell committed Oct 3, 2017
1 parent aa9c887 commit e55e41b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
37 changes: 37 additions & 0 deletions HW2/bisect.m
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{:});
39 changes: 39 additions & 0 deletions HW2/falsepos.m
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 - (func(xu)*(xl-xu))/(func(xl)-func(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{:});
30 changes: 30 additions & 0 deletions HW2/mod_secant.m
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;
17 changes: 17 additions & 0 deletions HW2/powerline_plot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cat_cable = @(T) T/10.*cosh(10./T*30)+30-T/10-35;
[root,fx,ea,iter] = falsepos(cat_cable,100,1000,0.00001,10000);
[root1,fx1,ea1,iter1] = bisect(cat_cable,100,1000,0.00001,10000);
[root2,ea2,iter2] = mod_secant(cat_cable,100,1000,0.00001,10000);

%define T
T = root2;

%plotting the shape of the powerline
x = -10:0.1:50;
y = T/10.*cosh(10./T*x)+30-T/10;
%setDefaults
plot(x,y)
title('Final Powerline Shape')
xlabel('distance (m)')
ylabel('height (m)')
print('figure01','-dpng')

0 comments on commit e55e41b

Please sign in to comment.