2020年6月7日日曜日

学習環境

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


  1. 正方形のわくを作る針金の長さを x とする。

    このとき 長方形のわくを作る針金の長さは

    34-x

    正方形の面積は

    x42

    2辺の辺の比が

    1:2

    である長方形の面積は

    13·34-x2·23·34-x2=34-x22·32

    正方形と長方形の面積の和は、

    x242+34-x22·32
    =x224+x2-68x+3422·32
    =124·3232x2+23x2-23·68x+3422·32
    =23+3224·32x2-23·6823+32x+3422·32
    =23+3224·32x-23·3423+322-23+3224·32·23+3423+322+3422·32

    よって、 針金を

    23·3423+32=8·3417=16cm34-16=18cm

    に切ったとき、正方形と長方形の面程は最小となり、その値は

    1642+13·182·23·182=16+18=34cm2

    である。

コード

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

print('20.')

a = 34
x = symbols('x')
m = x / 4
n1 = (a - x) / 6
n2 = 2 * (a - x) / 6
s1 = m ** 2
s2 = n1 * n2
s = s1 + s2
colors = ['red', 'green', 'blue', 'brown', 'orange',
          'purple', 'pink', 'gray', 'skyblue', 'yellow']

p = plot(s, 34,
         (x, 0, 40),
         ylim=(10, 50),
         legend=True,
         show=False)
for o, color in zip(p, colors):
    o.line_color = color
p.save('sample20.png')
p.show()

frames = 50


def update_rect(i: int, square: plt.Rectangle, rect: plt.Rectangle):
    if i == 0:
        square.set_y(0)
        square.set_width(a / 4)
        square.set_height(a / 4)
    sw = square.get_width()
    sw -= a / 4 / frames
    l = (a - 4 * sw) / 2 / 3
    square.set_width(sw)
    square.set_height(sw)
    rect.set_width(2 * l)
    rect.set_height(l)
    square.set_y(l)


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

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

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

% ./sample20.py 
20.
%

0 コメント:

コメントを投稿