2015年11月26日木曜日

開発環境

  • 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 2.(Visualizing Data with Graphs)、Programming Challenges #2: Exploring a Quadratic Function Visually(No. 793)を解いてみる。

#2: Exploring a Quadratic Function Visually(No. 1438)

コード(Emacs)

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

import matplotlib.pyplot as plt

n = 100
x_values = range(-n, n + 1)
y_values = []

def draw_graph(x, y):
    plt.plot(x, y, marker='o')
    plt.title('Exploring a Quadratic Function Visually')
    plt.xlabel('x')
    plt.ylabel('y = x ** 2 + 2 * x + 1')
    
    plt.savefig('sample2.svg')
    
    plt.show()    

if __name__ == '__main__':
    for x in x_values:
        y = x ** 2 + 2 * x + 1
        y_values.append(y)

    draw_graph(x_values, y_values)

入出力結果(Terminal, IPython)

0 コメント:

コメントを投稿