2018年6月19日火曜日

開発環境

Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の6章(幾何図形とフラクタルを描画する)、5.4(プログラミングチャレンジ)、問題6-3(エノンの関数を調べる)を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3
import matplotlib.pyplot as plt
from matplotlib import animation


def henon(x, y):
    return y + 1 - 1.4 * x ** 2, 0.3 * x


def get_points(n):
    x, y = (1, 1)
    xs = [x]
    ys = [y]
    for _ in range(n - 1):
        x, y = henon(x, y)
        xs.append(x)
        ys.append(y)
    return xs, ys


def update(i, xs, ys, p):
    p.set_data(xs[:i], ys[:i])
    if i == len(xs) - 1:
        print(f'完了({i + 1}回)')
    return p


if __name__ == '__main__':
    xs, ys = get_points(1000)
    fig = plt.gcf()
    ax = plt.axes(xlim=(min(xs), max(xs)), ylim=(min(ys), max(ys)))
    p = plt.plot([], [], 'go', markersize=1)[0]
    ani = animation.FuncAnimation(fig, update, fargs=(
        xs, ys, p), frames=int(len(xs)), interval=1, repeat=False)
    print(p, ani, sep='\n')
    ani.save('sample3.gif', writer='imagemagick')
    # plt.show()

入出力結果(Terminal, Jupyter(IPython))

$ ./sample3.py
Line2D(_line0)
<matplotlib.animation.FuncAnimation object at 0x1056f7b00>
完了(1000回)
$

1 コメント :

น้องส้มส้มさんのコメント...

数学の学習は非常に難しいと思っていますが、私の子供は学ぶことができると思います。 数学とは何ですか? それを望む การดูแลสุขภาพ

コメントを投稿