## 2020年6月5日金曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 2次関数の最大と最小 - 周囲の長さが一定の長方形の対角線の長さの最小値、正方形

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

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

$\frac{20}{2}-x=10-x$

対角線の長さは

$\begin{array}{l}0\le x\le 10\\ \sqrt{{x}^{2}+{\left(10-x\right)}^{2}}\\ =\sqrt{{x}^{2}+100-20x+{x}^{2}}\\ =\sqrt{2{x}^{2}-20x+100}\\ =\sqrt{2}\sqrt{{x}^{2}-10x+50}\\ =\sqrt{2}\sqrt{{\left(x-5\right)}^{2}+25}\end{array}$

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

$\sqrt{2}\sqrt{25}=5\sqrt{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)

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


% ./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)
%