2020年3月12日木曜日

学習環境

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


  1. もとの立方体の一辺の長さを x とすると、

    x + 2 x + 3 x - 4 = x 3 x 2 + 5 x + 6 x - 4 = x 3 x 3 + x 2 - 14 x - 24 = 0 x 2 - 14 x - 24 = 0 x = 7 ± 49 + 24 = 7 ± 73

    また、 高さを4 cm 縮めることから

    x > 4

    なので、 求めるもとの立方体の一辺の長さは

    7 + 73 cm

コード

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

print('4.')


x = symbols('x', positive=True)
f = (x + 2) * (x + 3) * (x - 4)
g = x ** 3
eq = f - g
x0 = 7 + sqrt(73)
y = (7 + sqrt(73)) ** 3


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


p = plot(f, g, y,
         (x, x0 - 0.1, x0 + 0.1),
         ylim=(y - 100, y + 100),
         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'sample4.png')

if __name__ == "__main__":
    main()

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

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

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

OK
%

0 コメント:

コメントを投稿