0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonでネイピア数を求めてみた

Last updated at Posted at 2023-06-03

前提条件

  • Python3
  • matplotlib

ネイピア数eは以下の値を持つとされている。
e = 2.71828 18284 59045 23536 02874 71352 …

式は以下で定義される。これはネイピア数の定義の一つである。

e = \lim_{n \to \infty} \Big(1 + \frac{1}{n} \Big)^n

目的

ネイピア数eを求めてみる。

環境構築

pip install matplotlib

サンプル

napier.py
# -*- coding: utf-8 -*-
import math
import matplotlib.pyplot as plt


def napier(n):
    return (1+1/n)**n  
    
i = int(1.0e2)
y = []
for j in range(1,i+1):
    y.append(napier(j))

print(y[i-1])
print(math.e)

plt.title("Napier")
plt.plot(y)
plt.show()

実行結果

2.7048138294215285
2.718281828459045

グラフ

napier.png

napier.pyの修正

ネイピア数の別な定義

e =1+1/1!+1/2!+1/3!+1/4!+⋯

指摘を受けてnapier.pyを修正

napier2.py
import math
import matplotlib.pyplot as plt

def napier(n):
    return (1+1/n)**n  

def d_napier(n):
    x = 0
    for i in range(n + 1):
        x += 1 / math.factorial(i)
    return x

i = int(1.0e2)
y = []
for j in range(1,i+1):
    y.append(napier(j))

y1 = []
for k in range(1,i+1):
    y1.append(d_napier(k))

print(y[i-1])
print(y1[i-1])
print(math.e)

plt.title("Napier")
plt.plot(y)
plt.plot(y1)
plt.show()

実行結果

2.7048138294215285
2.7182818284590455
2.718281828459045

グラフ

napier2.png

参考資料

編集後記

ゆっくり動画を見て、ネイピア数を計算してみたいと思い立ち、やってみた。
コメントの指摘を受けて、napier.pyを修正しました。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?