Finding the Square root of 2
Here is some code I wrote to find a close appoximation of the square root of 2.
u1=2;
u2=1;
l1=1;
l2=1;
ub=u1/u2;
lb=l1/l2;
n=3;
d=2;
k=2;
for i=1:k
if (n^2/d^2)>(u1/u2)
u1=n;
u2=d;
n=2*n-1;
d=2*d;
elseif (n^2/d^2)<(l1/l2)
l1=n;
l2=d;
n=2*n;
d=2*d+1;
else
l1=n;
l2=d;
n=2*n;
d=2*d+1;
end
end

Leave a Reply