from
Calculate PI with Gauss-Legendre
by Daniel Serrano
Calculate pi to "c" decimal places using the Gauss-Legendre algorithm.
|
| digi=pigl(c,out)
|
function digi=pigl(c,out)
%PIGL Clculo del nmero PI
% Clculo del nmero PI empleando el algoritmo
% de Gauss-Legendre.
%
% PIGL(C,OUT) Clcula los C primeros decimales de PI
% (Ojo con pasarse ;-). OUT es el nombre del archivo
% de salida. Este debe ir entre comillas. Si no se
% especifica se considera 'pi.dat'.
%
% See also PI, VPA.
% Copyleft (c) 2001 by Daniel Serrano.
% $Revision: 1.1 $ $Date: 2001/12/23 23:24:10 $
%--------->clculo<---------
count=round(log2(c));% clcula el n de iteraciones necesarias
a=1;
b=1/sqrt(2);
t=1/4;
x=1;
for i=1:count
y=a;
a=(a+b)/2;
b=sqrt(b*y);
t=t-x*(y-a)^2;
x=2*x;
end
p=((a+b)^2)/(4*t);
%--------->salida<---------
if nargin==1
out='pi.dat';
end
fid1=fopen(out,'w','s'); %Prepara la salida del archivo
digi_sym=vpa(p,c+1); %se suma 1 porque tambin cuenta el 3 entero
digi=char(digi_sym);
fprintf(fid1,'PI= %s',digi); %formato de salida del nmero
fclose(fid1); %cierra el archivo
disp('Listo :-)')
|
|
Contact us at files@mathworks.com