www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ApproximateValueofExample.m
%% Approximate Value of π % Approximate the value of $\pi$ using a rational representation of the % quantity |pi|. %% % The mathematical quantity $\pi$ is not a rational number, but the % quantity |pi| that approximates it _is_ a rational number since all % floating-point numbers are rational. % % Find the rational representation of |pi|. format rat pi %% % The resulting expression is a character vector. You also can use % |rats(pi)| to get the same answer. %% % Use |rat| to see the continued fractional expansion of |pi|. R = rat(pi) %% % The result is an approximation by continued fractional expansion. If you % consider the first two terms of the expansion, you get the approximation % $3 + \frac{1}{7} = \frac{22}{7}$, which only agrees with |pi| to 2 % decimals. %% % However, if you consider all three terms printed by |rat|, you can % recover the value |355/113|, which agrees with |pi| to 6 decimals. % % $$3 + \frac{1}{7 + \frac{1}{16}} = \frac{355}{113}$$ % %% % Specify a tolerance for additional accuracy in the approximation. R = rat(pi,1e-7) %% % The resulting approximation, |104348/33215|, agrees with |pi| to 9 % decimals.