You are on page 1of 1

function HalleysMethod

%Halleys Method - this is a vairation of Newton-Raphson method, useful as


%is (computationally) quicker.
%Halley's Method invloves: g(x) = x - f(x)/f'(x)*[1 - f(x)f''(x)/(2(f'(x))^2)
%f(x) = x^2 - 6.

i = 1;
p0 = 1; %initial conditions
N = 100; %maximum number of iterations
error = 0.00000001; %precision required

syms 'x'
f(x) = x^2 - 6; %the function we are root finding.
dx = diff(f); %first and second derivatives of f(x)
ddx = diff(dx);

while i <= N
p = p0 - (f(p0)/dx(p0))*(1 - (f(p0)*ddx(p0)/dx(p0)^2))^(-1);
%Implementation og Halleys Methof (i.e. g(x)).

if (abs(p - p0)/abs(p)) < error %stopping criterion when


difference between iterations is below tolerance
fprintf('Solution is %f \n', double(p))
return
end

i = i + 1;
p0 = p; %update p0
end

fprintf('Solution did not coverge within %d iterations at a required precision of


%d \n', N, error) %error for non-convergence within N iterations

end

You might also like