반응형

ChatGPT 4.0이 나왔다는 소식을 듣고,

성능이 궁금해 20달러 투척하고 유료 가입했습니다. 

 

평소 궁금했던 것들 이것저것 해보고 있는데,

이런 것도 가능한가 싶어서 단진자 운동을 시각화하는 파이썬 코드를 생성해달라고 했습니다.  

 

결과는 성공적 ㅎㅎ

ChatGPT Prompt : 

파이썬을 이용해 중력을 받는 단진자가 대략 70도 각도로 좌우로 움직이는 모습을 시각화하는 코드를 만들어줘. matplotlib과 FuncAnimation을 이용해서 작성해줘.

 

 

Python Code : 

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# 단진자의 파라미터
g = 9.81  # 중력 가속도 (m/s^2)
L = 1.0   # 단진자의 길이 (m)
theta_0 = np.deg2rad(45)  # 초기 각도 (45도를 라디안으로 변환)

# 시간 변수
t = np.linspace(0, 10, 1000)  # 시간 범위 (0초부터 10초까지)

# 단진자의 움직임을 계산하는 함수
def pendulum_motion(t, theta_0, L, g):
    return theta_0 * np.cos(np.sqrt(g / L) * t)

# 시각화를 위한 그림 및 축 설정
fig, ax = plt.subplots()
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal', adjustable='box')

# 단진자를 표현하는 선과 점
line, = ax.plot([], [], lw=2)
dot, = ax.plot([], [], 'o', markersize=10, color='red')

# 애니메이션을 위한 초기화 함수
def init():
    line.set_data([], [])
    dot.set_data([], [])
    return line, dot,

# 애니메이션을 위한 업데이트 함수
def update(frame):
    theta = pendulum_motion(t[frame], theta_0, L, g)
    x = L * np.sin(theta)
    y = -L * np.cos(theta)
    line.set_data([0, x], [0, y])
    dot.set_data(x, y)
    return line, dot,

# 애니메이션 실행
ani = FuncAnimation(fig, update, frames=len(t), init_func=init, blit=True, interval=20)
plt.show()
반응형

+ Recent posts