Advertisement
Guest User

Fit Function

a guest
May 13th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import division, print_function
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from scipy.optimize import curve_fit
  7. plt.style.use('ggplot')
  8.  
  9. def func(x, a, b, c, d, e, f):  # define function to fit
  10.     return a + np.cos(b * x + f) * np.sin(c * x + d) * np.exp(e * x)
  11.  
  12. csv = np.genfromtxt('Default_Dataset.csv', delimiter=",")  # load data
  13. popt, pcov = curve_fit(func, csv[:, 0], csv[:, 1])  # fit function
  14.  
  15. x_range = np.linspace(0, 8, 60)  # define range where to evaluate
  16. y_fitted = func(x_range, popt[0], popt[1], popt[2],
  17.                 popt[3], popt[4], popt[5])  # evaluate
  18.  
  19. fig, ax = plt.subplots()  # plot
  20. ax.plot(csv[:, 0], csv[:, 1], label='Original data')
  21. ax.plot(x_range, y_fitted, '--', label='Fitted data')
  22. ax.legend(loc='best')
  23. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement