From e55e41b76a740fdca3976459af65fccc2bf53b06 Mon Sep 17 00:00:00 2001 From: Terrell Date: Tue, 3 Oct 2017 11:01:28 -0400 Subject: [PATCH] HW2 --- HW2/bisect.m | 37 +++++++++++++++++++++++++++++++++++++ HW2/falsepos.m | 39 +++++++++++++++++++++++++++++++++++++++ HW2/mod_secant.m | 30 ++++++++++++++++++++++++++++++ HW2/powerline_plot.m | 17 +++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 HW2/bisect.m create mode 100644 HW2/falsepos.m create mode 100644 HW2/mod_secant.m create mode 100644 HW2/powerline_plot.m diff --git a/HW2/bisect.m b/HW2/bisect.m new file mode 100644 index 0000000..47ee7ec --- /dev/null +++ b/HW2/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{:}); \ No newline at end of file diff --git a/HW2/falsepos.m b/HW2/falsepos.m new file mode 100644 index 0000000..2d010c8 --- /dev/null +++ b/HW2/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 - (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{:}); \ No newline at end of file diff --git a/HW2/mod_secant.m b/HW2/mod_secant.m new file mode 100644 index 0000000..b74f181 --- /dev/null +++ b/HW2/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; \ No newline at end of file diff --git a/HW2/powerline_plot.m b/HW2/powerline_plot.m new file mode 100644 index 0000000..9e20645 --- /dev/null +++ b/HW2/powerline_plot.m @@ -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') \ No newline at end of file