diff --git a/README.md b/README.md index c25bf87..c17a81a 100644 --- a/README.md +++ b/README.md @@ -56,3 +56,22 @@ end ``` ![nxn Interior Nodes, n = 10](./figures/figure2.png) + +### Part E + +```matlab +[pw_se,w] = SE_diff(T,P,n); +[root,fx,ea,iter] = bisect_final_project(@(T) SE_diff(T,0.001,25),.001,1,.1) +``` +```matlab +The same code was run five times, changing the value of n from 20 to 40 for intervals of 5. +``` + +| number of nodes | Tension (uN/um) | rel. error| +| --- | --- | --- | +| 3 | 0.0489 | n/a | +| 20 | 0.0599 | 18.4% | +| 25 | 0.0601 | 0.30% | +| 30 | 0.0602 | 0.17% | +| 35 | 0.0603 | 0.16% | +| 40 | 0.0603 | 0.00% | diff --git a/bisect_final_project.m~ b/bisect_final_project.m~ new file mode 100644 index 0000000..0fae3c9 --- /dev/null +++ b/bisect_final_project.m~ @@ -0,0 +1,37 @@ +function [root,fx,ea,iter]=bisect_final_project(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/bisect_final_project_script.m b/bisect_final_project_script.m new file mode 100644 index 0000000..a3e3cf0 --- /dev/null +++ b/bisect_final_project_script.m @@ -0,0 +1,2 @@ +[pw_se,w] = SE_diff(T,P,n); +[root,fx,ea,iter] = bisect_final_project(@(T) SE_diff(T,0.001,40),.001,1,.1) \ No newline at end of file