
import numpy as np
import matplotlib.pyplot as plt

# 1. 시간 설정 (0~4일)
t = np.linspace(0, 4, 401)

# 2. 가상의 식물 생장량 함수
# N(t) = 10 + ((t-2)^3 + 8)/3 + t
def growth(t):
    return 10 + ((t - 2)**3 + 8) / 3 + t

# 3. 순간 생장 속도 함수: N'(t)
def growth_rate(t):
    return (t - 2)**2 + 1

# 4. 비교할 두 시점
t1 = 1
t2 = 3

N1 = growth(t1)
N2 = growth(t2)

R1 = growth_rate(t1)
R2 = growth_rate(t2)

# 5. 결과 출력
print("===== 생장 상태 비교 =====")
print(f"{t1}일째 생장량: {N1:.2f}")
print(f"{t2}일째 생장량: {N2:.2f}")
print()
print(f"{t1}일째 순간 생장 속도: {R1:.2f}")
print(f"{t2}일째 순간 생장 속도: {R2:.2f}")
print()
print(f"생장량 차이: {abs(N2 - N1):.2f}")
print(f"생장 속도 차이: {abs(R2 - R1):.2f}")

# 6. 그래프 그리기
fig, axes = plt.subplots(1, 2, figsize=(12, 4.5))

# 생장량 그래프
axes[0].plot(t, growth(t), label="Growth amount N(t)")
axes[0].scatter([t1, t2], [N1, N2], color="red", zorder=3)
axes[0].annotate(f"t=1, N={N1:.2f}", (t1, N1),
                 xytext=(5, 10), textcoords="offset points")
axes[0].annotate(f"t=3, N={N2:.2f}", (t2, N2),
                 xytext=(5, 10), textcoords="offset points")
axes[0].set_title("Plant Growth Amount")
axes[0].set_xlabel("Time (days)")
axes[0].set_ylabel("Growth amount (arbitrary units)")
axes[0].grid(True)
axes[0].legend()

# 순간 생장 속도 그래프
axes[1].plot(t, growth_rate(t), label="Growth rate N'(t)")
axes[1].scatter([t1, t2], [R1, R2], color="red", zorder=3)
axes[1].annotate(f"t=1, rate={R1:.1f}", (t1, R1),
                 xytext=(5, 10), textcoords="offset points")
axes[1].annotate(f"t=3, rate={R2:.1f}", (t2, R2),
                 xytext=(5, 10), textcoords="offset points")
axes[1].set_title("Instantaneous Growth Rate")
axes[1].set_xlabel("Time (days)")
axes[1].set_ylabel("Growth rate (units/day)")
axes[1].grid(True)
axes[1].legend()

plt.tight_layout()
plt.show()
