Added check for the correct number of variables and added a few comments

This commit is contained in:
2018-09-22 16:19:44 -04:00
parent 3b669a2979
commit 3f00730c9f
6 changed files with 60 additions and 13 deletions

View File

@@ -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 %Uses the bisection method to find the possible answers to the root of the function f
% %
pkg load symbolic; %Check that the number of input variables is correct
warning('off','OctSymPy:sym:rationalapprox'); if(nargin ~= 4)
%Setting necesary values for the function error('That is not the correct number of arguments')
cnt = 1; end
%A few necesary things before we begin
pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox');
%Constant Variables
maxItterations = 50; maxItterations = 50;
%Variables
cnt = 1;
errorValue = 1; errorValue = 1;
currentValue = 0.0; currentValue = 0.0;

View File

@@ -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 %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; pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox'); warning('off','OctSymPy:sym:rationalapprox');
%Constant variables
maxIt = 50; maxIt = 50;
%Variables
cnt = 2; cnt = 2;
q0 = double(subs(f,p0)); q0 = double(subs(f,p0));
q1 = double(subs(f,p1)); q1 = double(subs(f,p1));
p = 0; p = 0;
q = 0; q = 0;
currentError = errorAllowed + 1; 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)) while((cnt <= maxIt) && (currentError >= errorAllowed))
p = p1 - (q1 * (p1 - p0))/(q1 - q0); p = p1 - (q1 * (p1 - p0))/(q1 - q0);
currentError = abs(p - p1); currentError = abs(p - p1);

View File

@@ -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 %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; pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox'); warning('off','OctSymPy:sym:rationalapprox');
%Constant variables
maxIt = 50;
%Variables
h1 = p1 - p0; h1 = p1 - p0;
h2 = p2 - p1; h2 = p2 - p1;
s1 = (double(subs(f,p1)) - double(subs(f,p0))) / h1; s1 = (double(subs(f,p1)) - double(subs(f,p0))) / h1;
s2 = (double(subs(f,p2)) - double(subs(f,p2))) / h2; s2 = (double(subs(f,p2)) - double(subs(f,p2))) / h2;
d = (s2 - s1) / (h2 + h1); d = (s2 - s1) / (h2 + h1);
cnt = 2; 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) while(cnt < maxIt)
b = s2 + h2 * d; b = s2 + h2 * d;
D = (b^2 - (4 * double(subs(f,p2)) * d))^(1/2); 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; p = p2 + h;
xList(end+1) = p; xList(end+1) = p;
functionValueList(end+1) = double(subs(f,p)); functionValueList(end+1) = double(subs(f,p));
%Exit the function if you get an answer within error
if(abs(h) < errorAllowed) if(abs(h) < errorAllowed)
return; return;
else else

View File

@@ -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 %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; pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox'); warning('off','OctSymPy:sym:rationalapprox');
%Constant variables
maxIt = 50;
fp = diff(f);
fpp = diff(fp);
%Variables
oldAnswer = startingValue; oldAnswer = startingValue;
newAnswer = 0; newAnswer = 0;
currentError = errorAllowed + 1; currentError = errorAllowed + 1;
cnt = 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)) 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))))); 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); currentError = abs(newAnswer - oldAnswer);

View File

@@ -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 %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; pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox'); warning('off','OctSymPy:sym:rationalapprox');
%Constant variables
maxIt = 50; maxIt = 50;
fp = diff(f); fp = diff(f);
%Variables
oldAnswer = startingValue; oldAnswer = startingValue;
newAnswer = 0; newAnswer = 0;
cnt = 1; cnt = 1;

View File

@@ -4,16 +4,23 @@ function [xList, errorList] = Secant(f, p0, p1, errorAllowed)
%This function find the root of a function using the Secant Method %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; pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox'); warning('off','OctSymPy:sym:rationalapprox');
%Constant variables
maxIt = 50; maxIt = 50;
%Variables
cnt = 2; cnt = 2;
q0 = double(subs(f,p0)); q0 = double(subs(f,p0));
q1 = double(subs(f,p1)); q1 = double(subs(f,p1));
currentError = errorAllowed + 1; currentError = errorAllowed + 1;
p = 0; p = 0;
%Loop until you find an answer within error or you reach the maximum number of itterations
while((cnt <= maxIt) && (currentError >= errorAllowed)) while((cnt <= maxIt) && (currentError >= errorAllowed))
p = p1 - (q1 * (p1 - p0))/(q1 - q0); p = p1 - (q1 * (p1 - p0))/(q1 - q0);
currentError = abs(p - p1); currentError = abs(p - p1);