幂定律适合给出一条直线

时间:2019-04-03 00:18:26

标签: python numpy scipy curve-fitting

我基本上想做一个适合以下距离和加速度阵列的幂定律。但是,幂定律的拟合基本上给了我一条直线。我将如何获得真正的幂律关系,将为您提供任何帮助。

Dis= [0.2065 0.2661 0.2026 0.22   0.2065 0.2661 0.264  0.2173 0.2615 0.2682
 0.407  0.4085 0.409  0.4045 0.405  0.3985 0.5235 0.5846 0.5171 0.5385
 0.6415 0.7661 0.699  0.6523 0.7745 0.7332 0.842  0.9085 0.909  0.8445
 0.84   0.8635]

Acc= [-43.3  -3.  -86.8 -10.5 -56.2  -2.5  -7.2 -12.2  -4.6  -9.  -21.3  -2.
  -3.2  -2.7  -5.8  -6.8 -15.5  -1.8 -22.1  -0.5  -8.7  -0.8   0.   -3.3
  -0.8  -0.8 -12.5  -0.5  -0.7   0.3  -1.   -1.2] 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def f(r, c0, m, c):
    return c0 + r**m * c
data= pd.read_table('/Users/Hrihaan/Desktop/File.txt', dtype=float, header=None, sep='\s+').values
dis=r=data[:,0]
acc=data[:,1]
dis_min=np.min(dis)
dis_max=np.max(dis)
popt, pcov= curve_fit(f, dis, acc, p0 = np.asarray([-1, 10**5, 0]))
rr = np.linspace(dis_min, dis_max, len(dis))
aa = f(rr, *popt)
plt.xlabel('Distance (km)', fontsize=30)
plt.ylabel(' Acceleration (m/s-2)', fontsize=30)
plt.scatter(r, a, c='burlywood', s=10**2)
plt.plot(rr, aa, linewidth=3, label='Power law fit')
plt.show()

1 个答案:

答案 0 :(得分:2)

数据和方程式的这种组合似乎对curve_fit()中使用的非线性求解器的初始参数值非常敏感。我的拟合值似乎很合适:

c = -2.2896848166160833E-21
c0 = -5.0760537033961146E+00
m = -3.2529524073781118E+01

得出R平方= 0.899,RMSE = 5.83。

plot

相关问题