2
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.

テイラー展開を描いてみる

Last updated at Posted at 2017-05-22

テイラー展開ってなんやねん。
ってことでsympy で描いてみた話。

legendがやたら長くて収まりが悪いのはご愛嬌。
matplotlibsympy.plotting 経由で描かないとならないのでちょっとめんどくさい。

from sympy import sin, exp, plotting
from sympy.abc import x, y
from functools import reduce
import operator
import itertools

import seaborn as sns
sns.set_palette(sns.color_palette("Set1", 16))
sns.set_style('whitegrid')
col = sns.color_palette()

f = sin(x)
#  もしくは
#f = exp(x)

x0 = 0

def series(fx, x0=0, n=6):
    y = fx.series(x0=x0, n=None)    # x0周りのテイラー展開
    # n=None だと generator を返すので
    return reduce(operator.add, itertools.islice(y, n))    # 第n項まで抜き出す

def explot(fx, x0, i):
    if x0 == 0:
        title = "%s / Maclaurin series " % (fx)
    else:
        title = "%s / Taylor series(x=%f)" % (fx, x0)
        
    return plotting.plot(fx, (x, -10, 10), line_color=col[i], ylim=(-10, 10), title=title, show=False, legend=True, axis=True)

# 元の関数
p0 = explot(f, x0, 0)

# テイラー展開
for i in range(1, 8):
    p = explot(series(f, x0=x0, n=i), x0, i)
    p0.extend(p)

p0.show()

sin(x) のマクローリン展開

sin.maclaurin.png

sin(x)x=5 周りのテイラー展開

sin.taylor5.png

sin(x)x=π 周りのテイラー展開

sin.taylorPi.png

exp(x) のマクローリン展開

exp.maclaurin.png

exp(x)x=2 周りのテイラー展開

exp.taylor2.png

2
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
2
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?