You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
764 B

4 years ago
  1. import matplotlib.pyplot as plt
  2. from scipy.optimize import curve_fit
  3. import numpy as numpy
  4. #Fitting funtion
  5. def func(x, a, b)
  6. return a*np.exp(b*x)
  7. #return a*x+b
  8. #Experimental x and y data points
  9. xData = np.array([1,2,3,4,5])
  10. yData = np.array([1, 9, 50, 300, 1500])
  11. #Plot experimental data points
  12. plt.plot(xData, yData, 'bo', label='experimeatal-data')
  13. # Initial guess for the parameters
  14. #initialGuess = [1, 0, 1, 0]
  15. #Perform the curve-fit
  16. popt, pcov = curve_fit(func, xData, yData, initialGuess)
  17. print(popt)
  18. #x values for the fitted function
  19. xFit = np.arange(0.0, 5.0, 0.01)
  20. #Plot the fitted function
  21. plt.plot(xFit, func(xFit, *popt), 'r', label= 'fit params: a= %5.3f, b=%5.3f' %tuple(popt))
  22. plt.xlabel('x')
  23. plt.ylabel('y')
  24. plt.legend()
  25. plt.show()