Reputation: 285
I am basically trying to do a power law fit to the following distance and acceleration arrays. However, the power law fit is basically giving me a straight line. Any help would be appreciated on how I can get a true power law relationship.
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()
Upvotes: 2
Views: 353
Reputation: 4647
This combination of data and equation appears to be very sensitive to the initial parameter values for the non-linear solver used in curve_fit(). I have what appears to be a good fit with fitted values of:
c = -2.2896848166160833E-21
c0 = -5.0760537033961146E+00
m = -3.2529524073781118E+01
giving R-squared = 0.899 and RMSE = 5.83.
Upvotes: 2