[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 아날로그 입출력 기본

NPN, PNP 트랜지스터 차이점

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

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

3선 결선식 센서의 타입 PNP, NPN

[아두이노] 가변저항(Potential Divider)과 전압분배(Voltage Divider)

3상 모터 전력에서 전류 계산하기 (How to Convert Three-Phase Power to Amps)

커패시터에 저장된 에너지 계산

[공압밸브] 5포트 2웨이 & 4포트 2웨이, 단동 VS 복동 차이점

SSR과 기계식 릴레이 차이점