## 2020年6月22日月曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 係数、通る点、最小値、頂点

1. 問題の仮定より、

$\begin{array}{l}5={2}^{2}+2p+q\\ q=1-2p\end{array}$
$\begin{array}{l}y={x}^{2}+px+\left(1-2p\right)\\ ={\left(x+\frac{p}{2}\right)}^{2}-\frac{{p}^{2}}{4}+1-2p\end{array}$

最小値が4なので、

$\begin{array}{l}-\frac{{p}^{2}}{4}+1-2p=4\\ {p}^{2}+8p-4=-16\\ {p}^{2}+8p+12=0\\ \left(p+2\right)\left(p+6\right)=0\end{array}$

よって、

$\begin{array}{l}p=-2,q=5\\ p=-6,q=13\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import plot, Rational
from sympy.abc import x, p, q

print('12.')

y = x ** 2 + p * x + q
pqs = [(-2, 5), (-6, 13)]
ys = [y.subs({p: p0, q: q0}) for p0, q0 in pqs]

class Test(TestCase):
def test(self):
for y in ys:
self.assertEqual(y.subs({x: 2}), 5)

p = plot(*ys, 5, 4,
(x, -5, 5),
ylim=(-0, 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('sample12.png')

if __name__ == "__main__":
main()


% ./sample12.py -v
12.
test (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
%