0
import numpy as np
import matplotlib.pyplot as plt

points = np.array([
 (333, 195.3267),
 (500, 223.0235),
 (1000, 264.5914),
 (2000, 294.8728),
 (5000, 328.3523),
 (10000, 345.4688)
])

# get x and y vectors
x = points[:,0]
y = points[:,1]

What would be my next steps in order to create an exponential curve fit the graph?

Ivan
  • 34,531
  • 8
  • 55
  • 100
  • 1
    [How to do exponential and logarithmic curve fitting in Python](https://stackoverflow.com/questions/3433486/how-to-do-exponential-and-logarithmic-curve-fitting-in-python-i-found-only-poly) – Ben Dumoulin Jul 10 '18 at 16:17
  • Possible duplicate of [How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting](https://stackoverflow.com/questions/3433486/how-to-do-exponential-and-logarithmic-curve-fitting-in-python-i-found-only-poly) –  Jul 10 '18 at 17:33

1 Answers1

1

Here is an example of fitting the data to a logarithmic quadratic equation that fits the data somewhat better than an exponential, and plots the fitted curve against a scatterplot of the raw data. The code is not optimal, for example it repeatedly takes the log of X rather than just doing it one time. The log(x) data can be more efficiently fit by directly using a linear fitting method as well, however here you can replace the fitted equation with an exponential much more easily with less code changes.

import numpy
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

points = numpy.array([(333, 195.3267), (500, 223.0235), (1000, 264.5914), (2000, 294.8728
), (5000, 328.3523), (10000, 345.4688)])
# get x and y vectors
xData = points[:,0]
yData = points[:,1]

# function to be fitted
def LogQuadratic(x, a, b, c):
    return a + b*numpy.log(x) + c*numpy.power(numpy.log(x), 2.0)


# some initial parameter values
initialParameters = numpy.array([1.0, 1.0, 1.0])

fittedParameters, pcov = curve_fit(LogQuadratic, xData, yData, initialParameters)

# values for display of fitted function
a, b, c = fittedParameters

# for plotting the fitting results
xPlotData = numpy.linspace(min(xData), max(xData), 50)
y_plot = LogQuadratic(xPlotData, a, b, c)

plt.plot(xData, yData, 'D') # plot the raw data as a scatterplot
plt.plot(xPlotData, y_plot) # plot the equation using the fitted parameters
plt.show()

print('fitted parameters:', fittedParameters)
James Phillips
  • 4,526
  • 3
  • 13
  • 11