1

Hi my question is about fitting data in a plot with a smoothed curve.

my x values look as follows:

[  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18 ...60]

my y values look like this:

[ 1. 1.95  2.83  3.65  4.43  5.19  5.94  6.54  7.39  8.01  8.47  9.03 9.55 10.08 10.78 11.24 11.33 11.96 12.29 12.66 13.11 13.59 13.84 14.23 ...]

I defined my function as follows:

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

and plotted my x and y values based on the function above with the library matplotlib and curfit function:

popt, pcov = curve_fit(f.func,x,mean, maxfev=10000)


plt.plot(x, y, ls="none", marker='.')
plt.plot(x, f.func(x, *popt),'-')
plt.show()

However the fitted line starts by - 10 but my data starts by (1,1). enter image description here

Zara Arshad
  • 69
  • 1
  • 5
  • 1
    x^2 is probably not the best curve fit choice - I suspect you'd be better with some exponential-based function or H = (a + sample_size) / (a * sample_size). Alternatively if you find the reciprocal of your data, that ought to linearise it, then fit that instead - the shape is similar to a michaelis menten curve, so https://en.wikipedia.org/wiki/Lineweaver%E2%80%93Burk_plot plotting 1/Hill vs 1/sample_size should work well – Andrew Dec 06 '18 at 14:47
  • Are you sure that your data is quadratic? That looks logarithmic to me. – David Culbreth Dec 06 '18 at 14:47
  • 1
    Your function is quadratic and the data shown is pretty clearly not quadratic. Also why would you use a*x**2 + b*x**2? why would you fit two parameters for the same order term? omitting the b*x**2 will not change anything to the fit you will get so leave that out. – Alexander Vandenberghe Dec 06 '18 at 14:47
  • I just re-read your title... you know your data is logarithmic. Don't use a quadratic approximation, because it is already fundamentally different from your anticipated model – David Culbreth Dec 06 '18 at 14:56
  • I am a beginner in matplotlib, I know that my data is logarithmic, I tried logarithmic functions as well, but they didn't fit to my data. I tried other functions as well and x^2 was the only way where the curve looked almost like I wanted it – Zara Arshad Dec 06 '18 at 15:02
  • Try `np.log10` with base 10 as well – Sheldore Dec 06 '18 at 17:14
  • didn't solved my problem – Zara Arshad Dec 07 '18 at 14:15

0 Answers0