2019年3月29日金曜日

数学 - Python - 解析学 - 級数 - テイラーの公式 - 指数関数(累乗(べき乗、平方)、逆数)

1. $\begin{array}{}\frac{d}{\mathrm{dx}}{e}^{-{x}^{2}}=-2x{e}^{-{x}^{2}}\\ \frac{{d}^{2}}{{\mathrm{dx}}^{2}}{e}^{-{x}^{2}}\\ =-2{e}^{-{x}^{2}}-2x{e}^{-{x}^{2}}\left(-2x\right)\\ =2{e}^{-{x}^{2}}\left(2{x}^{2}-1\right)\\ \frac{{d}^{3}}{{\mathrm{dx}}^{3}}{e}^{-{x}^{2}}\\ =2\left(-2x{e}^{-{x}^{2}}\left(2{x}^{2}-1\right)+{e}^{-{x}^{2}}4x\right)\\ =4{e}^{-{x}^{2}}\left(-2{x}^{3}+3x\right)\\ \frac{{d}^{4}}{{\mathrm{dx}}^{4}}{e}^{-{x}^{2}}\\ =4\left(-2x{e}^{-{x}^{2}}\left(-2{x}^{3}+3x\right)+{e}^{-{x}^{2}}\left(-6{x}^{2}+3\right)\right)\\ =4{e}^{-{x}^{2}}\left(-2x\left(-2{x}^{3}+3x\right)-6{x}^{2}+3\right)\\ =4{e}^{-{x}^{2}}\left(4{x}^{4}-12{x}^{2}+3\right)\end{array}$

よって、 求める4次のテイラー多項式は、

$\begin{array}{}x-\frac{2}{2!}{x}^{2}+\frac{12}{4!}{x}^{4}\\ =x-{x}^{2}+\frac{1}{2}{x}^{4}\end{array}$

コード

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, exp, plot, factorial, Derivative

print('1.')

x = symbols('x')
f = exp(-x**2)
g = sum([Derivative(f, x, i).subs({x: 0}) /
factorial(i) * x ** i for i in range(5)])
for o in [f, g, g.doit()]:
pprint(o)
print()

p = plot(f, g.doit(), (x, -2.5, 2.5), ylim=(-5, 5), show=False, legend=True)
colors = ['red', 'green', 'blue', 'brown']
for s, color in zip(p, colors):
s.line_color = color
p.show()
p.save('sample1.png')


C:\Users\...>py -3 sample1.py
1.
2
-x
ℯ

⎛  4⎛   2⎞⎞│         ⎛  3⎛   2⎞⎞│         ⎛  2⎛   2⎞⎞│
4 ⎜ d ⎜ -x ⎟⎟│       3 ⎜ d ⎜ -x ⎟⎟│       2 ⎜ d ⎜ -x ⎟⎟│
x ⋅⎜───⎝ℯ   ⎠⎟│      x ⋅⎜───⎝ℯ   ⎠⎟│      x ⋅⎜───⎝ℯ   ⎠⎟│
⎜  4      ⎟│         ⎜  3      ⎟│         ⎜  2      ⎟│        ⎛  ⎛   2⎞⎞│
⎝dx       ⎠│x=0      ⎝dx       ⎠│x=0      ⎝dx       ⎠│x=0     ⎜d ⎜ -x ⎟⎟│
────────────────── + ────────────────── + ────────────────── + x⋅⎜──⎝ℯ   ⎠⎟│
24                   6                    2              ⎝dx      ⎠│x=

+ 1
0

4
x     2
── - x  + 1
2

C:\Users\...>