## 2019年11月23日土曜日

### 数学 - Python - 微分積分学 - 平均値の定理 - 中間値の定理 - 3次、連続関数、導関数、解、実根の個数

1. $\begin{array}{l}f\left(x\right)={x}^{3}-2{x}^{2}+5\\ f\text{'}\left(x\right)=3{x}^{2}-4x\\ =x\left(3x-4\right)\\ f\text{'}\left(x\right)=0\\ x=0,\frac{4}{3}\\ f\left(0\right)=5>0\\ f\left(\frac{4}{3}\right)={\left(\frac{4}{3}\right)}^{3}-2{\left(\frac{4}{3}\right)}^{2}+5\\ =\frac{64}{27}-\frac{32}{27}+\frac{135}{27}\\ >0\end{array}$

よって、 方程式

${x}^{3}-2{x}^{2}+5=0$

は1個の実根をもつ。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, symbols, plot, solveset, S

print('2.')

x = symbols('x', real=True)
f = x ** 3 - 2 * x ** 2 + 5

class MyTestCase(TestCase):
def test(self):
xs = solveset(f, x, domain=S.Reals)
self.assertEqual(len(xs), 1)

p = plot(f,
ylim=(-10, 10),
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('sample2.png')

if __name__ == '__main__':
main()


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

----------------------------------------------------------------------
Ran 1 test in 0.132s

OK
%