本次作业题目如下:
11.1
代码如下:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100)
y = np.sin(x-2)*np.sin(x-2)*np.exp(-(x*x))
plt.plot(x, y)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("f(x) = sin^2(x-2)*exp(-x^2)")
plt.show()
绘制结果如图:
11.2
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import *
def find_est(b_est, X, y):
return np.linalg.norm(np.dot(X, b_est) - y, ord = 2)
X = np.random.normal(size=(20, 10))
b = np.random.normal(size=(10, 1))
z = np.random.normal(size=(20, 1))
y = np.dot(X, b) + z
b_est =[]
i = 0
while i < 10:
b_est.append([0])
i = i+1
point = minimize(find_est, b_est, args=(X,y))
b_est_find = point.x
x_P = np.linspace(-5, 10, 10)
plt.figure()
plt.xlabel('x')
plt.ylabel('y')
point1 = plt.scatter(x_P, b, c='R')
point2 = plt.scatter(x_P, b_est_find, c='B')
plt.legend([point1, point2], ['true parameters b', 'estimated parameters b'])
plt.show()
结果如图:
11.3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab
ran_num = np.random.normal(loc=0, scale=1, size = 10000)
print(ran_num)
n, bins, patches = plt.hist(ran_num, bins = 25, normed = True)
y = matplotlib.mlab.normpdf(bins, 0, 1)
plt.plot(bins, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Histogram and density estimation')
plt.show()
结果如图: