Added new files for Numerical Analysis

This commit is contained in:
2018-09-22 15:27:54 -04:00
parent 52997050f5
commit 3b669a2979
7 changed files with 197 additions and 0 deletions

35
Bisection.m Normal file
View File

@@ -0,0 +1,35 @@
function [xList,errorList] = 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;
maxItterations = 50;
errorValue = 1;
currentValue = 0.0;
%If the lower and upper bounds are mixed up swap them
if(double(subs(f,lowerValue)) > 0)
[lowerValue,upperValue] = swap(lowerValue, upperValue);
end
%Loop until the error is within bounds or the Maximum number of iterations is reached
while((abs(errorValue) > allowError) && (cnt < maxItterations))
currentValue = (lowerValue + upperValue)/ 2;
errorValue = double(subs(f,currentValue));
%Replace the correct value with the new value
%if error == 0 then the value has been found
if(errorValue < 0)
lowerValue = currentValue;
else
upperValue = currentValue;
end
xList(cnt) = currentValue;
errorList(cnt) = errorValue;
++cnt;
end
end