# Thread: Halleys metyhod matlab help

1. ## Halleys metyhod matlab help

This is my code, to find the f(x)=0 positive root, using halleys method

2 m files

1. function file
function [y,dydx,ddyddxx]=h(x)
% The function is expressed, and the 1st and 2nd derivatives are noted
y=exp(cos(x)^2)-x^2;
dydx=-sin(2*x)*exp(cos(x)^2)-2*x;
ddyddxx=2*exp(cos(x)^2)-4*cos(x)^4*exp(cos(x)^2)-2;

end

2. Halleys method
clear all
% clears all the stored variable
format long
%formats the outputs to 10 significant figures
x(1) = 0;
%left bound
x(2) = 1000;
%right bound
eps = 1e-10;
count = 2;
while abs(x(count-1)-x(count)) > eps
% calculating the derivatives
[y,dydx,ddyddxx]= h(x(count));
x(count+1) = x(count) - ((y*dydx)/(((dydx)^2)-(ddyddxx*y)/2));
fprintf('The root is approximately %1.10f\n',x(count+1));
count = count + 1;
end
fprintf('The solution of exp(cos(x)^2)-x^2 is approximately %1.10f\nThis algorithm took %u iterations to complete\n',x(count),count-2)

OK my problem is my initial conditions, as all i have done is put halleys method into newtons method. I need to "using an initial condition value of x1=1000, apply this iterative procedure to f(x) untill |f(xn)|<10^-10 or the number of iterations is 1000, also my program needs to oputput the number of iterations and the root to 10 signif figures. my code has found x to 10 signifs not f(xn) apparently i need to make it like a controlled bisection, and have an || and && statements somewhere... i litterally have no idea how to do this... please can someone help me?

2. the conrolled bisection i have give is:

clear all
% clears all the stored variable
format long
xa = pi/2;
xb = pi;
xc(1) = pi/2;
xc(2) = pi;
eps = 1e-10;
delta = 1e-10;
count = 2;
M = 100;
while ((abs(xc(count)-xc(count-1)) > eps) || (abs(f(xc(count)) - f(xc(count-1)))) > delta) && (count < M+2)
count = count + 1;
% claculating the midpoint
xc(count) = (xa+xb)/2;
% determining whether the midpoint lies between [xa,xc] ot [xc,xb].
% Then changing the values of xa and xb according.
if f(xa)*f(xc(count)) < 0;
xb = xc(count);
else
xa = xc(count);
end
fprintf('The root lies between %1.10f and %1.10f\n',xa,xb);
end
if (count == M+2)
fprintf('This series did not \n');
else
fprintf('This series converged in %u steps to %f\n',count-2, xc(count));
end

I dont know what to change in my code... I am very very new to matlab, im not a good coder, i just try to work from example...

3. Assuming your code for Halley's method is correct:

Code:
clear all       % clears all the stored variable
format long  %formats the outputs to 10 significant figures

x=zeros(1,1000);
x(1) = 1;
[y,dydx,ddyddxx]= h(x(1));

eps = 1e-10;
count = 1;

while (abs(y) > eps) && (count<=1000)

% calculating the derivatives
[y,dydx,ddyddxx]= h(x(count));

x(count+1) = x(count) - ((y*dydx)/(((dydx)^2)-(ddyddxx*y)/2));

fprintf('The root is approximately %1.10f\n',x(count+1));
count = count + 1;

end

fprintf('The solution of exp(cos(x)^2)-x^2 is approximately %1.10f\nThis algorithm took %u iterations to complete\n',x(count),count-2)
Note the initial guess should be near the root.

CB

4. Is it possibly like this? if i use M is the max values, it donest go past 1000 iterarations, if i take out the first part of the while loop. Idk im confused

clear all
% clears all the stored variable
format long
%formats the outputs to 10 significant figures
x(2) = 1000;
eps = 1e-10;
count = 2;
M = 1000;
%Maximum nuber of iterations
delta = 1e-10;
while abs(x(count-1)-x(count)) > eps || (abs(h(x(count)) - h(x(count-1))) > delta) && count<M;
% calculating the derivatives
[y,dydx,ddyddxx]= h(x(count));
x(count+1) = x(count) - ((y*dydx)/(((dydx)^2)-(ddyddxx*y)/2));
fprintf('The root is approximately %1.10f\n',x(count+1));
count = count + 1;
end
fprintf('The solution of exp(cos(x)^2)-x^2 is approximately %1.10f\nThis algorithm took %u iterations to complete\n',x(count),count-2)

5. ahh i see thanks heaps