2020年5月11日月曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第5章(連立方程式と高次方程式) 、問7の解答を求めてみる。


  1. 長方形の2辺の長さをそれぞれ x、 y とする。

    このとき、

    { 2 x + y = 42 x 2 + y 2 = 1 5 2 x 2 + y 2 = 225 x + y = 21 y = 21 - x x 2 + 21 - x 2 = 225 2 x 2 - 42 x + 216 = 0 x 2 - 21 x + 108 = 0 x = 21 ± 441 - 432 2 = 21 ± 3 2 = 9 , 12 y = 12 , 9

    よって、求める長方形の2辺の長さは9 cm、 12 cm。

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, solve, Rational, sqrt, sin, cos, pi, plot
from sympy.plotting import plot_parametric

print('7.')

a, b = symbols('a:b', real=True)


class TestRectangle(TestCase):
    def test(self):
        self.assertEqual(
            solve([2 * (a + b) - 42, sqrt(a ** 2 + b ** 2) - 15]),
            [{a: 9, b: 12},
             {a: 12, b: 9}],
        )


r = Rational(15, 2)
t = symbols('t')
p = plot_parametric(r * cos(t), r * sin(t),
                    (t, 0, 2 * pi),
                    xlim=(-10, 10),
                    ylim=(-10, 10),
                    show=False)
p0 = plot(*[s * o
            for o in [Rational(9, 2), Rational(12, 2)]
            for s in [-1, 1]],
          legend=True,
          show=False)
for o in p0:
    p.append(o)

colors = ['red', 'green', 'blue', 'brown', 'orange', 'pink']

for o, color in zip(p, colors):
    o.line_color = color

p.show()
p.save('sample7.png')

if __name__ == "__main__":
    main()

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

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

----------------------------------------------------------------------
Ran 1 test in 0.274s

OK
%

0 コメント:

コメントを投稿