def main(): # Testing the get points num_splines = 10 num_dados = 1000 x_min = 0 x_max = 10 lmbda = 100 # Generate a random spline to get samples from it weights = np.random.rand(num_splines) spl = spline(weights, x_min=x_min, x_max=x_max) # Get samples with noise t, x_t, spl_t = get_points(num_dados, num_splines, spl, x_min, x_max) if num_dados < 1000: plt.scatter(t, x_t, label="Simulated with noise", color=np.random.rand(3,)) plt.plot(t, spl_t, label="Original data") plt.xlabel("t") plt.ylabel("spl(t)") plt.title("Dados") # Interpolate the samples res = interpolate(t, x_t, num_splines, lmbda) new_spline = spline(res, x_min=x_min, x_max=x_max) plt.plot(t, new_spline(t), label="Simulated curve") plt.legend(loc="upper right") plt.show()
def interpolate(x, y, num_splines, lmbda): default_spl = spline(np.ones(num_splines), x_min=x[0], x_max=x[-1]) mu = np.zeros((len(x), num_splines)) for i in range(len(x)): for j in range(num_splines): mu[i][j] = default_spl.beta_j(j, x[i]) m_1 = mu.T@mu b = [email protected] m_2 = matrix_m2(num_splines) M = m_1 + lmbda*m_2 res = np.linalg.solve(M, b) return res
def points_random_noise(timeline, n_data_points, n_splines, x_min, x_max): # Gera pesos aleatórios e cria uma spline a partir deles rand_weights = np.array(np.random.rand(n_splines)) rand_spline = spl.spline(rand_weights, x_min=timeline[0], x_max=timeline[-1])(timeline) # Adiciona ruído aos pontos da spline gerada sigma = np.std(rand_spline) / 5 noise = np.array(np.random.normal(0, sigma, n_data_points)) points_generated = rand_spline + noise return points_generated
def main(n_data_points=350, n_splines=14, lmb=0.1, x_min=0, x_max=50): timeline = np.linspace(x_min, x_max, n_data_points) points = points_random_noise(timeline, n_data_points, n_splines, x_min, x_max) adjusted = adjust_spline(timeline, points, n_splines, lmb) points_with_noise = plt.scatter(timeline, points, s=3.0, color="green") generated_spline = plt.plot(timeline, spl.spline(adjusted, timeline[0], timeline[-1])(timeline), color='red') plt.show()
def adjust_spline(timeline, x_t, num_splines, lmb): # Cria uma spline "base", com coeficientes iguais a 1 para usar os beta_j's base_spline = spl.spline(np.ones(num_splines), x_min=timeline[0], x_max=timeline[-1]) mu_aux = [] for j in range(num_splines): mu_aux.append(base_spline.beta_j(j, timeline)) mu = np.array(mu_aux).T m1 = np.dot(mu.T, mu) y = np.dot(mu.T, x_t.T) m2 = spl.matrix_m2(num_splines) M = m1 + lmb * m2 # Resolve o sistema linear e retorna a solução adjusted = np.linalg.solve(M, y) return adjusted
def sphere_best_mobility_known(location, ETA, A): '''Best mobility known for a single sphere close to a wall. This function uses the mobilities translational-perpendicular: P. Huang and K. S. Breuer PRE 76, 046307 (2007). translational-parallel: for distances very close to the wall A. J. Goldman, R. G. Cox and H. Brenner, Chemical engineering science, 22, 637 (1967). For other distances L. P. Fauxcheux and A. J. Libchaber PRE 49, 5158 (1994). rotational-perpendicular: Cubic spline fit to the mobility of a sphere discretize with 162 markers. rotational-parallel: Cubic spline fit to the mobility of a sphere discretize with 162 markers. rotation-translation-coupling: Cubic spline fit to the mobility of a sphere discretize with 162 markers. ''' # Init the function the first time try: # Check if the spline function has been called sphere_best_mobility_known.init += 1e-01 except: # Exception to call the spline function only once per mobility component sphere_best_mobility_known.init = 1e-01 # Create variables to store mobilities: # distance to the wall sphere_best_mobility_known.mX = [] # Mobilities sphere_best_mobility_known.mRotationRotationParallel = [] sphere_best_mobility_known.mRotationRotationPerpendicular = [] sphere_best_mobility_known.mRotationTranslationCoupling = [] # Read 162-blobs sphere mobility. # rotational-roational mobility is normalize by (8*pi*ETA*A**3) # rotational-translation mobility is normalize by (6*pi*ETA*A**2) f = open('mobility.162-blob.dat', 'r') next(f) for line in f: data = line.split() # distance to the wall sphere_best_mobility_known.mX.append(float(data[0])) # Mobilities sphere_best_mobility_known.mRotationRotationParallel.append( float(data[3])) sphere_best_mobility_known.mRotationRotationPerpendicular.append( float(data[4])) sphere_best_mobility_known.mRotationTranslationCoupling.append( float(data[5])) # Create second derivative of mobility finctions # Call spline function sphere_best_mobility_known.mRotationRotationParallel_y2 = splines.spline( sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationParallel, len(sphere_best_mobility_known.mX), 1e+30, 1e+30) sphere_best_mobility_known.mRotationRotationPerpendicular_y2 = splines.spline( sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationPerpendicular, len(sphere_best_mobility_known.mX), 1e+30, 1e+30) sphere_best_mobility_known.mRotationTranslationCoupling_y2 = splines.spline( sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationTranslationCoupling, len(sphere_best_mobility_known.mX), 1e+30, 1e+30) # Compute mobilities # define threshold, at this distance the parallel mobilities # of Goldman and Feucheux cross threshold = 1.02979 * A # distance to the wall h = location[2] # dimensional factors factor_tt = 1.0 / (6.0 * np.pi * ETA * A) factor_rr = 1.0 / (8.0 * np.pi * ETA * A**3) factor_tr = 1.0 / (6.0 * np.pi * ETA * A**2) fluid_mobility = np.zeros([6, 6]) # translation-translation perpendicular to the wall fluid_mobility[2, 2] = factor_tt * Huang.selfMobilityHuang(A, h)[1] # translation-translation parallel to the wall if (h < threshold): mobility = factor_tt * Goldman.selfMobilityGoldman(A, h)[0, 0] fluid_mobility[0, 0] = mobility fluid_mobility[1, 1] = mobility else: mobility = factor_tt * Faucheux.selfMobilityFaucheux(A, h) fluid_mobility[0, 0] = mobility fluid_mobility[1, 1] = mobility # Rescale distance to the wall, splines are for a sphere of radius 1 h = h / A # rotation-rotation parallel to the wall mobility = factor_rr * splines.splint( sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationParallel, sphere_best_mobility_known.mRotationRotationParallel_y2, len(sphere_best_mobility_known.mX), h) fluid_mobility[3, 3] = mobility fluid_mobility[4, 4] = mobility # rotation-rotation perpendicular to the wall mobility = factor_rr * splines.splint( sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationPerpendicular, sphere_best_mobility_known.mRotationRotationPerpendicular_y2, len(sphere_best_mobility_known.mX), h) fluid_mobility[5, 5] = mobility # rotation-translation coupling mobility = factor_tr * splines.splint( sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationTranslationCoupling, sphere_best_mobility_known.mRotationTranslationCoupling_y2, len(sphere_best_mobility_known.mX), h) fluid_mobility[0, 4] = mobility fluid_mobility[1, 3] = mobility fluid_mobility[3, 1] = mobility fluid_mobility[4, 0] = mobility return fluid_mobility
def sphere_best_mobility_known(location, ETA, A): '''Best mobility known for a single sphere close to a wall. This function uses the mobilities translational-perpendicular: P. Huang and K. S. Breuer PRE 76, 046307 (2007). translational-parallel: for distances very close to the wall A. J. Goldman, R. G. Cox and H. Brenner, Chemical engineering science, 22, 637 (1967). For other distances L. P. Fauxcheux and A. J. Libchaber PRE 49, 5158 (1994). rotational-perpendicular: Cubic spline fit to the mobility of a sphere discretize with 162 markers. rotational-parallel: Cubic spline fit to the mobility of a sphere discretize with 162 markers. rotation-translation-coupling: Cubic spline fit to the mobility of a sphere discretize with 162 markers. ''' # Init the function the first time try: # Check if the spline function has been called sphere_best_mobility_known.init += 1e-01 except: # Exception to call the spline function only once per mobility component sphere_best_mobility_known.init = 1e-01 # Create variables to store mobilities: # distance to the wall sphere_best_mobility_known.mX = [] # Mobilities sphere_best_mobility_known.mRotationRotationParallel = [] sphere_best_mobility_known.mRotationRotationPerpendicular = [] sphere_best_mobility_known.mRotationTranslationCoupling = [] # Read 162-blobs sphere mobility. # rotational-roational mobility is normalize by (8*pi*ETA*A**3) # rotational-translation mobility is normalize by (6*pi*ETA*A**2) f = open('mobility.162-blob.dat', 'r') next(f) for line in f: data = line.split() # distance to the wall sphere_best_mobility_known.mX.append(float(data[0])) # Mobilities sphere_best_mobility_known.mRotationRotationParallel.append(float(data[3])) sphere_best_mobility_known.mRotationRotationPerpendicular.append(float(data[4])) sphere_best_mobility_known.mRotationTranslationCoupling.append(float(data[5])) # Create second derivative of mobility finctions # Call spline function sphere_best_mobility_known.mRotationRotationParallel_y2 = splines.spline(sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationParallel, len(sphere_best_mobility_known.mX), 1e+30, 1e+30) sphere_best_mobility_known.mRotationRotationPerpendicular_y2 = splines.spline(sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationPerpendicular, len(sphere_best_mobility_known.mX), 1e+30, 1e+30) sphere_best_mobility_known.mRotationTranslationCoupling_y2 = splines.spline(sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationTranslationCoupling, len(sphere_best_mobility_known.mX), 1e+30, 1e+30) # Compute mobilities # define threshold, at this distance the parallel mobilities # of Goldman and Feucheux cross threshold = 1.02979 * A # distance to the wall h = location[2] # dimensional factors factor_tt = 1.0 / (6.0*np.pi*ETA*A) factor_rr = 1.0 / (8.0*np.pi*ETA*A**3) factor_tr = 1.0 / (6.0*np.pi*ETA*A**2) fluid_mobility = np.zeros( [6, 6] ) # translation-translation perpendicular to the wall fluid_mobility[2,2] = factor_tt * Huang.selfMobilityHuang(A,h)[1] # translation-translation parallel to the wall if(h < threshold): mobility = factor_tt * Goldman.selfMobilityGoldman(A, h)[0,0] fluid_mobility[0,0] = mobility fluid_mobility[1,1] = mobility else: mobility = factor_tt * Faucheux.selfMobilityFaucheux(A, h) fluid_mobility[0,0] = mobility fluid_mobility[1,1] = mobility # Rescale distance to the wall, splines are for a sphere of radius 1 h = h / A # rotation-rotation parallel to the wall mobility = factor_rr * splines.splint(sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationParallel, sphere_best_mobility_known.mRotationRotationParallel_y2, len(sphere_best_mobility_known.mX), h) fluid_mobility[3,3] = mobility fluid_mobility[4,4] = mobility # rotation-rotation perpendicular to the wall mobility = factor_rr * splines.splint(sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationRotationPerpendicular, sphere_best_mobility_known.mRotationRotationPerpendicular_y2, len(sphere_best_mobility_known.mX), h) fluid_mobility[5,5] = mobility # rotation-translation coupling mobility = factor_tr * splines.splint(sphere_best_mobility_known.mX, sphere_best_mobility_known.mRotationTranslationCoupling, sphere_best_mobility_known.mRotationTranslationCoupling_y2, len(sphere_best_mobility_known.mX), h) fluid_mobility[0,4] = mobility fluid_mobility[1,3] = mobility fluid_mobility[3,1] = mobility fluid_mobility[4,0] = mobility return fluid_mobility
import splines as sp import numpy as np import matplotlib.pyplot as plt # a função original n_weights = 10 w = np.random.randn(n_weights) x = np.arange(0, 15.01, 0.01) s = sp.spline(w, x.min(), x.max()) y = s(x) label1, = plt.plot(x, y, c='g') # criação de amostras com ruídos x = np.arange(0, 15.5, 0.5) n_samples = x.shape[0] noise = np.random.normal(loc=0.0, scale=20, size=n_samples) y = s(x) + noise label2 = plt.scatter(x, y, c='r', marker='x') # matriz B B = np.zeros((n_samples, n_weights)) for i in range(n_samples): for j in range(n_weights): B[i, j] = s.beta_j(j, x[i]) # suavizador M2 = sp.matrix_m2(n_weights) b = B.T.dot(y) M1 = B.T.dot(B)