0

我在 Python 中绘制了一些实验数据,需要找到适合数据的三次方。我需要这样做的原因是因为三次拟合将用于消除背景(在这种情况下是二极管中的电阻),并且您将留下明显的特征。这是我目前用来首先进行三次拟合的代码,其中 Vnew 和 yone 表示实验数据的数组。

answer1=raw_input ('Cubic Plot attempt?\n ')

    if answer1 in['y','Y','Yes']:


        def cubic(x,A):
            return A*x**3

        cubic_guess=array([40])               
        popt,pcov=curve_fit(cubic,Vnew,yone,cubic_guess)

        plot(Vnew,cubic(Vnew,*popt),'r-',label='Cubic Fit: curve_fit')
        #ylim(-0.05,0.05)   
        legend(loc='best')
        print 'Cubic plotted'
    else:
        print 'No Cubic Removal done'

我有曲线平滑的知识,但仅限于理论上。我不知道如何实现它。我真的很感激任何帮助。

这是到目前为止生成的图表:

4

1 回答 1

0

To make the fitted curve "wider", you're looking for extrapolation. Although in this case, you could just make Vnew cover a larger interval, in which case you'd put this before your plot command:

Vnew = numpy.linspace(-1,1, 256)  # min and max are merely an example, based on your graph
plot(Vnew,cubic(Vnew,*popt),'r-',label='Cubic Fit: curve_fit')

"Blanking out" the feature you see, can be done with numpy's masked arrays but also just by removing those elements you don't want from both your original Vnew (which I'll call xone) and yone:

mask = (xone > 0.1) & (xone < 0.35)  # values between these voltages (?) need to be removed
xone = xone[numpy.logical_not(mask)]
yone = yone[numpy.logical_not(mask)]

Then redo the curve fitting:

popt,_ = curve_fit(cubic, xone, yone, cubic_guess)

This will have fitted only to the data that was actually there (which aren't that many points in your dataset, from the looks of it, so beware!).

于 2014-12-03T22:48:43.187 回答