## 2019年4月3日水曜日

### 数学 - Python - 解析学 - 級数 - テイラーの公式 - 指数関数(逆数、小数第3位まで、剰余項の評価)

1. $\begin{array}{}{e}^{-x}\\ =1-x+\frac{1}{2!}{x}^{2}-\frac{1}{3!}{x}^{3}+\frac{1}{4!}{x}^{4}-\frac{1}{5!}{x}^{5}+\frac{1}{6!}{x}^{6}+\left|{R}_{7}\right|\\ 7!=5040\\ {R}_{7}\le \frac{1}{5040}·{e}^{0}=\frac{1}{5040}\end{array}$

求める小数第3位までの値。

$\begin{array}{}\frac{1}{e}\\ \fallingdotseq 1-1+\frac{1}{2!}-\frac{1}{3!}+\frac{1}{4!}-\frac{1}{5!}+\frac{1}{6!}\\ =\frac{6·5·4·3-6·5·4+6·5-6+1}{6!}\\ =\frac{360-120+30-6+1}{6!}\\ =\frac{265}{6!}\\ =\frac{53}{6·4·3·2}\\ =\frac{53}{144}\\ =0.368\dots \end{array}$

コード

Python 3

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

print('6.')

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

for o in [f, g, g.doit(), g.doit().subs({x: 1}), float(g.doit().subs({x: 1})),
float(exp(-1))]:
pprint(o)
print()

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


C:\Users\...>py sample6.py
6.
-x
ℯ

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

⎛  2     ⎞│
2 ⎜ d ⎛ -x⎞⎟│
x ⋅⎜───⎝ℯ  ⎠⎟│
⎜  2     ⎟│
⎝dx      ⎠│x=0     ⎛d ⎛ -x⎞⎞│
+ ───────────────── + x⋅⎜──⎝ℯ  ⎠⎟│    + 1
2             ⎝dx     ⎠│x=0

6     5    4    3    2
x     x    x    x    x
─── - ─── + ── - ── + ── - x + 1
720   120   24   6    2

53
───
144

0.3680555555555556

0.36787944117144233

C:\Users\...>