2019年12月17日火曜日

数学 - Python - 解析学 - 微分方程式論 - 三角関数(正弦と余弦、加法定理、2倍角)、定積分

1. $\begin{array}{l}\underset{0}{\overset{\frac{\pi }{4}}{\int }}\left(2\mathrm{sin}x\left(1+\mathrm{cos}x\right)-2\mathrm{sin}x\right)\mathrm{dx}\\ =\underset{0}{\overset{\frac{\pi }{4}}{\int }}\left(2\mathrm{sin}x+2\mathrm{sin}x\mathrm{cos}x-2\mathrm{sin}x\right)\mathrm{dx}\\ =\underset{0}{\overset{\frac{\pi }{4}}{\int }}\mathrm{sin}2x\mathrm{dx}\\ =\frac{1}{2}{\left[-\mathrm{cos}2x\right]}_{0}^{\frac{\pi }{4}}\\ =-\frac{1}{2}\left(\mathrm{cos}\frac{\pi }{2}-\mathrm{cos}0\right)\\ =-\frac{1}{2}\left(0-1\right)\\ =\frac{1}{2}\end{array}$

コード

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

x = symbols('x')
f1 = 2 * sin(x) * (1 + cos(x))
f2 = 2 * sin(x)

class MyTestCase(TestCase):
def test(self):
self.assertEqual(
Integral(f1 - f2, (x, 0, pi / 4)).doit(), Rational(1, 2))

p = plot(f1, f2, f1 - f2,
(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('sample3.png')

if __name__ == '__main__':
main()


% ./sample3.py -v
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.194s

OK
%