-2
from scipy import optimize
import matplotlib.pyplot as plt

x_data = np.linspace(0,11,12)
y_data = np.array([0.02471114, 0.02057292, 0.01752668, 0.01494543, 0.01273249, 0.0110999 , 0.00946524, 0.00805622, 0.00670716, 0.00558925, 0.00465331, 0.00387775])

def func(x,a,b):
    return ((a-1)*(-b*x - (0.024711**(1-a)/(1-a))))**(1/(1-a))
popt,pcov = optimize.curve_fit(func, x_data, y_data, p0=[1.1,0.1])

a_opt, b_opt = popt
x_model = np.linspace(min(x_data), max(y_data), 100)
y_model = func(x_model, a_opt, b_opt) 
plt.scatter(x_data, y_data)
plt.plot(x_model, y_model, color='r')
plt.show()

This returns a tiny horizontal line at the coordinates of the first datapoint. What am I doing wrong? I am also getting an OptimizeWarning: Covariance of the parameters could not be estimated error.

3
  • Yes. Commented Jun 26, 2023 at 13:18
  • Thanks for confirming, I have updated the question so that there is actually some content now, I would appreciate a little push in the right direction :) Commented Jun 28, 2023 at 9:28
  • 1
    Try with proper starting parameters: [1.1, -0.1]. If that feels incorrect (e.g., because b is negative), carefully check your equation, as there are many parentheses that you can mix up (split it into parts and temporary variables). Commented Jun 28, 2023 at 10:30

1 Answer 1

0

Probably the error is in x_model = np.linspace(min(x_data), max(y_data), 100) which I guess should really be x_model = np.linspace(min(x_data), max(x_data), 100).

Also, as noted in the comment by @9769953 you should use an initial guess of [1.1, -0.1].

With these changes, the fit looks ok to me.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, much appreciated. I did manage to figure this out myself in the end, after randomly trying to change all parameters until things started to happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.