如何利用小波变换更好地改善非线性回归拟合scipy.ODR公司?

2024-10-06 08:13:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我用电脑有问题scipy.odr公司拟合数据

这是我的数据

https://www.dropbox.com/s/zuo6rqokcxsbnh5/data.txt?dl=0

第一列是数据x,第二列是数据y

我的Python fit代码:

import numpy as np 
import scipy.odr
import matplotlib.pyplot as plt
a = open('C:/Users/san/Desktop/data.txt','r')
data = np.loadtxt(a, delimiter=' ')
a.close()
#data x
f = data[:,0]

#data y
Soddre = data[:,1]

#####fit function#####
def S11fit(beta,f):

    w = 2*np.pi*f
    L1 = 25*1e-3
    L2 = 25*1e-3
    dL = 0.01*1e-3
    z0 = 376.73415
    Z0 = 50
    ZL1 = 50
    c = 2.997925e8
    miu0 = z0/c
    miur = 1
    y0 = 1j*w/c;

    episr = beta[0]+1j*beta[1]+(beta[2]+1j*beta[3])/(1+1j*beta[4]*w)#My target

    ys = 1j*w/c*np.sqrt(episr*miur)
    the = np.sqrt(episr/miur)
    Zin3_1 = np.tanh(ys*L2) 
    Zin3_2 = the*np.tanh(y0*dL) 
    Zin3_3 =  the*( 1+the*np.tanh(y0*dL)*np.tanh(ys*L2) )
    Zin3 = ZL1*( Zin3_1 + Zin3_2 + Zin3_3*np.tanh(y0*L1) )\
    /( Zin3_3 +( Zin3_1 + Zin3_2 )*np.tanh(y0*L1) )
    S11t = (Zin3-Z0)/(Zin3+Z0)

    return S11t.real

#Setting initial beta 
A0r = 1
A0i = 0.02
A0 = A0r+1j*A0i
er1 = 1+0.02
er2 = 1+0.03
B1 = ( er1-er2 )/( 1j*( -w[200]*(er1-A0)+w[300]*(er2-A0) ) )
A1 = (er2-A0)*(1+1j*w[300]*B1)    

#Using scipy.odr to fit data

Model = scipy.odr.Model(S11fit)
Data = scipy.odr.Data(f,Soddre)
odr = scipy.odr.ODR(Data, Model, [A0r, A0i,A1.real, A1.imag, B1.real])
output = odr.run()
resulr = output.pprint()
beta = output.beta
print "ODR", beta

#Fitting result
plt.plot(f, Soddre, "b")
plt.plot(f, S11fit(beta, w), "g--", lw = 1)
plt.tight_layout()
plt.show()

#By fitting data get episr

episr = beta[0]+1j*beta[1]+(beta[2]+1j*beta[3])/(1+1j*beta[4]*w)
plt.plot(f, episr.real, "b")

结果

Beta: [ -5.13825455e+00  -1.08908338e+00   5.46365124e+00  -4.76830313e+01
  -5.13452718e-10]

拟合结果及参考图:

https://www.dropbox.com/s/9yj4ulo57mqgcc2/Fitting%20result.pdf?dl=0

我的拟合曲线不太合适

有没有人有一个很好的建议来拟合数据?你知道吗


Tags: the数据datanppltscipya0real
1条回答
网友
1楼 · 发布于 2024-10-06 08:13:26

我可以调整我的数据好吧。数据需要在Soddre中添加偏差。你知道吗

只需添加:

ca=np.满((1801),0.01)

和修改数据:

数据=scipy.odr.RealData数据库(f,Soddre,sy=ca)

相关问题 更多 >