## 2020年5月27日水曜日

### 数学 - Python - 代数学 - 1次関数、2次関数 - 1次関数とそのグラフ - 絶対値、場合分け

1. $\begin{array}{l}y=\left|x-1\right|\\ =\left\{\begin{array}{l}-x+1\left(x\le 1\right)\\ x-1\left(x\ge 1\right)\end{array}\end{array}$

グラフ。

2. $\begin{array}{l}y=\left|x-1\right|+\left|x+1\right|\\ =\left\{\begin{array}{l}-x+1-x-1\left(x\le -1\right)\\ -x+1+x+1\left(-1\le x\le 1\right)\\ x-1+x+1\left(1\ge x\right)\end{array}\\ =\left\{\begin{array}{l}-2x\left(x\le -1\right)\\ 2\left(-1\le x\le 1\right)\\ 2x\left(1\ge x\right)\end{array}\end{array}$

グラフ。

3. $\begin{array}{l}y=\frac{\left|x\right|+x}{2}\\ =\left\{\begin{array}{l}\frac{-x+x}{2}\left(x\le 0\right)\\ \frac{x+x}{2}\left(x\ge 0\right)\end{array}\\ =\left\{\begin{array}{l}0\left(x\le 0\right)\\ x\left(x\ge 0\right)\end{array}\end{array}$

グラフ。

4. $\begin{array}{l}y=\left|x-1\right|+\left|2x+4\right|-\left|2x-4\right|\\ =\left\{\begin{array}{l}-x+1-2x-4+2x-4\left(x\le -2\right)\\ -x+1+2x+4+2x-4\left(-2\le x\le 1\right)\\ x-1+2x+4+2x-4\left(1\le x\le 2\right)\\ x-1+2x+4-2x+4\left(x\ge 2\right)\end{array}\\ =\left\{\begin{array}{l}-x-7\left(x\le -2\right)\\ 3x+1\left(-2\le x\le 1\right)\\ 5x-1\left(1\le x\le 2\right)\\ x+7\left(x\ge 2\right)\end{array}\end{array}$

グラフ。

コード

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

print('7.')
x = symbols('x')

p = plot(abs(x - 1),
abs(x - 1) + abs(x + 1),
(abs(x) + x) / 2,
abs(x - 1) + abs(2 * x + 4) - abs(2 * x - 4),
(x, -10, 10),
ylim=(-10, 10),
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.show()
p.save('sample7.png')


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