## 2019年10月19日土曜日

### 数学 - Python - 解析学 - 級数 - べき級数の微分と積分 - 三角関数(正弦と余弦)のべき級数による定義、性質、項別微分

1. $\begin{array}{l}\frac{d}{\mathrm{dx}}S\left(x\right)\\ =\frac{d}{\mathrm{dx}}\sum _{k=0}^{\infty }{\left(-1\right)}^{k}\frac{{x}^{2k+1}}{\left(2k+1\right)!}\\ =\sum _{k=0}^{\infty }{\left(-1\right)}^{k}\left(2k+1\right)\frac{{x}^{2k}}{\left(2k+1\right)!}\\ =\sum _{k=0}^{\infty }{\left(-1\right)}^{k}\frac{{x}^{2k}}{\left(2k\right)!}\\ =C\left(x\right)\\ \frac{d}{\mathrm{dx}}C\left(x\right)\\ =\frac{d}{\mathrm{dx}}\sum _{k=0}^{\infty }{\left(-1\right)}^{2k}\frac{{x}^{2k}}{\left(2k\right)!}\\ =\sum _{k=1}^{\infty }{\left(-1\right)}^{k}2k\frac{{x}^{2k-1}}{\left(2k\right)!}\\ =\sum _{k=1}^{\infty }{\left(-1\right)}^{k}\frac{{x}^{2k-1}}{\left(2k-1\right)!}\\ =\sum _{k=0}^{\infty }{\left(-1\right)}^{k+1}\frac{{x}^{2k+1}}{\left(2k+1\right)!}\\ =-\sum _{k=0}^{\infty }{\left(-1\right)}^{k}\frac{{x}^{2k+1}}{\left(2k+1\right)!}\\ =-S\left(x\right)\end{array}$

コード

Python 3

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, symbols, summation, oo, Limit, plot, factorial, Derivative

print('1.')

x, k, n = symbols('x, k, n')
s = summation((-1) ** k * x ** (2 * k + 1) / factorial(2 * k + 1), (k, 0, oo))
c = summation((-1) ** k * x ** (2 * k) / factorial(2 * k), (k, 0, oo))

class MyTestCase(TestCase):
def setUp(self):
pass

def tearDown(self):
pass

def test_ds_c(self):
self.assertEqual(Derivative(s, x, 1).doit(), c)

def test_dc_s(self):
self.assertEqual(Derivative(c, x, 1).doit(), -s)

def f(n):
return sum([(-1) ** k * x ** (2 * k + 1) / factorial(2 * k + 1) for k in range(n + 1)])

def g(n):
return sum([(-1) ** k * x ** (2 * k) / factorial(2 * k) for k in range(n + 1)])

ns = range(4)
fs = [s] + [f(n0) for n0 in ns]
gs = [c] + [f(n0) for n0 in ns]

p = plot(*(fs + gs),
(x, -10, 10),
ylim=(-10, 10),
legend=False,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color

for o in zip(fs + gs, colors):
pprint(o)
print()

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

if __name__ == '__main__':
main()


% ./sample1.py -v
1.
(sin(x), red)

(x, green)

⎛   3          ⎞
⎜  x           ⎟
⎜- ── + x, blue⎟
⎝  6           ⎠

⎛  5    3           ⎞
⎜ x    x            ⎟
⎜─── - ── + x, brown⎟
⎝120   6            ⎠

⎛    7      5    3            ⎞
⎜   x      x    x             ⎟
⎜- ──── + ─── - ── + x, orange⎟
⎝  5040   120   6             ⎠

(cos(x), purple)

(x, pink)

⎛   3          ⎞
⎜  x           ⎟
⎜- ── + x, gray⎟
⎝  6           ⎠

⎛  5    3             ⎞
⎜ x    x              ⎟
⎜─── - ── + x, skyblue⎟
⎝120   6              ⎠

⎛    7      5    3            ⎞
⎜   x      x    x             ⎟
⎜- ──── + ─── - ── + x, yellow⎟
⎝  5040   120   6             ⎠

test_dc_s (__main__.MyTestCase) ... ok
test_ds_c (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK
%