2019年4月4日木曜日

数学 - Python - 解析学 - 級数 - テイラーの公式 - 指数関数(平方、立方、3次、剰余項の評価)

1. $\begin{array}{}{e}^{2}<{3}^{2}=9\\ \left|{R}_{4}\right|\\ <9·\frac{{2}^{4}}{4!}\\ =6\end{array}$

2. $\begin{array}{}{e}^{3}<{3}^{3}=27\\ \left|{R}_{4}\right|\\ <27·\frac{{3}^{4}}{4!}\\ =\frac{27·{3}^{3}}{4·2}\\ =\frac{729}{8}\end{array}$

コード

Python 3

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

print('7.')

x = symbols('x')
f = exp(x)
g = sum([Derivative(f, x, i).subs({x: 0}) /
factorial(i) * x ** i for i in range(4)])
for o in [g, g.doit()]:
pprint(o)
print()
xs = [2, 3]
rs = [6, Rational(729, 8)]
for i, (x0, r0) in enumerate(zip(xs, rs)):
print(f'x = {x0}')
gx = g.subs({x: x0})
r = (exp(x0) - gx).doit()
for o in [gx, float(r), r0, r <= r0]:
pprint(o)
print()

p = plot(f, g.doit(), (x, -5, 5), ylim=(0, 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('sample7.png')


C:\Users\...>py sample8.py
7.
⎛  3    ⎞│         ⎛  2    ⎞│
3 ⎜ d ⎛ x⎞⎟│       2 ⎜ d ⎛ x⎞⎟│
x ⋅⎜───⎝ℯ ⎠⎟│      x ⋅⎜───⎝ℯ ⎠⎟│
⎜  3    ⎟│         ⎜  2    ⎟│
⎝dx     ⎠│x=0      ⎝dx     ⎠│x=0     ⎛d ⎛ x⎞⎞│
──────────────── + ──────────────── + x⋅⎜──⎝ℯ ⎠⎟│    + 1
6                  2             ⎝dx    ⎠│x=0

3    2
x    x
── + ── + x + 1
6    2

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

1.055722765597317

6

True

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

7.085536923187668

729/8

True

C:\Users\...>