Python scipy曲线适合早期停止的二次函数

时间:2016-05-04 00:43:32

标签: python numpy matplotlib

因此,我最适合曲线的绘制线在第二点切断,而对于我的生活,我无法找出原因。

from matplotlib import pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
accuracy = 250

def quadratic_function(E,a,b,c):    
    B = (a*(E**2.0)) + (b*E) + c
    return B

Implantation_E = np.array([5.0,10.0,15.0,20.0,25.0])

plt.figure()
plt.title("A plot showing how the Magnetic Field varies with Implantation Energy")
plt.ylabel("Magnetic Field Strength (T)")
plt.xlabel("Implantation Energy (J)")
plt.plot(Implantation_E,D_array[0],'bo')
parameters, var = curve_fit(quadratic_function,Implantation_E,D_array[0], absolute_sigma = True, p0 = (1.0,1.0,1.0))
newTime = np.linspace(0,10,accuracy)
newAsymmetry = quadratic_function(newTime, *parameters)
plt.plot(newTime, newAsymmetry) 
plt.show()

(注D_array [0]:[0.00523265 0.00860683 0.0109838 0.01241191 0.01284149] )

Image of plotted graph

1 个答案:

答案 0 :(得分:0)

您的参数中的原始数据范围为5到25.但您的拟合仅适用于10.将newTime定义更改为25,您就可以了。

from matplotlib import pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
%matplotlib inline

accuracy = 250

def quadratic_function(E,a,b,c):    
    B = (a*(E**2.0)) + (b*E) + c
    return B

dd= np.array([ 0.00523265, 0.00860683 ,0.0109838, 0.01241191 ,0.01284149])
Implantation_E = np.array([5.0,10.0,15.0,20.0,25.0])

plt.plot(Implantation_E,dd,'b-o',lw=15,ms=10,alpha=0.3)

parameters, var = curve_fit(quadratic_function,Implantation_E,dd,absolute_sigma = True, p0 = (1.0,1.0,1.0))
newTime = np.linspace(0,10,accuracy)
newAsymmetry = quadratic_function(newTime, *parameters)

plt.plot(newTime, newAsymmetry,'r.',ms=1) 
plt.show()

enter image description here

...
newTime = np.linspace(3,25,accuracy)
...

enter image description here