Approximation of pi is "too precise" .

3 次查看(过去 30 天)
Thomas
Thomas 2021-6-16
评论: Thomas 2021-6-20
The function below should approximate pi adding about 2 digits of precision for increasing n. Why is piApprox2(3) exactly = pi and not just close to pi? Is their any built in knowledge Matlab uses, that I am not aware of?
When extracting 10 Mio digits of the result P (via Pc = char(vpa(P,10000000))) and looking at the last 10 characters of Pc, they are exactly the same as when looking at these digits in pi.
function P = piApprox2(n)
% approximates pi using n approximation steps of formula (7) from:
% https://www.davidhbailey.com/dhbpapers/pi-formulas.pdf
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
for k = 0:n
s1 = s1 + (-1)^k / ((2*k+1)*57^(2*k+1));
s2 = s2 + (-1)^k / ((2*k+1)*239^(2*k+1));
s3 = s3 + (-1)^k / ((2*k+1)*682^(2*k+1));
s4 = s4 + (-1)^k / ((2*k+1)*12943^(2*k+1));
end
P = 4 * ( 44*s1 + 7*s2 - 12*s3 + 24*s4);
end
  2 个评论
Walter Roberson
Walter Roberson 2021-6-16
vpa(P,10000000)
You should be using
vpa(sym(P,'f'),10000000)
because the default conversion of double to symbolic involves approximating.
Thomas
Thomas 2021-6-20
Thank you very much - that explains it!

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2021-6-16
For what it's worth, when I tried a pi estimation series, it was so accurate that after 2 terms, MATLAB couldn't tell the difference from pi. So I had a similar problem.
% Computes Ramanujan's formula for pi.
% http://www.scientificamerican.com/article/equations-are-art-inside-a-mathematicians-brain/
% On the bottom of the heap of beautiful formulas, mathematicians consistently rated
% Srinivasa Ramanujan's infinite series for estimating pi as the most ugly.
% Problem: convergence is so fast that double precision is not
% precise enough to show the convergence curve.
clc; % Clear the command window.
close all; % Close all figures.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Compute the sum for a bunch of different number of terms
% in the series to see the shape of the error curve
% as it converges towards the true value of pi.
for kMax = 1 : 10
thisSum = 0;
for k = 0 : kMax
numerator = factorial(4 * k) * (1103 + 26390*k);
denominator = factorial(k)^4 * 396^(4*k);
thisTerm = numerator / denominator;
thisSum = thisSum + thisTerm;
end
theConstant = 2 * sqrt(2) / 9801;
oneOverPi = theConstant * thisSum;
estimatedPi(kMax) = 1 / oneOverPi
theError(kMax) = estimatedPi(kMax) - pi
end
% Plot the value of estimated pi.
subplot(2, 1, 1);
plot(estimatedPi, 'b*-', 'LineWidth', 2);
grid on;
title('Estimated \pi', 'FontSize', fontSize);
xlabel('Number of Terms in the Sum', 'FontSize', fontSize);
% Plot the value of the error (difference between true pi and estimated pi).
subplot(2, 1, 2);
plot(theError, 'b*-', 'LineWidth', 2);
grid on;
title('Error', 'FontSize', fontSize);
xlabel('Number of Terms in the Sum', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Estimation of pi', 'NumberTitle', 'Off')
I don't have the symbolic toolbox so I guess I'm out of luck. Any way to see convergence without that toolbox?

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by