Code to estimate pi

8 views (last 30 days)
John Jamison
John Jamison on 1 Feb 2017
Commented: Rena Berman on 1 Feb 2017
see above image.
Please help for both calculations.
thanks
  2 Comments
Image Analyst
Image Analyst on 1 Feb 2017
John, somehow the formula image got deleted. Can you put it back?
Rena Berman
Rena Berman on 1 Feb 2017
(Answers dev) Restored edit

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 1 Feb 2017
Use the MATLAB vector capabilities:
N = 1E+8; % Number Of Terms
pie1 = sqrt(sum(6./[1:N].^2))
pie2 = sqrt(8 + sum(16./((2*[1:N] - 1).^2 .* (2*[1:N] + 1).^2)))
pie1 =
3.1415926440655
pie2 =
3.14159265358975

More Answers (1)

Image Analyst
Image Analyst on 1 Feb 2017
Here's code that gets to pi even faster. Dubbed the world's ugliest formula by mathematicians, it's so accurate after one iteration that MATLAB can't even detect a difference after just one term! Very interesting article if you want to read it. If someone can show me how to get the true difference, which is less than 1E-16, I'd appreciate it. By the way, most of the mathematicians agreed on which equations were beautiful and which were ugly, with Euler's identity, 1+eiπ=0, consistently rated the most attractive equation in the lot.
% 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 it 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')

Tags

Community Treasure Hunt

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

Start Hunting!