2020年6月5日金曜日

学習環境

代数への出発 (新装版 数学入門シリーズ) (松坂 和夫(著)、岩波書店)の第6章(1次方程式、2次方程式)、5(2次関数の最大と最小)の問18の解答を求めてみる。


  1. 長方形の一辺の長さを x とする。

    このとき、もう1つの辺の長さは

    20 2 - x = 10 - x

    対角線の長さは

    0 x 10 x 2 + 10 - x 2 = x 2 + 100 - 20 x + x 2 = 2 x 2 - 20 x + 100 = 2 x 2 - 10 x + 50 = 2 x - 5 2 + 25

    よって x が5のとき、 すなわち 正方形のときに対角線の長さは最小となり、その値は

    2 25 = 5 2

コード

#!/usr/bin/env python3
import matplotlib.pyplot as plt
from matplotlib import animation
from sympy import symbols, plot, sqrt

print('18.')

a = 20
x = symbols('x')
s = sqrt(x ** 2 + (10 - x) ** 2)
colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']

p = plot(s, 5 * sqrt(2),
         (x, 0, a / 2),
         ylim=(0, a / 2),
         legend=True,
         show=False)
for o, color in zip(p, colors):
    o.line_color = color
p.save('sample18.png')
p.show()


def update_rect(i, rect):
    rect.set_width((0.2 * i))
    rect.set_height((a / 2 - 0.2 * i))
    plt.plot([0, 0.2 * i], [0, a / 2 - 0.2 * i])
    return rect


fig = plt.gcf()
ax = plt.axes(xlim=(0, a / 2), ylim=(0, a / 2), aspect='equal')
rect = plt.Rectangle((0, 0), 0, a / 4)
ax.add_patch(rect)

anim = animation.FuncAnimation(fig, update_rect,
                               fargs=(rect,),
                               frames=50,
                               interval=100,
                               repeat=True)
plt.show()
anim.save('sample18.gif', writer='imagemagick')

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

% ./sample18.py
18.
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/plotting/plot.py:1065: MatplotlibDeprecationWarning: 
The set_smart_bounds function was deprecated in Matplotlib 3.2 and will be removed two minor releases later.
  self.ax[i].spines['left'].set_smart_bounds(True)
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sympy/plotting/plot.py:1066: MatplotlibDeprecationWarning: 
The set_smart_bounds function was deprecated in Matplotlib 3.2 and will be removed two minor releases later.
  self.ax[i].spines['bottom'].set_smart_bounds(False)
%

0 コメント:

コメントを投稿