2015年11月27日金曜日

開発環境

  • 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 #3: Enhanced Projectile Trajectory Comparison(No. 1454)を解いてみる。

#3: Enhanced Projectile Trajectory Comparison(No. 1438)

コード(Emacs)

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

import matplotlib.pyplot as plt
import math

def frange(start, final, increment):
    numbers = []
    while start < final:
        numbers.append(start)
        start += increment
    return numbers

def draw_graph(x, y):
    plt.plot(x, y)
    plt.xlabel('x-coordinate')
    plt.ylabel('y-coordinate')
    plt.title('Projectile motions of a ball')

def draw_trajectory(u, theta):
    theta = math.radians(theta)
    g = 9.8

    t_flight = 2 * u * math.sin(theta / g)
    intervals = frange(0, t_flight, 0.001)
    x = []
    y = []
    for t in intervals:
        x.append(u * math.cos(theta) * t)
        y.append(u * math.sin(theta) * t - 0.5 * g * t ** 2)
    draw_graph(x, y)
    
if __name__ == '__main__':
    velocities = []
    angles = []
    legends = []
    g = 9.8
    try:
        n = int(input('how many?: '))
        for i in range(n):
            velocity = float(input(
                'Enter the initial velocity for trajectory ' + \
                '{0} (m/s): '.format(i + 1)))
            angle = math.radians(float(input(
                'Enter the angle of projection for trajectory ' + \
                '{0} (degrees): '.format(i + 1))))
            velocities.append(velocity)
            angles.append(angle)
        for velocity, angle in zip(velocities, angles):
            t_flight = 2 * velocity *  math.sin(angle / g)
            max_x = velocity * math.cos(angle) * t_flight
            max_y = velocity * math.sin(angle) * t_flight / 2 - \
                    0.5 * g * (t_flight / 2) ** 2
            print('velocity: {0}, angle: {1}, time: {2}, max horizontal: {3},' \
                  'max vertical: {4}'.format(
                    velocity, angle, t_flight, max_x, max_y))
            draw_trajectory(velocity, angle)
            legends.append('u: {0}, theta: {1}'.format(
                velocity, math.degrees(angle)))
            
    except Exception as err:
        print(err)
    else:
        plt.legend(legends)
        plt.savefig('sample3.svg')
        plt.show()

入出力結果(Terminal, IPython)

$ ./sample3.py
how many?: 3
Enter the initial velocity for trajectory 1 (m/s): 45
Enter the angle of projection for trajectory 1 (degrees): 45
Enter the initial velocity for trajectory 2 (m/s): 60
Enter the angle of projection for trajectory 2 (degrees): 45
Enter the initial velocity for trajectory 3 (m/s): 45
Enter the angle of projection for trajectory 3 (degrees): 90
velocity: 45.0, angle: 0.7853981633974483, time: 7.205121593108402, max horizontal: 229.2655651992257,max vertical: 51.03840556455357
velocity: 60.0, angle: 0.7853981633974483, time: 9.606828790811202, max horizontal: 407.5832270208457,max vertical: 90.73494322587302
velocity: 45.0, angle: 1.5707963267948966, time: 14.363990553004129, max horizontal: 3.957933387086849e-14,max vertical: 70.44261229927284
$

0 コメント:

コメントを投稿

Comments on Google+: