User:Egm6322.s12.team1.zheng.zx/R12

Problem 4: Write a matlab program to compute the convergents for $$\sqrt 2$$
 Solved without assistance 

Given:

 * {| style="width:100%" border="0" align="left"

\displaystyle \sqrt 2=1+\frac{1}{2+\frac{1}{2+\frac{1}{2+\frac{1}{2+\frac{1}{\ddots}}}}} $$
 * }
 * }
 * }
 * {| style="width:100%" border="0" align="left"

\displaystyle C_0:=1 $$
 * }
 * }
 * }
 * {| style="width:100%" border="0" align="left"

\displaystyle C_1:=1+\frac{1}{2}=\frac{3}{2} $$
 * }
 * }
 * }
 * {| style="width:100%" border="0" align="left"

\displaystyle C_2:=1+\frac{1}{2+\frac{1}{2}}=1+\frac{2}{5}=\frac{7}{5} $$
 * }
 * }
 * }
 * {| style="width:100%" border="0" align="left"

\displaystyle C_3:=1+\frac{1}{2+\frac{1}{2+\frac{1}{2}}}=1+\frac{1}{2+\frac{2}{5}}=1+\frac{5}{12}=\frac{17}{12} $$
 * }
 * }
 * }

Find:
Write a matlab program to compute the convergents $$C_i$$for i=0,1,...,10,and plot these convergents together with the line $$\sqrt 2$$to visualize the convergence
 * {| style="width:100%" border="0" align="left"

\displaystyle \lim_{i \to \infty}C_i=\sqrt 2 $$
 * }
 * }
 * }

Solution:
clear

n=10;

f=sqrt(2); f=sym(f); f=char(f); fplot(f,[-2 2],'r','o'); legend('sqrt(2)'); hold on

disp(['C' char(48) '=']) disp(1)

f=1; f=sym(f); f=char(f); fplot(f,[-2 2]);

for x=1:n; s=0; for i=1:x; s=1/(2+s); end s=1+s; s=vpa(s); disp(['C' char(sym(x)) '=']) disp(s)

f=s; f=sym(f); f=char(f); fplot(f,[-2 2]);

end axis([-2 2 1.39 1.51])

The results are as follows:









From the results above, we can see that with the increasing of fraction terms, the value will be convergent to the actual value: $$\sqrt 2=1.4142135623730950488016887242097$$.