2020年3月7日土曜日

学習環境

解析入門 原書第3版 (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第Ⅵ部(多変数の関数)、第18章(ベクトルの微分)、2(曲線の長さ)の練習問題1、2の解答を求めてみる。


  1. d dt cos t , sin t , t = - sin t , cos t , 1 0 1 sin 2 t + cos 2 t + 1 dt = 0 1 2 dt = 2 t 0 1 = 2

  2. d dt cos 2 t , sin 2 t , 3 t = - 2 sin 2 t , 2 cos 2 t , 3 1 3 4 sin 2 2 t + 4 cos 2 2 t + 9 dt = 1 3 13 dt = 2 13

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, Derivative, sin, cos, Integral, sqrt
from sympy.plotting import plot3d_parametric_line


print('1, 2.')

t = symbols('t')
x1 = Matrix([cos(t), sin(t), t])
x2 = Matrix([cos(2 * t), sin(2 * t), 3 * t])
tss = [(0, 1),
       (1, 3)]


class MyTestCase(TestCase):
    def test1(self):
        l = Integral(sqrt(sum([Derivative(f, t, 1).doit() ** 2 for f in x1])),
                     (t, *tss[0])).doit().simplify()
        self.assertEqual(l, sqrt(2))

    def test2(self):
        l = Integral(sqrt(sum([Derivative(f, t, 1).doit() ** 2 for f in x2])),
                     (t, *tss[1])).doit().simplify()
        self.assertEqual(l, 2 * sqrt(13))


colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']
p = plot3d_parametric_line(*[(*x1, (t, t1, t2))
                             for t1, t2 in [(-10, 0), tss[0], (1, 10)]],
                           legend=True,
                           show=False)
for o, color in zip(p, colors):
    o.line_color = color
p.save('sample1.png')

p = plot3d_parametric_line(*[(*x2, (t, t1, t2))
                             for t1, t2 in [(-10, 1), tss[1], (3, 10)]],
                           legend=True,
                           show=False)
for o, color in zip(p, colors):
    o.line_color = color
p.show()
p.save('sample2.png')


if __name__ == "__main__":
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample1.py -v
1, 2.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 1.271s

OK
%

0 コメント:

コメントを投稿