## 2020年5月29日金曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 2次関数のグラフ - y軸との交点、x軸との交点、グラフの軸、頂点の座標、平行移動

1. $\begin{array}{l}y={\left(0-2\right)}^{2}-1=3\\ 0={\left(x-2\right)}^{2}-1\\ \left(x-2+1\right)\left(x-2-1\right)=0\\ \left(x-1\right)\left(x-3\right)=0\\ x=1,3\end{array}$

1. 軸の方程式。

$x=0$

頂点。

$\left(0,-2\right)$

2. $\begin{array}{l}x=-1\\ \left(-1,0\right)\end{array}$

3. $\begin{array}{l}x=-2\\ \left(-2,1\right)\end{array}$

4. $\begin{array}{l}x=3\\ \left(3,4\right)\end{array}$

コード

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

print('9, 10.')
x = symbols('x')
ys = [x ** 2 / 2 - 2,
-2 * (x + 2) ** 2,
(x + 2) ** 2 + 1,
-(x - 3) ** 2 / 2 + 4]

class Test(TestCase):
def test9(self):
y = (x - 2) ** 2 - 1
self.assertEqual(y.subs({x: 0}), 3)
self.assertEqual(y.subs({x: 1}), 0)
self.assertEqual(y.subs({x: 3}), 0)

p = plot(*ys,
(x, -10, 10),
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('sample10.png')

if __name__ == "__main__":
main()


% ./sample9.py -v
9, 10.
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/plotting/plot.py:1065: MatplotlibDeprecationWarning:
The set_smart_bounds function was deprecated in Matplotlib 3.2 and will be removed two minor releases later.
self.ax[i].spines['left'].set_smart_bounds(True)
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/plotting/plot.py:1066: MatplotlibDeprecationWarning:
The set_smart_bounds function was deprecated in Matplotlib 3.2 and will be removed two minor releases later.
self.ax[i].spines['bottom'].set_smart_bounds(False)
test9 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
%