## 2019年3月11日月曜日

### 数学 - Python - 解析学 - 級数 - テイラーの公式 - 三角関数(余弦の多項式による近似)

1. $\begin{array}{}\mathrm{cos}0+\left(-\mathrm{sin}0\right)x+\frac{-\mathrm{cos}0}{2!}{x}^{2}+\frac{\mathrm{sin}0}{3!}{x}^{3}+\frac{\mathrm{cos}0}{4!}{x}^{4}\\ =1-\frac{1}{2!}{x}^{2}+\frac{1}{4!}{x}^{4}\end{array}$

コード

Python 3

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

x = symbols('x')
f = cos(x)
g = sum([Derivative(f, x, n).subs({x: 0}) * x ** n / factorial(n)
for n in range(5)])

for o in [f, g, g.doit()]:
pprint(o)
print()
p = plot(f, g.doit(), ylim=(-5, 5), legend=True, show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange', 'purple']
for s, color in zip(p, colors):
s.line_color = color

p.show()
p.save('sample1.png')


C:\Users\...>py -3 sample1.py
cos(x)

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

⎞│
(x))⎟│    + 1
⎠│x=0

4    2
x    x
── - ── + 1
24   2

C:\Users\...>