## 2020年6月13日土曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 3点を通る2次関数

1. $\left\{\begin{array}{l}4a-2b+c=-3\\ a+b+c=-6\\ 4a+2b+c=1\end{array}$
$\begin{array}{l}4b=4\\ b=1\end{array}$
$\begin{array}{l}a+1+c=-6\\ c=-a-7\end{array}$
$\begin{array}{l}4a+2-a-7=1\\ 3a=6\\ a=2\\ c=-9\end{array}$

よって求める 3点を通る2次関数は

$y=2{x}^{2}+x-9$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, plot, solve
from sympy.abc import a, b, c, x

print('4.')

y = a * x ** 2 + b * x + c

class Test(TestCase):
def test(self):
self.assertEqual(
solve([y.subs({x: x0}) - y0
for x0, y0 in [(-2, -3),
(1, -6),
(2, 1)]], a, b, c),
{a: 2, b: 1, c: -9})

colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

p = plot(y.subs({a: 2, b: 1, c: -9}),
-3, -6, 1,
(x, -10, 10),
ylim=(-10, 10),
legend=True,
show=False)
for o, color in zip(p, colors):
o.line_color = color
p.save('sample4.png')
p.show()

if __name__ == "__main__":
main()


% ./sample4.py -v
4.
test (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.020s

OK
%