Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. from scipy import optimize
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5. MIN_DIST = 1
  6.  
  7. x = np.linspace(0, 10, 300)
  8.  
  9. def f(x, min_dist):
  10. y = []
  11. for i in range(len(x)):
  12. if(x[i] <= min_dist):
  13. y.append(1)
  14. else:
  15. y.append(np.exp(- x[i] + min_dist))
  16. return y
  17.  
  18. dist_low_dim = lambda x, a, b: 1 / (1 + a*x**(2*b))
  19.  
  20. p , _ = optimize.curve_fit(dist_low_dim, x, f(x, MIN_DIST))
  21. print(p)
  22.  
  23. plt.figure(figsize=(20,15))
  24. plt.plot(x, f(x, MIN_DIST), "o")
  25. plt.plot(x, dist_low_dim(x, p[0], p[1]), c = "red")
  26. plt.title("Non-Linear Least Square Fit of Piecewise Function", fontsize = 20)
  27. plt.gca().legend(('Original', 'Fit'), fontsize = 20)
  28. plt.xlabel("X", fontsize = 20)
  29. plt.ylabel("Y", fontsize = 20)
  30. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement