## 2019年11月15日金曜日

### 数学 - Python - 円の中にひそむ関数 - 三角関数 - 加法定理 - 三角関数の合成 - 正弦と余弦

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

2. $\begin{array}{l}3\mathrm{sin}\theta +4\mathrm{cos}\theta \\ =5\left(\frac{3}{5}\mathrm{sin}\theta +\frac{4}{5}\mathrm{cos}\theta \right)\\ =5\mathrm{sin}\left(\theta +\alpha \right)\\ \mathrm{sin}\alpha =\frac{4}{5},\mathrm{cos}\alpha =\frac{3}{5}\end{array}$

3. $\begin{array}{l}5\mathrm{sin}\theta +12\mathrm{cos}\theta \\ =13\left(\frac{5}{13}\mathrm{sin}\theta +\frac{12}{13}\mathrm{cos}\theta \right)\\ =13\mathrm{cos}\left(\theta +\alpha \right)\\ \mathrm{sin}\alpha =-\frac{5}{13},\mathrm{cos}\alpha =\frac{12}{13}\end{array}$

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

コード

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

print('20.')
theta = 2

class MyTestCase(TestCase):
def test1(self):
self.assertEqual(float(sqrt(3) * sin(theta) - cos(theta)),
float(2 * sin(theta + 11 * pi / 6)))

def test2(self):
alpha = asin(Rational(4, 5))
self.assertEqual(float(3 * sin(theta) + 4 * cos(theta)),
float(5 * sin(theta + alpha)))

def test3(self):
alpha = asin(-Rational(5, 13))
self.assertEqual(float(5 * sin(theta) + 12 * cos(theta)),
float(13 * cos(theta + alpha)))

def test4(self):
self.assertEqual(float(cos(theta) - sin(theta)),
float(sqrt(2) * cos(theta + pi / 4)))

x = symbols('x')
p = plot(sqrt(3) * sin(x) - cos(x),
3 * sin(x) + 4 * cos(x),
5 * sin(x) + 12 * cos(x),
cos(x) - sin(x),
ylim=(-10, 10),
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'sample20.png')

if __name__ == '__main__':
main()


% ./sample20.py -v
20.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok
test4 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.032s

OK
%