## 2019年11月29日金曜日

### 数学 - Python - 円の中にひそむ関数 - 三角関数 - 加法定理 - 三角関数の諸公式 - 正弦と余弦、2倍角、方程式の解、累乗の和(平方)、因数分解

1. $\begin{array}{l}0\le \theta <2\pi \\ \mathrm{sin}\theta =\mathrm{sin}2\theta \\ \mathrm{sin}\theta =2\mathrm{sin}\theta \mathrm{cos}\theta \\ \mathrm{sin}\theta \left(2\mathrm{cos}\theta -1\right)=0\\ \mathrm{sin}\theta =0\\ \mathrm{cos}\theta =\frac{1}{2}\\ \theta =0,\pi ,\frac{\pi }{3},\frac{5}{3}\pi \end{array}$

2. $\begin{array}{l}\mathrm{sin}\theta -\mathrm{cos}2\theta =0\\ \mathrm{sin}\theta -\left({\mathrm{cos}}^{2}\theta -{\mathrm{sin}}^{2}\theta \right)=0\\ \mathrm{sin}\theta -\left(1-2{\mathrm{sin}}^{2}\theta \right)=0\\ 2{\mathrm{sin}}^{2}\theta +\mathrm{sin}\theta -1=0\\ \left(2\mathrm{sin}\theta -1\right)\left(\mathrm{sin}\theta +1\right)=0\\ \mathrm{sin}\theta =-1,\frac{1}{2}\\ \theta =\frac{3}{2}\pi ,\frac{\pi }{6},\frac{5}{6}\pi \end{array}$

3. $\begin{array}{l}\mathrm{cos}2\theta -3\mathrm{cos}\theta -1=0\\ {\mathrm{cos}}^{2}\theta -{\mathrm{sin}}^{2}\theta -3\mathrm{cos}\theta -1=0\\ 2{\mathrm{cos}}^{2}\theta -3\mathrm{cos}\theta -2=0\\ \left(2\mathrm{cos}\theta +1\right)\left(\mathrm{cos}\theta -2\right)=0\\ \mathrm{cos}\theta =-\frac{1}{2}\\ \theta =\frac{2}{3}\pi ,\frac{4}{3}\pi \end{array}$

コード

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

print('30.')

theta = symbols('θ')
domain = Interval.Ropen(0, 2 * pi)
f1 = sin(theta) - sin(2 * theta)
f2 = sin(theta) - cos(2 * theta)
f3 = cos(2 * theta) - 3 * cos(theta) - 1
fs = [f1, f2, f3]

class MyTestCase(TestCase):
def test1(self):
self.assertEqual(solveset(f1, theta, domain=domain),
{0, pi, pi / 3, 5 * pi / 3})

def test2(self):
self.assertEqual(solveset(f2, theta, domain=domain),
{3 * pi / 2, pi / 6, 5 * pi / 6})

def test3(self):
self.assertEqual(solveset(f3, theta, domain=domain),
{2 * pi / 3, 4 * pi / 3})

p = plot(*fs,
(theta, 0, 2 * pi),
ylim=(-pi, pi),
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'sample30.png')

if __name__ == '__main__':
main()


% ./sample30.py -v
30.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 1.357s

OK
%