This Pi Day, Calculate the Value of Pi for Yourself

综合技术 2018-03-14

It is once
againPi Day (March 14—which is like the first digits of pi: 3 and 14). Before getting into this year's celebration of pi, let me just summarize some of the most important things about this awesome number.

But today, I am going to calculate pi with a numerical integral. What does that even mean? Let me start with an example—how do you find the area of a half-circle?

The area of a circle is pi times the radius squared. This is half of a circle with a radius of 1 (no units) such that it would have an area of pi/2. If I find the area with some other method, I can just multiply this area by 2 and get pi. That's the plan.

But how do you find the area of some shape—or any shape for that matter? This is where calculus comes in handy. I can find the area of the half circle by adding up the area of a bunch of rectangles. It turns out that it's pretty easy to find the area of a rectangle. Let me just draw a few rectangles in that half-circle so you can see what I mean.

The area of each of these skinny rectangles can be found with the formula "base times height." A rectangle has a height of "y" and a base of "dx" where the dx is just some arbitrary length along the x-axis. I can find the actual value of the height because the top of the rectangle hits the circle where this height can be found from the equation of a circle.

Now I just need to add up all these rectangles—boom, that's the area of half a circle. I can write this as a sum of areas like this:

But wait! Isn't this a poor approximation to the actual area of a circle (half-circle)? Yes, that is indeed true—but it really depends on the width of these tiny area rectangles. In fact, if I take the limit as the width (dx) goes to zero then I will get the exact area. This is actually the definition of the integral in calculus—but I will save that for another day. Instead we will do a numerical calculation by simply adding up the area of a bunch of rectangles. You could of course do this by hand—but it might get boring. Instead, let's do this with a computer program. Yup.

Here is numerical calculation in python. You can go ahead and run the code by pressing the "play" button, but I will give some code comments below.

You can change the code if it makes you happy—here are a few things to consider.

  • This is a numerical calculation. That means the program only deals with numbers. Technically, the area should have units of m 2
    or something like that but not here. Only numbers.
  • For loops in python, it includes everything that is tab-indented as part of the loop. Once you dedent, it's no longer in a loop.
  • Line 18 should look weird because it is. If you consider this to be an algebraic equation, the A should cancel since it is on both sides of the equation—but this is not an equation. In python (and most other languages), the "=" means "make equal to". This line takes the old value of A, adds the new stuff and then makes it the new value of A.

This initial calculation has a dx of 0.1. That means there will be just 20 rectangles to add up and get the area of the half-circle. With this, I get an approximate pi value of 3.10452—which is clearly not exact pi. Of course I can make a better estimate by making smaller width rectangles. You should try this by changing the code above (hint: change the value for dx). However, since I can't let this go here is a plot of the value of pi for different step sizes.

Perhaps that's not the best plot—but it's good enough for now. If you want to check out the code for this plot, here you go
. But in the end, the value does approach the expected value of pi. This method might not get you one million digits of pi, but maybe you at least can learn something about integration.


责编内容by:Wired (源链)。感谢您的支持!


从 Node 到 Go:一个粗略的比较 在 XO 公司,我们最初使用 Node 和 Ruby 构建相互连接的服务系统。我们享受 Node 带来的明显性能优势,以及可以访问已有的大型软件包仓库。我们也可...
Should you learn R or Python for data science? One of the most common questions I get asked is, "Should I learn R or Python?"....
A New Internet — WHY NOT? PubNub empowers you to build immersiv...
手把手教你用Python写一个在线网站,这真的很装逼!... pip install Flask 在做我们的工作之前先来了解一下flask框架是怎样的一个运行机制吧: 首先创建一个flaskrun.py文...
两数相加 leetcode Python 给定两个 非空 链表来表示两个非负整数。位数按照 逆序 方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。 你可以...