[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()

아래는 실행결과입니다.


끝.


댓글

이 블로그의 인기 게시물

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

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

[주식] 한국거래소(KRX) 데이터 API 입문 가이드

[PLC] 절연 변압기 (Isolation Transformer)

[수학] 정규 분포란? 왜 종 모양을 띠고 있을까?

[농사] 식물의 광합성과 호흡

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

[자동화] 안쓰는 안드로이드폰을 활용한 식물 성장 타임랩스 촬영

[트레이딩] 현재 주가 기준으로 기대수익률은 어떻게 계산할까?

[PLC] 래더 다이어그램과 PLC