์๋๋ ํ์ด์ฌ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ผ๊ฐํจ์๋ก ๊ทผ์ฌ์ํค๋ ์ฝ๋์
๋๋ค.
ํ์ด์ฌ ๋ค๋ฃฐ ์ค ์์๋ ์ ์๋์ด ๊ณ์๊ฑฐ๋ ํ์์ด ์์ผ๋ฉด ์ด์ฉํ์๋ฉด ์ข๊ฒ ์ต๋๋ค. (์ ์๊ถ์ ์ฐจํญ๊ท์๊ฒ ์์ต๋๋ค. ๊ณต์ ์ ์ด ์นํ์ด์ง ๋งํฌ๋ก ๊ณต์ ํด์ฃผ์ธ์)
import numpy as np
import matplotlib.pyplot as plt
# ์ฃผ์ด์ง ๋ฐ์ดํฐ
xv = np.array([n for n in range(1,61)])
yv = np.array([
57, 70, 82, 68, 52, 47, 42, 40,40,40,40,30.9, 40, 40, 43,
58, 70, 82, 67, 52, 45, 42, 40,40,40,40,30.9, 40, 40, 42,
56.5, 70, 82, 68, 52, 45, 42, 40,40,40,39,30.9, 40, 40, 43,
57, 70, 82, 68, 52, 45, 42, 40,40,38,39,30.9, 40, 40, 44
])
# ์ฃผ์ด์ง ํจ์ ํํ๋ก fittingํ๊ธฐ ์ํด sin(x) ๊ฐ์ ๊ตฌํจ
sin_xv = np.sin(xv)
# ์ฃผ์ด์ง ํจ์ ํํ๋ก fitting์ ์ํ ํ๋ ฌ ์์ฑ (range(1,32) : sinx(x)์ ๋ํ 31์ฐจ ๋คํญ์์ผ๋ก ๊ทผ์ฌํ๋ค๋ ๋ป)
X = np.column_stack([sin_xv**n for n in range(1, 32)])
# ๋คํญ์ ๊ณ์ ๊ณ์ฐ
coeff_fitting= np.linalg.lstsq(X, yv, rcond=None)[0]
print("Fitted coefficients:", coeff_fitting)
# fitting๋ ํจ์ ์์ฑ
def custom_function(x):
return sum(coeff * np.sin(x)**n for n, coeff in enumerate(coeff_fitting, start=1))
# ๋ฐ์ดํฐ์ fitting๋ ํจ์ ์๊ฐํ
plt.scatter(xv, yv, label='Data')
x_vals = np.linspace(1, 59, 30) # ๊ทธ๋ํ๋ฅผ ๋ถ๋๋ฝ๊ฒ ๋ง๋ค๊ธฐ ์ํด x ๊ฐ ๋ฒ์ ์ง์
plt.plot(x_vals, custom_function(x_vals), color='red', label='Fitted Sine Curve')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Height of Wave according to Time')
plt.grid(True)
plt.show()