## 2020年3月13日金曜日

### 数学 - Python - 代数学 - 1次方程式, 2次方程式 - 線分、内分点、比、解

1. AC の長さを x とすると、

$\begin{array}{l}0\le x\le 1\\ AB:AC=AC:CB\\ 1:x=x:\left(1-x\right)\\ 1-x={x}^{2}\\ {x}^{2}+x-1=0\\ x=\frac{-1+\sqrt{1+4}}{2}\\ =\frac{-1+\sqrt{5}}{2}\mathrm{cm}\end{array}$

コード

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

print('5.')

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

eq = x ** 2 + x - 1
x0 = (-1 + sqrt(5)) / 2

class MyTestCase(TestCase):
def test(self):
xs = [t for t in solve(eq, x) if 0 <= t <= 4]
self.assertEqual(len(xs), 1)
self.assertEqual(xs[0], x0)

p = plot(eq, eq.subs({x: x0}),
(x, -5, 5),
ylim=(-5, 5),
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'sample5.png')

if __name__ == "__main__":
main()


% ./sample5.py -v
5.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.315s

OK
%