Geometric mean

Here is my riddle of the day for you. Explain why the following piece of Matlab code returns the following

>>blog1(10,3)
x=5.477226, y=5.477226
sqrt(x0y0)=5.477226

Here is the piece of code:

%lasting gemsfunction

function blog1(x0,y0)

x=x0;
y=y0;
for i = 1 : 100000
tmp=x;
x = (x+y)/2;
y = 2*tmp*y/(tmp+y);
end

fprintf(‘x=%f, y=%f\n’,x,y);
fprintf(‘sqrt(x0y0)=%f\n’,sqrt(x0*y0));

Also, generalize the above example and realize that the above gives you a numerical procedure to find the geometric mean of two numbers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: