mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-06 18:53:57 -05:00
25 lines
699 B
Matlab
25 lines
699 B
Matlab
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
|