function [Xs] = Squareone(p)
%SquareRoot Finds square root of a function
% Uses Newton's method to find the quare root of a number for upto 20
% iterations
% Returns the root
format long;
F = @(x) x^2 - p;
syms Fd(x) x;
Fd(x) = @(x) 2*x;
Tolerance = 1e-6;
xEST = p;
i = 0;
if p >= 0
if F(xEST) == 0
fprintf('True solution: %0.6f\n', xEST);
Xs = xEST;
else
if F(xEST) <= Tolerance
fprintf('Approximate solution: %0.6f\n', xEST);
Xs = xEST;
else
while i <= 20
xEST = xEST - (F(xEST) / Fd(xEST));
if F(xEST) == 0
fprintf('True solution: %0.6f\n', xEST);
Xs = xEST;
break;
else
if F(xEST) <= Tolerance
fprintf('Approximate solution: %0.6f', xEST);
Xs = xEST;
break;
else
i = i + 1;
end
end
end
if i > 20
fprintf('Max iteration reached(20)');
end
end
end
else
fprintf('Error: Input is negative!\n');
end
end