## 2020年6月4日木曜日

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

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

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

$\frac{a}{2}-x$

よって長方形の面積は

$\begin{array}{l}S=x\left(\frac{a}{2}-x\right)\\ =-{x}^{2}+\frac{a}{2}x\\ =-\left({x}^{2}-\frac{a}{2}x\right)\\ =-{\left(x-\frac{a}{4}\right)}^{2}+\frac{{a}^{2}}{16}\end{array}$

よって、 長方形は2つの辺の長さが

$\frac{a}{4},\frac{a}{2}-\frac{a}{4}=\frac{a}{4}$

すなわち正方形のとき、最大値

$\frac{{a}^{2}}{16}$

となる。

コード

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

print('17.')

a = 10
x = symbols('x')
s = x * (Rational(a, 2) - x)

colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

p = plot(s, Rational(a ** 2, 16),
(x, 0, 10),
ylim=(0, 10),
legend=True,
show=False)
for o, color in zip(p, colors):
o.line_color = color
p.save('sample17.png')
p.show()

def update_rect(i, rect):
rect.set_width((0.1 * i))
rect.set_height((a / 2 - 0.1 * i))
return rect

fig = plt.gcf()
ax = plt.axes(xlim=(0, a / 2), ylim=(0, a / 2))
rect = plt.Rectangle((0, 0), 0, a / 2)
anim = animation.FuncAnimation(fig, update_rect,
fargs=(rect,),
frames=50,
interval=100,
repeat=True)
plt.show()
anim.save('sample17.gif', writer='imagemagick')


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