Simulating noise-free and noise-corrupted data

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

# number of data
N = 31

# coordinates of the data
x = np.linspace(0., 30., N)

xmax = np.max(x)
xmin = np.min(x)
Dx = xmax - xmin

# true constants a and b
a = -5.
b = 0.1

# noise-free data vector
d_free = a + b*x

# Gaussian noise with null mean and standard deviation defined 
#by the variable stdev
stdev = 0.3
noise = np.random.normal(loc = 0., scale=stdev, size=N)

# noise-corrupted data
d_noise = d_free + noise

dmin = np.min(d_noise)
dmax = np.max(d_noise)
Dd = dmax - dmin

plt.close('all')
plt.figure(figsize=(10,8))
plt.plot(x, d_free, 'ko', label='noise-free data')
plt.plot(x, d_noise, 'ro', label='noise-corrupted data')
plt.xlim(xmin - 0.05*Dx, xmax + 0.05*Dx)
plt.ylim(dmin - 0.05*Dd, dmax + 0.05*Dd)
plt.xlabel('x', fontsize=14)
plt.ylabel('data', fontsize=14)
plt.grid()
plt.legend(loc='best', fontsize=12)
plt.show()

  https://github.com/birocoles/Disciplina-metodos-computacionais/blob/master/Content/least_squares.ipynb

猜你喜欢

转载自www.cnblogs.com/gisalameda/p/11367849.html