From 3f00730c9f5389eddfd5f5905a2bcd9d26e70382 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sat, 22 Sep 2018 16:19:44 -0400 Subject: [PATCH] Added check for the correct number of variables and added a few comments --- Bisection.m | 17 ++++++++++++----- FalsePosition.m | 9 +++++++++ Mullers.m | 13 +++++++++++-- NewNewton.m | 15 +++++++++++---- Newton.m | 10 +++++++++- Secant.m | 9 ++++++++- 6 files changed, 60 insertions(+), 13 deletions(-) diff --git a/Bisection.m b/Bisection.m index 2dc1500..9292bd4 100644 --- a/Bisection.m +++ b/Bisection.m @@ -1,13 +1,20 @@ -function [xList,errorList] = Bisection (f, lowerValue, upperValue, allowError) +function [xList,errorList] = Bisection(f, lowerValue, upperValue, allowError) % +%Bisection(f, lowerValue, upperValue, allowError) %Uses the bisection method to find the possible answers to the root of the function f % - pkg load symbolic; - warning('off','OctSymPy:sym:rationalapprox'); - %Setting necesary values for the function - cnt = 1; + %Check that the number of input variables is correct + if(nargin ~= 4) + error('That is not the correct number of arguments') + end + %A few necesary things before we begin + pkg load symbolic; + warning('off','OctSymPy:sym:rationalapprox'); + %Constant Variables maxItterations = 50; + %Variables + cnt = 1; errorValue = 1; currentValue = 0.0; diff --git a/FalsePosition.m b/FalsePosition.m index 1e20e19..24be32c 100644 --- a/FalsePosition.m +++ b/FalsePosition.m @@ -4,15 +4,24 @@ function [xList, errorList] = FalsePosition(f, p0, p1, errorAllowed) %This function finds the root of a function using the method of False Position % + %Make sure the number of arguments is correct + if(narginchk(4, 4)) + error('That is an incorrect number of arguments') + end + %A few necesary before we begin pkg load symbolic; warning('off','OctSymPy:sym:rationalapprox'); + %Constant variables maxIt = 50; + %Variables cnt = 2; q0 = double(subs(f,p0)); q1 = double(subs(f,p1)); p = 0; q = 0; currentError = errorAllowed + 1; + + %Loop until you find a value within the error or you reach the maximum number of itterations while((cnt <= maxIt) && (currentError >= errorAllowed)) p = p1 - (q1 * (p1 - p0))/(q1 - q0); currentError = abs(p - p1); diff --git a/Mullers.m b/Mullers.m index a4b0e07..b758c95 100644 --- a/Mullers.m +++ b/Mullers.m @@ -4,17 +4,25 @@ function [xList, functionValueList] = Mullers(f, p0, p1, p2, errorAllowed) %This function finds the root of a function using Muller's Method % + %Make sure the number of arguments is correct + if(narginchk(5,5)) + error('That is an incorrect number of arguments') + end + %A few necesary things before we get started pkg load symbolic; warning('off','OctSymPy:sym:rationalapprox'); - + %Constant variables + maxIt = 50; + %Variables h1 = p1 - p0; h2 = p2 - p1; s1 = (double(subs(f,p1)) - double(subs(f,p0))) / h1; s2 = (double(subs(f,p2)) - double(subs(f,p2))) / h2; d = (s2 - s1) / (h2 + h1); cnt = 2; - maxIt = 50; + %Loop until you reach the maximum number of itterations + %If you find an answer within error it will return while(cnt < maxIt) b = s2 + h2 * d; D = (b^2 - (4 * double(subs(f,p2)) * d))^(1/2); @@ -27,6 +35,7 @@ function [xList, functionValueList] = Mullers(f, p0, p1, p2, errorAllowed) p = p2 + h; xList(end+1) = p; functionValueList(end+1) = double(subs(f,p)); + %Exit the function if you get an answer within error if(abs(h) < errorAllowed) return; else diff --git a/NewNewton.m b/NewNewton.m index e239746..8e07c1d 100644 --- a/NewNewton.m +++ b/NewNewton.m @@ -4,17 +4,24 @@ function [xList, errorList] = NewNewton (f, startingValue, errorAllowed) %This function computes the root of a function using the modified Newton's method % + %Make sure the number of arguments is correct + if(narginchk(3,3)) + error('That is an incorrect number of arguments') + end + %A few necesary things before we get started pkg load symbolic; warning('off','OctSymPy:sym:rationalapprox'); + %Constant variables + maxIt = 50; + fp = diff(f); + fpp = diff(fp); + %Variables oldAnswer = startingValue; newAnswer = 0; currentError = errorAllowed + 1; cnt = 1; - maxIt = 50; - fp = diff(f); - fpp = diff(fp); - + %Loop until you find an answer within error or you reach the maximum number of itterations while((currentError >= errorAllowed) && (cnt < maxIt)) newAnswer = oldAnswer - ((double(subs(f,oldAnswer)) * double(subs(fp,oldAnswer)))/(double(subs(fp,oldAnswer))^2 - (double(subs(f,oldAnswer)) * double(subs(fpp,oldAnswer))))); currentError = abs(newAnswer - oldAnswer); diff --git a/Newton.m b/Newton.m index 1d87775..65e0174 100644 --- a/Newton.m +++ b/Newton.m @@ -1,12 +1,20 @@ -function [xList,errorList] = Newton (f, startingValue, errorAllow) +function [xList,errorList] = Newton(f, startingValue, errorAllow) % +%Newton(f, startingValue, errorAllow) %This function uses Newtons method to find a solution to the root of f % + %Check that the number of arguments is correct + if(nargin ~= 3) + error('That is the wrong number of arguments') + end + %Things that are necessary to begin pkg load symbolic; warning('off','OctSymPy:sym:rationalapprox'); + %Constant variables maxIt = 50; fp = diff(f); + %Variables oldAnswer = startingValue; newAnswer = 0; cnt = 1; diff --git a/Secant.m b/Secant.m index 5662b4c..0a5bd10 100644 --- a/Secant.m +++ b/Secant.m @@ -4,16 +4,23 @@ function [xList, errorList] = Secant(f, p0, p1, errorAllowed) %This function find the root of a function using the Secant Method % + %Make sure the number of arguments is correct + if(narginchk(4,4)) + error('That is an incorrect number of arguments') + end + %A few necesary things before we get started pkg load symbolic; warning('off','OctSymPy:sym:rationalapprox'); + %Constant variables maxIt = 50; + %Variables cnt = 2; q0 = double(subs(f,p0)); q1 = double(subs(f,p1)); currentError = errorAllowed + 1; p = 0; - + %Loop until you find an answer within error or you reach the maximum number of itterations while((cnt <= maxIt) && (currentError >= errorAllowed)) p = p1 - (q1 * (p1 - p0))/(q1 - q0); currentError = abs(p - p1);