## 2020年6月2日火曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 2次関数の最大と最小 - 定義域

1. $\begin{array}{l}-5\le x\le 0\\ y={x}^{2}+4x\\ ={\left(x+2\right)}^{2}-4\end{array}$

x が-5 のとき最大値

$25-20=5$

x が-2のとき最小値-4。

2. $\begin{array}{l}y={\left(x-2\right)}^{2}+1\\ x=-2,y=17\\ x=1,y=2\end{array}$

3. $\begin{array}{l}y=-2\left({x}^{2}-3x\right)-\frac{9}{2}\\ =-2{\left(x-\frac{3}{2}\right)}^{2}\\ x=\frac{3}{2},y=0\\ x=-1,y=-\frac{25}{2}\end{array}$

4. $\begin{array}{l}y=-\frac{1}{2}\left({x}^{2}+\frac{5}{2}x\right)+3\\ =-\frac{1}{2}{\left(x+\frac{5}{4}\right)}^{2}+\frac{25}{32}+3\\ =-\frac{1}{2}{\left(x+\frac{5}{4}\right)}^{2}+\frac{121}{32}\\ x=-\frac{5}{4},y=\frac{121}{32}\\ x=2,y=-2-\frac{5}{2}+3=-\frac{3}{2}\end{array}$

コード

#!/usr/bin/env python3
from sympy import symbols, plot, Rational

print('15.')
x = symbols('x')
ys = [x ** 2 + 4 * x,
x ** 2 - 4 * x + 5,
-2 * x ** 2 + 6 * x - Rational(9, 2),
-x ** 2 / 2 - 5 * x / 4 + 3]
maxs = [5, 17, 0, Rational(121, 32)]
mins = [-4, 2, -Rational(25, 2), -Rational(3, 2)]
intervals = [(x, a, b) for a, b in [(-5, 0),
(-2, 1),
(-1, Rational(3, 2)),
(-Rational(5, 2), 2)]]
for i, (y, mi, ma, interval) in enumerate(zip(ys, mins, maxs, intervals), 1):
p = plot(y, mi, ma,
interval,
ylim=(min(mins), max(maxs)),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color
p.save(f'sample15_{i}.png')

p.show()


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