首页 > 编程语言 > 详细

Python用积分思想计算圆周率

时间:2019-12-08 23:22:59      阅读:105      评论:0      收藏:0      [点我收藏+]

[文本出自天外归云的博客园]

早上起来突然想求圆周率,1单位时圆的面积。

代码如下:

from math import pow, sqrt


def calc_circle_s_with(r, dy, x_slices):
    x_from_start_to_cc = sqrt(1 - pow(dy, 2))
    dx = x_from_start_to_cc / x_slices
    x_to_edge = 1 - x_from_start_to_cc
    quarter_circle_s = 0
    while x_to_edge < 1:
        rect_s = dy * dx
        quarter_circle_s += rect_s
        x_to_edge = x_to_edge + dx
        dy = sqrt(1 - pow((1 - x_to_edge), 2))
    circle_s = 4 * quarter_circle_s
    print(circle_s)


calc_circle_s_with(1, 0.0001, 10000000)

运行结果接近3.1415926,dy传的越小,x_slices传的越大,就越接近。

半径为:1

初始小矩形到圆周的距离:1 - x_from_start_to_cc

其中dy代表四分之一圆中初始小矩形的高度,x_slices代表小矩形的宽度:(1 - x_from_start_to_cc) / x_slices

四分之一圆的面积积分为:quarter_circle_s

Python用积分思想计算圆周率

原文:https://www.cnblogs.com/LanTianYou/p/12008228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!