Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from scipy import optimize
- import matplotlib.pyplot as plt
- import numpy as np
- MIN_DIST = 1
- x = np.linspace(0, 10, 300)
- def f(x, min_dist):
- y = []
- for i in range(len(x)):
- if(x[i] <= min_dist):
- y.append(1)
- else:
- y.append(np.exp(- x[i] + min_dist))
- return y
- dist_low_dim = lambda x, a, b: 1 / (1 + a*x**(2*b))
- p , _ = optimize.curve_fit(dist_low_dim, x, f(x, MIN_DIST))
- print(p)
- plt.figure(figsize=(20,15))
- plt.plot(x, f(x, MIN_DIST), "o")
- plt.plot(x, dist_low_dim(x, p[0], p[1]), c = "red")
- plt.title("Non-Linear Least Square Fit of Piecewise Function", fontsize = 20)
- plt.gca().legend(('Original', 'Fit'), fontsize = 20)
- plt.xlabel("X", fontsize = 20)
- plt.ylabel("Y", fontsize = 20)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement