## 2020年6月9日火曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 関数のグラフと方程式 - 2つの関数のグラフの交点、放物線と直線、共有点の個数、接線、接点

1. $\begin{array}{l}{x}^{2}+4=-2x+k\\ {x}^{2}+2x+4-k=0\\ \frac{D}{4}=1-\left(4-k\right)=k-3\end{array}$

よって、

$\begin{array}{l}k-3<0\\ k<3\end{array}$

のとき、共有点はない。

$k=3$

のとき、 1個。

$k>3$

のとき、 2個。

2. $\begin{array}{l}2kx-2k={x}^{2}+3\\ {x}^{2}-2kx+2k+3=0\\ \frac{D}{4}={k}^{2}-\left(2k+3\right)=0\\ {k}^{2}-2k-3=0\\ \left(k-3\right)\left(k+1\right)=0\end{array}$

よって、

$k=-1,3$

のとき接する。

コード

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

x, k = symbols('x, k')

print('24, 25.')
y11 = x ** 2 + 4
y12 = -2 * x + k

y21 = 2 * k * x - 2 * k
y22 = x ** 2 + 3

class Test(TestCase):
def test_24(self):
self.assertEqual(
len(solveset(y11 - y12.subs({k: 2}), domain=S.Reals)),
0,
)

def test_24_1(self):
self.assertEqual(
len(solveset(y11 - y12.subs({k: 3}), domain=S.Reals)),
1,
)

def test_24_2(self):
self.assertEqual(
len(solveset(y11 - y12.subs({k: 4}), domain=S.Reals)),
2,
)

def test_25(self):
for k0 in [-1, 3]:
self.assertEqual(
len(solveset((y21 - y22).subs({k: k0}))),
1,
)

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

for i, ys in enumerate([[y11] + [y12.subs({k: k0})
for k0 in [2, 3, 4]],
[y22] + [y21.subs({k: k0})
for k0 in [-1, (-1 + 3) / 2, 3]]], 24):
p = plot(*ys,
(x, -10, 10),
ylim=(-10, 10),
legend=True,
show=False)
for o, color in zip(p, colors):
o.line_color = color
p.save(f'sample{i}.png')
p.show()
if __name__ == "__main__":
main()


% ./sample24.py -v
24, 25.
test_24 (__main__.Test) ... ok
test_24_1 (__main__.Test) ... ok
test_24_2 (__main__.Test) ... ok
test_25 (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.077s

OK
%