diff --git a/bisect.m b/bisect.m deleted file mode 100644 index f4b17cd..0000000 --- a/bisect.m +++ /dev/null @@ -1,37 +0,0 @@ -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{:}); diff --git a/bisect_final_project.m~ b/bisect_final_project.m similarity index 100% rename from bisect_final_project.m~ rename to bisect_final_project.m diff --git a/least_squares.m b/least_squares.m new file mode 100644 index 0000000..9e5876a --- /dev/null +++ b/least_squares.m @@ -0,0 +1,11 @@ +function [a,fx,r2] = least_squares(Z,y); +%function that accepts a z-matrix and dependent variable and returns the vector of best-fit constants, a best-fit function, and the coefficient of determination, r2 +a = Z\y; +fx = Z*a; +e = y-fx; +st = std(y); +sr = std(e); +r2 = (st-sr)/st; + +end +