Code to estimate pi

4 次查看(过去 30 天)
see above image.
Please help for both calculations.
thanks
  2 个评论
Image Analyst
Image Analyst 2017-2-1
John, somehow the formula image got deleted. Can you put it back?
Rena Berman
Rena Berman 2017-2-1
(Answers dev) Restored edit

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2017-2-1
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

更多回答(1 个)

Image Analyst
Image Analyst 2017-2-1
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')

类别

Help CenterFile Exchange 中查找有关 Simulation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by