微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Curve_Fit 不准确

如何解决Curve_Fit 不准确

我试图尽可能好地拟合随时间变化很大的数据。所以首先我平滑了工作正常的数据。我从中得到的平滑数据应该进一步从拟合中表示出来,以得到更多的峰值。正如您在代码中看到的,我想使用 log-tanh 函数来拟合数据。我很清楚这个问题已经在一些线程中出现了,但我已经尝试过,而且数据也不是很小或很大,我知道这也会导致问题。

如您所见,我尝试的多项式拟合也很有效,但它并没有消除所有波浪值。它们对下面的导数造成了非常糟糕的问题。

import tkinter as tk
from tkinter import filedialog
import numpy as np
import scipy.signal
from scipy.optimize import curve_fit
from numpy import diff
import matplotlib.pyplot as plt
from lmfit.models import StepModel,LinearModel



def loghypfunc(x,A,B,C,D,E):
    return A*np.log(1+x)+B*np.tanh(C*x)+D*x+E

def expfunc(t,c0,c1,c2,c3):
    return c0+c1*t-c2*np.exp(-c3*t)

def expdecay(x,a,b,c):
    return a * np.exp(-b * x) + c



path="C:/Users/Sammy/Documents/Masterarbeit WT/CSM und Kriechdaten/Kriechen/Creep_10mN_00008_LC_20210406_2121_DYN.txt"

dataFile = np.loadtxt(path,delimiter='\t',skiprows=2,usecols=(0,1,2,3,29,30),dtype=float)
num_rows,num_cols = dataFile.shape

# time column
time = dataFile[:,[0]].transpose()
time = time.flatten()
refTime = time[0]  # get first time in column (reference)
# genullte Testzeit
timeNull = time - refTime
print("time",time)
flatTimeNull = timeNull.flatten()  # jetzt ein 1D array (one row)
##################################################################################
# indent displacement column
indentdis = dataFile[:,[4]].transpose()
indentdis = indentdis.flatten()
indentdis = indentdis - indentdis[0]
# the indendt data has to be smoothed so there is not such a big fluctuation
indentSmooth = scipy.signal.savgol_filter(indentdis,2001,3)
# null the indent Smooth data
indentSmooth_Null = indentSmooth - indentSmooth[0]
hind_Smooth_flat = indentSmooth_Null.flatten()  # jetzt ein 1D array
print('indent smooth',indentSmooth)
######################################################################

p0 = [100,0.1,100,0.1]
c,cov = curve_fit(expfunc,time,indentSmooth,p0)
y_indent = expfunc(indentSmooth,*c)


p0 = [70,0.5,50,100]
popt,pcov = curve_fit(loghypfunc,p0,maxfev = 10000)
y_indentTan = loghypfunc(indentSmooth,*popt)

modelh_t = np.poly1d(np.polyfit(time,8))

plt.plot(time,'r',label="Data smoothed")
plt.scatter(time,modelh_t(time),s=0.1,label="polyfit")
plt.plot(time,y_indentTan,label="Curve fit Tangens function")
plt.plot(time,y_indent,label="Curve fit exp function")
plt.legend(loc="lower right")
plt.xlabel("time")
plt.ylabel("indent")

plt.show()

这是我从中获取数据的两个数组

time [  6.299596   6.349592   6.399589 ... 608.0109   608.060897 608.110894] 
indent smooth [120.81411822 121.07093706 121.32748184 ... 476.78825661 476.89357473 476.99915287]

这里是情节 Plots

我现在的问题是如何解决它。是因为要拟合的错误优化参数吗?但是我猜python应该自动做到足够好? 我的第二个猜测是数据被定时沿这个轴压缩,因为数组大约有 12000 个值。这可能是一个原因吗?

我将非常感谢任何有关合身的建议。

问候 Hndrx

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。