2019年10月19日土曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第4部(級数)、第15章(級数)、7(べき級数の微分と積分)の練習問題1を求めてみる。


  1. d dx S x = d dx k = 0 - 1 k x 2 k + 1 2 k + 1 ! = k = 0 - 1 k 2 k + 1 x 2 k 2 k + 1 ! = k = 0 - 1 k x 2 k 2 k ! = C x d dx C x = d dx k = 0 - 1 2 k x 2 k 2 k ! = k = 1 - 1 k 2 k x 2 k - 1 2 k ! = k = 1 - 1 k x 2 k - 1 2 k - 1 ! = k = 0 - 1 k + 1 x 2 k + 1 2 k + 1 ! = - k = 0 - 1 k x 2 k + 1 2 k + 1 ! = - S x

コード

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()

入出力結果(Zsh、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

% ./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
%

0 コメント:

コメントを投稿

関連コンテンツ