Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I am trying to create a curve fit for the data in "time" and "volts" np arrays. When I try and fit the data I get the error "covariance of parameters could not be estimated". If I put in other data points though it works. I am not sure what could be going wrong here.
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame
time = np.array([100,80,70,60,55,50,45,40,35,30,25,22,20])
volts = np.array([6.28,6.04,5.72,5.32,5.08,4.80,4.44,3.96,3.40,
2.80,2.01,1.40,1.01])
## Defining exp func for curve fit
def func(x, a ,b):
return a * (1 - 2*np.exp(-b * x))
params, extras = curve_fit(func, time, volts)
print(params)
plt.scatter(time, volts)
x_data = np.arange(5,110)
plt.plot(x_data, params[0]*(1-2*np.exp(-params[1]*x_data)))
plt.show()
enter image description here
enter image description here
For example if I use the data:
volts = np.array([2.56, 2.54, 2.52, 2.46, 2.4, 2.38, 2.30, 2.26, 2.18, 2.08, 1.95, 1.80, 1.68, 1.44, 1.22, .9, .46])
time = np.array([])
for i in range(100, 15, -5):
time = np.append(time, i)
Then the fit works great.
you need to give realistic starting values for a
and b
to curve_fit()
. The default is (completely idiotic) to use values of 1.0 for all parameters, without warning. For your data, this is a pretty bad guess, and will cause the fit to fail. Trying better starting points like
params, extras = curve_fit(func, time, volts, [5.0, 0.1])
will work much better....
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.