2016年3月10日木曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 6.(Drawing Geometric Shapes and Fractals)、Programming Challenges #3: Exploring Hénon’s Function, (No. 4103)を取り組んでみる。

Programming Challenges #3: Exploring Hénon’s Function, (No. 4103)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from matplotlib import pyplot as plt

def transformation(p):
    x = p[0]
    y = p[1]
    x1 = y + 1 - 1.4 * x ** 2
    y1 = 0.3 * x
    return x1, y1

def draw_henon_function(n):
    x = [0]
    y = [0]
    x1 = 0
    y1 = 0
    for _ in range(n):
        x1, y1 = transformation((x1, y1))
        x.append(x1)
        y.append(y1)
    return x, y
    
if __name__ == '__main__':
    n = int(sys.argv[1])
    x, y = draw_henon_function(n)
    plt.plot(x, y, 'o')
    plt.title('Exploring Hénon’s Function with {0} points'.format(n))
    plt.savefig('henon_function_{0}.png'.format(n))
    plt.show()

入出力結果(Terminal, IPython)

$ ./sample3.py 10
$ ./sample3.py 100
$ ./sample3.py 1000
$ ./sample3.py 10000
$ ./sample3.py 20000
$

0 コメント:

コメントを投稿