## 2020年3月4日水曜日

### 数学 - Python - 微分積分学 - 積分法 - 平面図形の面積の計算 - 放物線とx軸で囲まれた図形、原点を通る直線、傾き

1. $\begin{array}{l}{x}^{2}-x=0\\ x\left(x-1\right)=0\\ {\int }_{0}^{1}-\left({x}^{2}-x\right)\mathrm{dx}\\ =-{\left[\frac{1}{3}{x}^{3}-{x}^{2}\right]}_{0}^{1}\\ =1-\frac{1}{3}\\ =\frac{2}{3}\end{array}$

2. $\begin{array}{l}{\int }_{0}^{{x}_{1}}\left({\lambda }_{1}x-{\lambda }_{2}x\right)\mathrm{dx}+{\int }_{{x}_{1}}^{{x}_{2}}\left(f\left(x\right)-{\lambda }_{2}x\right)\mathrm{dx}\\ =\frac{{\lambda }_{1}-{\lambda }_{2}}{2}{x}_{1}^{2}-\frac{{\lambda }_{2}}{2}\left({x}_{2}^{2}-{x}_{1}^{2}\right)+{\int }_{{x}_{1}}^{{x}_{2}}f\left(x\right)\mathrm{dx}\\ =\frac{1}{2}\left({\lambda }_{1}{x}_{1}^{2}-{\lambda }_{2}{x}_{2}^{2}\right)+{\int }_{{x}_{1}}^{{x}_{2}}f\left(x\right)\mathrm{dx}\end{array}$

コード

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

print('1, 2.')

x = symbols('x')
f = x ** 2 - x
g = - x ** 3 - 2 * x ** 2 + x + 10

lambda1 = 2
lambda2 = 1

l1 = lambda1 * x
l2 = lambda2 * x

class MyTestCase(TestCase):
def test1(self):
self.assertEqual(Integral(x - f, (x, 0, 1)).doit(), Rational(2, 3))

p = plot(f, g, l1, l2,
(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'sample1.png')

if __name__ == '__main__':
main()


% ./sample1.py -v
1, 2.
test1 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.022s

OK
%