2020年3月15日日曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第4章(1次方程式, 2次方程式 )、練習問題7の解答を求めてみる。


  1. x - a x - b = c 2 x 2 - a + b x + a b - c 2 = 0

    この2次方程式の判別式は、

    D = a + b 2 - 4 a b - c 2 = a 2 + 2 a b + b 2 - 4 a b + 4 c 2 = a 2 - 2 a b + b 2 + 4 c 2 = a - b 2 + 4 c 2 0

    よって、必ず実数解をもつ。

    (証明終)

コード

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

print('7.')


x, a, b, c = symbols('x, a, b, c', real=True)
eq = (x - a) * (x - b) - c ** 2


class MyTestCase(TestCase):
    def test(self):
        self.assertGreaterEqual(len(solve(eq, x)), 1)


p = plot(*[(x - random.randrange(-5, 6)) * (x - random.randrange(-5, 6))
           - random.randrange(-10, 11) ** 2
           for _ in range(10)],
         (x, -50, 50),
         ylim=(-50, 50),
         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'sample7.png')


if __name__ == "__main__":
    main()

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

% ./sample7.py -v
7.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.364s

OK
%

0 コメント:

コメントを投稿