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

24
Newton.m Normal file
View File

@@ -0,0 +1,24 @@
function [xList,errorList] = Newton (f, startingValue, errorAllow)
%
%This function uses Newtons method to find a solution to the root of f
%
pkg load symbolic;
warning('off','OctSymPy:sym:rationalapprox');
maxIt = 50;
fp = diff(f);
oldAnswer = startingValue;
newAnswer = 0;
cnt = 1;
currentError = errorAllow + 1;
%Loop until the error becomes small enough or the maximum number of itterations is met
while((cnt < maxIt) && (currentError > errorAllow))
newAnswer = oldAnswer - (double(subs(f,oldAnswer))/double(subs(fp,oldAnswer)));
currentError = abs(newAnswer - oldAnswer);
xList(end+1) = newAnswer;
errorList(end+1) = currentError;
oldAnswer = newAnswer;
++cnt;
end
end