[python] 수식의 가시화 - 포물선(발사체) 운동

 파이썬을 활용하여 포물선(발사체) 운동을 그래프로 표현해 보겠습니다.


초기 속도와 지면과의 각도가 주어지면 아래 공식들을 활용하여 시간에 따른 X, Y 좌표를 구할 수 있습니다. 공식에 대한 자세한 설명은 포물선 운동(Projectile Motion)에 관하여 찾아보시길 바랍니다.


위 수식들을 활용하여 포물선 운동하는 물체의 좌표를 구하는 함수를 정의하고 초기 속도 변화에 따른 포물선 궤적을 가시화해 보겠습니다. 아래는 샘플 코드입니다.


'''
포물선 운동 그래프 생성
'''

from matplotlib import pyplot as plt
import math

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

# floating point range
def frange(start, final, increment):
    numbers = []
    while start < final:
        numbers.append(start)
        start = start + increment

    return numbers

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

    # 체공시간 (Time of flight)
    t_flight = 2 * u * math.sin(theta) / g

    # 시간 인터벌 생성
    intervals = frange(0, t_flight, 0.001)

    # X, Y 좌표 저장
    x = []
    y = []
    for t in intervals:
        x.append(u * math.cos(theta) * t)
        y.append(u * math.sin(theta) * t - 0.5 * g * t * t)

    draw_graph(x, y)

if __name__ == '__main__':
    u_list = [20, 40, 60]
    theta = 45
    for u in u_list:
        draw_trajectory(u, theta)

    plt.legend(['20', '40', '60'])
    plt.show()

아래는 실행결과입니다.


끝.


댓글

이 블로그의 인기 게시물

[PLC] PLC 아날로그 입출력 기본

전력(kW) 계산하기 (직류, 교류 단상, 교류 삼상)

공압 속도 제어: 미터인 vs 미터아웃

제너 다이오드에 저항을 연결하는 이유

회로 차단기 용량 선정하는 방법

PLC 출력 형태

[PLC] 채터링 현상과 입력 필터

NPN, PNP 트랜지스터 차이점

[전기 기초] 저항의 정격전력(Watt) 표기의 의미

[python] 파이썬 pyplot 2차원 그래프 샘플 코드