## 2020年1月19日日曜日

### 数学 - Python - 解析学 - 多変数の関数 - ベクトル - ベクトルのノルム - 実数値関数、積分による内積(スカラー積)の定義、三角関数(正弦と余弦)、倍角

• $\begin{array}{l}∥\mathrm{sin}nx∥\\ =\sqrt{⟨\mathrm{sin}nx,\mathrm{sin}nx⟩}\\ =\sqrt{{\int }_{-\pi }^{\pi }{\mathrm{sin}}^{2}nx\mathrm{dx}}\\ ={\int }_{-\pi }^{\pi }\frac{1-\mathrm{cos}2nx}{2}\mathrm{dx}\\ =\sqrt{\frac{1}{2}·2{\int }_{0}^{\pi }\left(1-\mathrm{cos}2nx\right)\mathrm{dx}}\\ =\sqrt{{\left[x+\frac{1}{2n}\mathrm{sin}2nx\right]}_{0}^{\pi }}\\ =\sqrt{\pi }\end{array}$

• $\begin{array}{l}\sqrt{{\int }_{-\pi }^{\pi }{\mathrm{cos}}^{2}mx\mathrm{dx}}\\ =\sqrt{{\int }_{-\pi }^{\pi }\frac{\mathrm{cos}2mx+1}{2}\mathrm{dx}}\\ =\sqrt{\frac{1}{2}·2{\left[-\frac{1}{2m}\mathrm{sin}2mx+x\right]}_{0}^{\pi }}\\ =\sqrt{\pi }\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Integral, plot, sqrt, pi, sin, cos, plot

print('17.')

x = symbols('x')
n = symbols('n', integer=True, positive=True)

def dot(f, g):
return Integral(f * g, (x, -pi, pi)).doit()

def norm(f):
return sqrt(dot(f, f))

class MyTestCase(TestCase):
def test_sine(self):
self.assertEqual(norm(sin(n * x)), sqrt(pi))

def test_cosine(self):
self.assertEqual(norm(cos(n * x)), sqrt(pi))

p = plot(*[sin(n * x) for n in range(1, 3)],
*[cos(n * x) for n in range(1, 3)],
*[sin(n * x) ** 2 for n in range(1, 3)],
*[cos(n * x) ** 2 for n in range(1, 3)],
(x, -5, 5),
ylim=(-5, 5),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

p.show()
p.save(f'sample17.png')

if __name__ == '__main__':
main()


% ./sample17.py -v
17.
test_cosine (__main__.MyTestCase) ... ok
test_sine (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.237s

OK
%