## 2020年3月7日土曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトルの微分 - 曲線の長さ - スパイラル(正弦と余弦)、積分

1. $\begin{array}{l}\frac{d}{\mathrm{dt}}\left(\mathrm{cos}t,\mathrm{sin}t,t\right)\\ =\left(-\mathrm{sin}t,\mathrm{cos}t,1\right)\\ {\int }_{0}^{1}\sqrt{{\mathrm{sin}}^{2}t+{\mathrm{cos}}^{2}t+1}\mathrm{dt}\\ ={\int }_{0}^{1}\sqrt{2}\mathrm{dt}\\ ={\left[\sqrt{2}t\right]}_{0}^{1}\\ =\sqrt{2}\end{array}$

2. $\begin{array}{l}\frac{d}{\mathrm{dt}}\left(\mathrm{cos}2t,\mathrm{sin}2t,3t\right)\\ =\left(-2\mathrm{sin}2t,2\mathrm{cos}2t,3\right)\\ {\int }_{1}^{3}\sqrt{4{\mathrm{sin}}^{2}\left(2t\right)+4{\mathrm{cos}}^{2}\left(2t\right)+9}\mathrm{dt}\\ ={\int }_{1}^{3}\sqrt{13}\mathrm{dt}\\ =2\sqrt{13}\end{array}$

コード

#!/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()


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

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

OK
%