mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2026-02-04 04:02:32 -05:00
Added new files for Numerical Analysis
This commit is contained in:
24
Newton.m
Normal file
24
Newton.m
Normal 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
|
||||
Reference in New Issue
Block a user