## 2019年12月1日日曜日

### 数学 - Python - 円の中にひそむ関数 - 三角関数 - 加法定理 - 三角関数の諸公式 - 正弦と余弦、累乗(平方)、2倍角、グラフの描画、平行移動、縮小

1. $\begin{array}{l}y={\mathrm{sin}}^{2}x\\ =\frac{\mathrm{cos}\left(x-x\right)-\mathrm{cos}\left(x+x\right)}{2}\\ =-\frac{1}{2}\mathrm{cos}2x+\frac{1}{2}\\ y={\mathrm{cos}}^{2}x\\ =\frac{\mathrm{cos}\left(x+x\right)+\mathrm{cos}\left(x-x\right)}{2}\\ =\frac{1}{2}\mathrm{cos}\left(2x\right)+\frac{1}{2}\end{array}$

グラフの描画。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, symbols, sin, cos, Rational, plot

print('32.')

x = symbols('x')
fs = [sin(x) ** 2, cos(x) ** 2]

d = {x: 1}

class MyTestCase(TestCase):
def test_sin_square(self):
self.assertEqual(*[float(g.subs(d))
for g in [fs[0], -cos(2 * x) / 2 + Rational(1, 2)]])

def test_cos_square(self):
self.assertEqual(*[float(g.subs(d))
for g in [fs[1], cos(2 * x) / 2 + Rational(1, 2)]])

p = plot(cos(x), cos(2 * x), cos(2 * x) / 2, -cos(2 * x) / 2, *fs,
(x, -5, 5),
ylim=(-5, 5),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

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

if __name__ == '__main__':
main()

% ./sample32.py -v
32.
test_cos_square (__main__.MyTestCase) ... ok
test_sin_square (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK
%