MATLAB: Problem executing while loop

halley'sloopwhile

im writing a script to solve the square root of a number using halley's method
my equations: y=(1/a)*x^2
n = (x/8)*(15-(y)*(10-3*(y)))
then n becomes the new x and the equation repeats
my problem is its not looping it runs through once and then stops
clc
clear
a = input('number ');
x = input('guess ');
tol = 0.0000001;
while (abs(n-x)<=tol)
y=(1/a)*x^2;
n = ((x/8)*(15-(y)*(10-3*(y))));
x=n;
end
disp(n)
i want the loop to stop when the number is accurate to 6 decimal places. what am i doing wrong
very new to ML

Best Answer

insert before while n=x;
clc
clear
a = input('number ');
x = input('guess ');
tol = 0.0000001;
n=x
while (abs(n-x)<=tol)
y=(1/a)*x^2;
n = ((x/8)*(15-(y)*(10-3*(y))));
x=n;
end
disp(n)
Related Question