## 2020年3月16日月曜日

### 数学 - Python - 代数学 - 1次方程式, 2次方程式 - 2次方程式の解、判別式、異符号の2つの実数解をもつ場合、重複解をもつ場合

1. $\begin{array}{l}D={b}^{2}-4ac>{b}^{2}>0\\ x=\frac{-b±\sqrt{{b}^{2}-4ac}}{2a}\\ -b+\sqrt{{b}^{2}-4ac}>-b+b=0\\ -b-\sqrt{{b}^{2}-4ac}<0\end{array}$

よって異符号の2つの実数解をもつ。

2. $\begin{array}{l}{a}^{2}-4\left(a+3\right)=0\\ {a}^{2}-4a-12=0\\ \left(a-6\right)\left(a+2\right)=0\\ a=-2,6\\ {x}^{2}+2x+1=0\\ {\left(x+1\right)}^{2}=0\\ x=-1\\ {x}^{2}-6x+9=0\\ {\left(x-3\right)}^{2}=0\\ x=3\end{array}$

1. $\begin{array}{l}{x}^{2}+\left(k-3\right)x-2k+3=0\\ {\left(k-3\right)}^{2}-4\left(-2k+3\right)=0\\ {k}^{2}-6k+9+8k-12=0\\ {k}^{2}+2k-3=0\\ \left(k+3\right)\left(k-1\right)=0\\ k=-3,1\end{array}$

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

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

print('8, 9, 10.')

x = symbols('x', real=True)

class MyTestCase(TestCase):
def test_8_1(self):
a = symbols('a', positive=True)
c = symbols('c', negative=True)
b = symbols('b', real=True)
eq = a * x ** 2 + b * x + c
xs = solve(eq, x)
self.assertEqual(len(xs), 2)

def test_8_2(self):
a = symbols('a', negative=True)
c = symbols('c', positive=True)
b = symbols('b', real=True)
eq = a * x ** 2 + b * x + c
xs = solve(eq, x)
self.assertEqual(len(xs), 2)

def test_9(self):
for a in [-2, 6]:
eq = x ** 2 - a * x + (a + 3)
xs = solve(eq, x)
self.assertEqual(len(xs), 1)

def test_10_1(self):
for k in [-3, 1]:
eq = x ** 2 + (k - 3) * x - (2 * k - 3)
xs = solve(eq, x)
self.assertEqual(len(xs), 1)

def test_10_2(self):
k = Rational(4, 3)
eq = k * x ** 2 + k * x + k - 1
xs = solve(eq, x)
self.assertEqual(len(xs), 1)

p = plot(*[x ** 2 - a * x + (a + 3) for a in [-2, 6]],
*[x ** 2 + (k - 3) * x for k in [-3, 1]],
*[2 * k - 3 for k in [-3, 1]],
Rational(4, 3) * x ** 2 + Rational(4, 3) * x + Rational(4, 3) - 1,
(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(f'sample8.png')

if __name__ == "__main__":
main()


入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample8.py -v
8, 9, 10.
test_10_1 (__main__.MyTestCase) ... ok
test_10_2 (__main__.MyTestCase) ... ok
test_8_1 (__main__.MyTestCase) ... ok
test_8_2 (__main__.MyTestCase) ... ok
test_9 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.558s

OK
%