拟合

import numpy as np
from scipy.optimize import curve_fit,minimize
import matplotlib.pyplot as plt

# 定义 MultivariateJacobianFunction
def multivariate_jacobian_function(params, x_data):
    A, b, c = params
    predicted = A * np.sin(b * x_data + c)
    return predicted

# 定义损失函数
def loss_function(params, x_data, y_data):
    predicted = multivariate_jacobian_function(params, x_data)
    residuals = y_data - predicted
    return np.sum(residuals**2)
# 定义要拟合的函数形式
def sine_function(x, A, b, c):
    return A *np.sin(b * x + c)

# 生成一些示例数据
# x_data=np.arange(1, 91,4)
x_data = np.array([0.0, 4500.0, 5250.0, 6000.0, 6750.0, 7500.0, 8250.0, 9000.0, 9750.0, 10500.0, 11250.0, 12000.0, 12750.0, 13500.0, 14250.0, 15000.0, 15750.0, 16500.0, 17250.0, 18000.0, 18750.0, 19500.0, 20250.0, 21000.0, 21750.0, 22500.0, 23250.0, 24000.0, 24750.0, 25500.0, 26250.0, 27000.0, 27750.0, 28500.0, 29250.0, 30000.0, 30750.0, 31500.0, 32250.0, 33000.0, 33750.0, 34500.0, 35250.0, 36000.0, 36750.0, 37500.0, 38250.0, 39000.0, 39750.0, 40500.0, 41250.0, 42000.0, 42750.0, 43500.0, 44250.0, 45000.0, 45750.0, 46500.0, 47250.0, 48000.0, 48750.0, 49500.0, 50250.0, 51000.0, 51750.0, 52500.0, 53250.0, 54000.0, 54750.0, 55500.0, 56250.0, 57000.0, 57750.0, 58500.0, 59250.0, 60000.0, 60750.0, 61500.0, 62250.0, 63000.0, 63750.0, 64500.0, 65250.0, 66000.0, 66750.0, 67500.0, 68250.0, 69000.0, 69750.0, 70500.0, 71250.0, 72000.0, 72750.0, 73500.0, 74250.0, 75000.0, 75750.0, 76500.0, 77250.0, 78000.0])
# x_data = x_data%360
y_data = np.array([1.4000000000000001,  1.157, 1.09, 1.016, 0.935, 0.849, 0.758, 0.662, 0.562, 0.458, 0.352, 0.243, 0.133, 0.022, -0.089, -0.199, -0.309, -0.41600000000000004, -0.521, -0.622, -0.72, -0.8130000000000001, -0.901, -0.984, -1.061, -1.131, -1.194, -1.25, -1.298, -1.338, -1.37, -1.393, -1.408, -1.414, -1.411, -1.4000000000000001, -1.3800000000000001, -1.352, -1.315, -1.27, -1.217, -1.157, -1.09, -1.016, -0.935, -0.849, -0.758, -0.662, -0.562, -0.458, -0.352, -0.243, -0.133, -0.022, 0.089, 0.199, 0.309, 0.41600000000000004, 0.521, 0.622, 0.72, 0.8130000000000001, 0.901, 0.984, 1.061, 1.131, 1.194, 1.25, 1.298, 1.338, 1.37, 1.393, 1.408, 1.414, 1.411, 1.4000000000000001, 1.3800000000000001, 1.352, 1.315, 1.27, 1.217, 1.157, 1.09, 1.016, 0.935, 0.849, 0.758, 0.662, 0.562, 0.458, 0.352, 0.243, 0.133, 0.022, -0.089, -0.199, -0.309, -0.41600000000000004, -0.521, -0.622])

# 初始猜测参数值
initial_guess = (1.5, 2*np.pi/78000, 1.0)

# 最小化损失函数以拟合数据
result = minimize(loss_function, initial_guess, args=(x_data, y_data), method='Nelder-Mead')

# 提取拟合后的参数
fitted_params = result.x

Aa, bb, cc = fitted_params
print(fitted_params)

# 使用 curve_fit 函数拟合数据
params, params_covariance = curve_fit(sine_function, x_data, y_data,p0=initial_guess)

# 提取拟合后的参数
A, b, c = params

print(params)
# 生成拟合后的曲线数据
x_fit = np.linspace(min(x_data), max(x_data), 100)

# print(x_fit)
y_fit = sine_function(x_fit, A, b, c)

z_fit = sine_function(x_fit,Aa,bb,cc)
print(y_fit)

# 绘制原始数据和拟合曲线
plt.scatter(x_data, y_data, label='Data')
plt.plot(x_fit, y_fit, 'r', label='Fit')
# plt.plot(x_fit, z_fit, 'b', label='Fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

print("Fitted Parameters:")
print("A:", A)
print("b:", b)
print("c:", c)
posted @ 2023-11-10 16:44  开花石头  阅读(97)  评论(0)    收藏  举报