curve-fit

import公式

from scipy.optimize import curve_fit
x,y = np.loadtxt("mystery1.csv",delimiter=",",unpack=True)

def test_func( m, a, b, c , d , e ):

    return a*m + b*m**2 + c*m + d*np.sin(e*m)

return一个像上面的函数,比如sin,重点是d之后的

   popt, pcov = curve_fit (test_func, x , y ,  p0 = [0.0556,  1 , 4 , 2, 3 ])

(函数,x,y,po=四个)

   a_expected = popt[0]    
   b_expected = popt[1]    
   c_expected = popt[2]
   d_expected = popt[3]
   e_expected = popt[4]
   
   x_model = np.linspace(-4 , 4 , 200)
   y_2 =  test_func( x_model , a_expected, b_expected, c_expected ,       d_expected  ,  e_expected)
   
   plt.plot(x_model  , y_2 , label = " fit using the curve  thing " , color = "green")
   plt.scatter(x,y, label = "the picture from data ")
   
   plt.legend()
   plt.xlabel("x")
   plt.ylabel("y")
   plt.title(" the realtionship between x and y ")

example 2
𝑦=𝐴∗𝑠𝑖𝑛(𝐵𝑥+𝐶)

> def my_sin_function(x, A, B, C):
      return A * np.sin(B * x + C)
      
# Use curve_fit by passing in the sin function, x data, and y data
popt, pcov = curve_fit(my_sin_function, x_sin, y_sin)

# Print the array of fitted parameter values
print(popt)

a_expected = popt[0]  # get fitted A value
b_expected = popt[1]  # get fitted B value
c_expected = popt[2]  # get fitted C value

# use my_sin_function with new parameters to get expected y values
y_sin_expected = my_sin_function(x_sin, a_expected, b_expected, c_expected)


# plot our actual data
plt.scatter(x_sin, y_sin, label = "data")

# plot our fitted curve
plt.plot(x_sin, y_sin_expected, color = "orange", label = "fit")

plt.legend()
plt.xlabel('x')
plt.ylabel('y')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值