mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-06 18:53:57 -05:00
Added new files for Numerical Analysis
This commit is contained in:
35
Bisection.m
Normal file
35
Bisection.m
Normal 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
|
||||
Reference in New Issue
Block a user