0
4

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 5 years have passed since last update.

[Pythonによる科学・技術計算] 和の計算, 数値計算

Last updated at Posted at 2017-07-18

#内容

$e^x$の値をテイラー展開のn次の項までの和

e^{x} = 1+\frac{x}{1!} +\frac{x^{2}}{2!}+ .... \frac{x^{n}}{n!}

によって計算する。


#コード

from math import pi,e, log, factorial
import matplotlib.pyplot as plt

"""
e^xのテイラー展開
例: x=1としてeを計算する
"""

def calc_e(n,x):
    dum=0.0
    for nn in range(n+1):  # x^n/n!の計算
        dum+=x**nn/factorial(nn)
    return dum

#main
sol=[]
for j in range(8):  # n=7次までの計算
    sol.append(calc_e(j,1)) #n=1 ~ 8までの次数で打ち切った場合のeをsolという名のリストへ格納する

# for plot
plt.plot(sol)
plt.xlabel('x',fontsize=24)
plt.show()

t.png


#結果
厳密値は,e = 2.718281828459045...
n=4で誤差は1%以内となっている。

n e
0 1.0
1 2.0
2 2.5
3 2.6666666666666665
4 2.708333333333333
5 2.7166666666666663
6 2.7180555555555554
7 2.7182539682539684

0
4
0

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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?