開発環境
- macOS High Sierra - Apple
- Emacs (Text Editor)
- Python 3.7 (プログラミング言語)
Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の2章(データをグラフで可視化する)、2.6(プログラミングチャレンジ)、問題2-2(2次関数を視覚的に探索する)を取り組んでみる。
コード(Emacs)
Python 3
#!/usr/bin/env python3
import matplotlib.pyplot as plt
def frange(start, stop, step):
res = []
while start < stop:
res.append(start)
start += step
return res
x_values = list(frange(-5, 5, 0.5))
y_values = [x ** 2 + 2 * x + 1 for x in x_values]
for x, y in zip(x_values, y_values):
print(f'x={x} y={y}')
plt.plot(x_values, y_values)
plt.legend(['x ** 2 + 2 * x + 1'])
plt.savefig('sample2.svg')
入出力結果(Terminal, Jupyter(IPython))
$ ./sample2.py x=-5 y=16 x=-4.5 y=12.25 x=-4.0 y=9.0 x=-3.5 y=6.25 x=-3.0 y=4.0 x=-2.5 y=2.25 x=-2.0 y=1.0 x=-1.5 y=0.25 x=-1.0 y=0.0 x=-0.5 y=0.25 x=0.0 y=1.0 x=0.5 y=2.25 x=1.0 y=4.0 x=1.5 y=6.25 x=2.0 y=9.0 x=2.5 y=12.25 x=3.0 y=16.0 x=3.5 y=20.25 x=4.0 y=25.0 x=4.5 y=30.25 $
0 コメント:
コメントを投稿