MATLAB: Can’t figure out what I am doing wrong. Looking to find square root using the equation given x=(x+x/a)/2. I also feel like I am not making use of the approximation errors ea and es.

initailizing variableMATLAB

Having trouble determining what I am doing wrong. I don't feel like I am initiallizing x incorrectly, because that is what I am trying to solve for. Not sure if I am making use of ea and es error approximations as well.
code is as follows:
clear
a=input('Enter Number'); %User input number
x=sqrt(a);
ea=100; %approximate relative error 100 percent
es=0.01; %stopping point for error must be less than
count=1; %iteration beginning
countmax=1000; %max number of iterations allowed
if 0<a %must be positive number
while (count<countmax) && (es<ea) %How is es/ea even a factor???
x=(x+(a/x))/2; %equation to solve for square root
disp(x); %show answer
x_old=x;
ea=((x-x_old)/x)*100;
count=count+1;
end
else
msgbox("Enter Positive Number") %number entered not positive
end

Best Answer

You need to take absolute values,
ea=abs((x-x_old)/x)*100;
Related Question