1 说明
=====
1.1 欧拉数:就是自然常数,e。
1.2 e是“指数”(exponential)的首字母,也是欧拉名字的首字母。
1.3 三大数学常数:自然常数e、圆周率π和虚数单位i。
1.4 第一次把e看成常数的是雅各布•伯努利,他开始尝试计算lim(1+1/n) n 的值;1727年欧拉首次用小写字母“e”表示这常数。
2 python实现自然常数e
==================
2.1 图1
2.2 图2
3 python的e求近似值==求阶乘法
=========================
3.1 代码1:
<code>#计算e的值(精度为10**-6,可调节,最大50,即小数点后50位) sum,tmp = 1,1 for i in range(1,100): #100,为取值最小17,代码2有讲解 tmp*=i sum += 1/tmp print("e的近似值(精度为10**-6)为%.6f"%sum) print("e的近似值(精度为10**-9)为%.9f"%sum) print("e的近似值(精度为10**-29)为%.29f"%sum) print("e的近似值(精度为10**-69)为%.69f"%sum)/<code>
3.2 代码1结果:
<code>#e的近似值(精度为10**-6)为2.718282 #e的近似值(精度为10**-9)为2.718281828 #e的近似值(精度为10**-29)为2.71828182845904553488480814849 #e的近似值(精度为10**-69)为2.718281828459045534884808148490265011787414550781250000000000000000000/<code>
3.3 代码2:
<code>n = int(input()) sum = 0 def jc(a): #求阶乘 num = 1 for i in range(a + 1): if i != 0: num *= i return num for i in range(n + 1): sum += 1 / jc(i) print('{:.50f}'.format(sum))/<code>
3.4 结果
<code>n为输入非负整数 n(≤1000) 17 #为最小n,大于17的n值基本不变 2.71828182845904553488480814849026501178741455078125 16 #<17的数值改变 2.71828182845904287034954904811456799507141113281250/<code>
4 图片1的代码
<code>import matplotlib.pyplot as plt #级数 1+1/1!+1/2!+⋯+1/n!+⋯ 来近似计算 x = [] y = [] #计算e的值(精度为10**-50) edata,tmp = 1,1 for i in range(1,50): x.append(i) tmp*=i y.append(edata) edata += 1/tmp plt.plot(x, y) plt.title('e自然常数(欧拉数)(小数点后50位)≈:\n{:.50f}'.format(edata)) plt.show()/<code>
5 图片2的说明
===========
5.1 源代码来自
<code>https://www.jianshu.com/p/107d97423e6b #2019-3-14 张子豪 同济大学 感谢原作者 #源代码有报错,修改bug,并对代码进行注释、修改、整理/<code>
5.2 源代码bug
<code>No handles with labels found to put in legend. /home/xgj/Desktop/python-e/2.py:35: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. plt.subplot(121) ...... (省略)/<code>
5.3 解决bug思路
<code>#参考文章 https://www.it1352.com/1605941.html /<code>
6 图片2代码
=========
6.1 第1步:导入模块
<code>import random import matplotlib.pyplot as plt import numpy as np/<code>
6.2 第2步:初始化参数设置
<code>#plt背景颜色定义,一定放在前面 plt.style.use('dark_background') DARTS = 300 #可自定义,源代码1024*1024,太大了 counts = 0 # 落在曲线下方的点数 e = 0 # e的计算值 xs = [0,0] ys = [0,0] #初始化定义画布,一行2个子图ax fig, (ax1, ax2) = plt.subplots(1, 2)/<code>
6.3 第3步:子图1=ax1
<code># 开始画左边的图:撒点估计曲线下方的面积 x = np.arange(0.5,2.5,0.001) ax1.set_ylim(0,1.25) # y轴坐标范围 ax1.set_xlabel('x') # x轴标签 ax1.set_ylabel('y') # y轴标签 ax1.plot(x,1/x) # 绘制反比例函数曲线 ax1.legend(['loc=1']) # 在右上角增加图例 ax2.legend(['y = 1 / x']) # 图例的内容 ax1.plot([1,1,2,2],[0,1,1,0],'r',linewidth=0.2) # 绘制撒点范围框 ax1.set_title('蒙特卡洛法动态散点分布图') # 图的标题动态更新/<code>
6.4 第4步:联动子图1和子图2
<code>#4-1 子图1的散点图 for i in range(DARTS): x = random.uniform(1,2) y = random.uniform(0,1) if y < 1/x: # 点落在曲线下方 counts += 1 ax1.plot(x,y,'g.') else: # 点落在曲线上方 ax1.plot(x,y,'r.') if counts>0: e = pow(2,i/counts) #4-2 子图2设置 # 开始画右边的图:e的计算值随投掷次数的关系 xs[0] = xs[1] # 上一个e值与下一个e值,通过xs与ys列表中的两个元素进行两点连线 xs[1] = i ys[0] = ys[1] ys[1] = e ax2.set_ylim(0,4.5) # y轴坐标范围 ax2.set_xlabel('Number of try') # x轴标签 ax2.set_ylabel('Estimation of e') # y轴标签 ax2.set_yticks(np.arange(0,4.5,0.5)) # y轴刻度线 ax2.set_title('e:{:.10f}\ncount:{}'.format(e,i)) # 图的标题动态更新 # 绘制2.71828参考线 ax2.axhline(np.e,linewidth=0.05,color='r') # 绘制e的计算值随撒点次数变化的曲线 ax2.plot(xs,ys,'y--',linewidth=0.3) #子图1和子图2交互(ax1与ax2交互) plt.ion() # 保持图像处于交互更新状态 plt.pause(0.2) # 控制撒点速度/<code>
7 小结
=====
7.1 环境:matplotlib3.2+python3.8
7.2 注意matplotlib中文设置:
请参考我的文章,以免报错。
《强!3种方法实现python-matplotlib显示中文》
===分析仔细,值得收藏===