python

时间:2018-03-19 08:08:26

标签: python matlab

我一直在尝试使用python给出x和y值进行曲线拟合,但不幸的是我没有得到 以我想要的方式曲线。我还尝试在matlab中拟合(指数)相同的x,y值,并得到精确的曲线。 问题是python代码返回的系数与matlab返回的系数不同,从而生成不同的曲线。 请帮我找到正确的系数值。 我附上了以下代码。

#CODE
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import math


n=13.75;

x=[0.375,2.125/4.85714,(2.125/5.6667), (2.125/11.33), (2.125/34),0]
y=[0,n/6.111, (n)/3.0555, (n)/2.24489, (n)/2.03708, (n)/1.96428];


#x = np.linspace(0,4,50) # Example data

def func(x, a, b, c, d):
    return a * np.exp(b * x) + c * np.exp(d * x)

#y = func(x, 2.5, 1.3, 0.5, 0.5) # Example exponential data

# Here you give the initial parameters for a,b,c which Python then iterates over
# to find the best fit
popt, pcov = curve_fit(func,x,y,p0=(0.17273307092464,0.050942379680265,0,0.050942379680265), method='trf')

print(popt) # This contains your three best fit parameters

p5 = popt[0] # This is your a
p6 = popt[1] # This is your b
p7 = popt[2] # This is your c
p8 = popt[3] # This is your d



yy=np.linspace(0,(n)/1.96428,50);
xx=p5 * np.exp(p6 * yy) + p7 * np.exp(p8 * yy)
plt.plot(yy,xx)
plt.scatter(y,x, c='b',label='The data points')
plt.show()

0 个答案:

没有答案