Paste: rex's code
Author: | rex |
Mode: | ml |
Date: | Mon, 1 Feb 2010 14:56:35 |
Plain Text |
% Written by Rex Ford
% Tell's if Newton's Method converges to root 1, 2 or 3.
% root 1: -1
% 2: .5+i*3^0.5
% 3: -.5+i*3^0.5
function sol = newton(guess)
tol=10^-12; %assignment's recommended tolerance
f=@(x)x^3+1; %our function
f_prime=@(x)3*x^2; %hand-computed derivative
max_its=15; %in case there isn't convergence
i=0;
while i<max_its && %abs
guess=guess-(f(guess)/f_prime(guess));
i=i+1;
end
sol=guess;
% file 2:
function which=which_root(x)
tol=10^-12;
is_near=@(y)abs(real(y-x))<tol && abs(imag(y-x))<tol;
if is_near(-1)==true, which=1;
else if is_near((1/2)+i*sqrt(3)/2)==true, which=2;
else which=3;
end
end
end
New Annotation