作业1 图表完成

This commit is contained in:
veypi 2021-01-16 23:58:08 +08:00
parent 104f21cb98
commit 5b638407f8
2 changed files with 66 additions and 42 deletions

View File

@ -56,5 +56,8 @@ end
作业提交有效时间是今天到10月23日两周后之前的任意时间。提交作业请将代码和报告打包以“课后作业1-名字-学号”命名提交。 作业提交有效时间是今天到10月23日两周后之前的任意时间。提交作业请将代码和报告打包以“课后作业1-名字-学号”命名提交。
![image-20210116232529898](https://public.veypi.com/img/screenshot/20210116232529.png)
![image-20210116235118221](https://public.veypi.com/img/screenshot/20210116235118.png)
![image-20210116235324496](https://public.veypi.com/img/screenshot/20210116235324.png)

View File

@ -28,7 +28,7 @@ def LU_decomposition(A):
return L, U return L, U
# 生成范围[-100, 100]之前的方阵A 和 b # 生成随机矩阵 A, b, 范围[-10, 10]
def randomAb(m): def randomAb(m):
A = np.random.random([m, m]) * 20 - 10 A = np.random.random([m, m]) * 20 - 10
dia = np.random.random(m) * 10 dia = np.random.random(m) * 10
@ -37,6 +37,29 @@ def randomAb(m):
return A, np.random.randint(0, 10, [m, 1]) return A, np.random.randint(0, 10, [m, 1])
# 生成稀疏矩阵A, b
def sparseMatrix(m):
A, b = randomAb(m)
for i in range(m):
for j in range(m):
if i != j:
a = A[i][j]
if abs(a) < 9:
A[i][j] = 0
elif a > 0:
A[i][j] = 10 * (a - 9)
else:
A[i][j] = 10 * (9 + a)
return A, b
# 生成病态矩阵A, b
def illMatrix(m):
A, b = randomAb(m)
return A, b
class Question1: class Question1:
""" """
求解 Ax=b 求解 Ax=b
@ -104,23 +127,26 @@ class Question1:
""" """
return np.linalg.solve(A, b) return np.linalg.solve(A, b)
def RMSE(self, solver, n=8): def RMSE(self, solver, n=8, randFunc=randomAb):
## 计算方差 ## 计算方差
s = time.time() s = time.time()
A, b = randomAb(n) A, b = randFunc(n)
X = (A.dot(solver(A, b)) - b) ** 2 X = (A.dot(solver(A, b)) - b) ** 2
for i in range(1000): for i in range(1000):
A, b = randomAb(n) A, b = randFunc(n)
X = X + (A.dot(solver(A, b)) - b) ** 2 X = X + (A.dot(solver(A, b)) - b) ** 2
return np.max(X), time.time() - s return np.max(X), time.time() - s
def show(self): def show(self):
N = [2 ** i for i in range(12)] n = 12
Y = [[0 for i in range(12)] for _ in range(4)] N = [2 ** i for i in range(n)]
Z = [[0 for i in range(12)] for _ in range(4)] Y = [[0 for i in range(n)] for _ in range(4)]
Z = [[0 for i in range(n)] for _ in range(4)]
plt.subplot(1, 2, 1) randomFuns = [randomAb, sparseMatrix, illMatrix]
for i in range(len(N)): for r in range(3):
plt.subplot(3, 2, 2*r + 1)
for i in range(n):
print("size: %s" % N[i]) print("size: %s" % N[i])
print("LU") print("LU")
Y[0][i], Z[0][i] = self.RMSE(self.solver1, N[i]) Y[0][i], Z[0][i] = self.RMSE(self.solver1, N[i])
@ -140,8 +166,8 @@ class Question1:
plt.title('Accuracy') plt.title('Accuracy')
plt.yscale('symlog') plt.yscale('symlog')
plt.xscale('symlog') plt.xscale('symlog')
plt.legend(loc='lower right') # plt.legend(loc='lower right')
plt.subplot(1, 2, 2) plt.subplot(3, 2, 2 * r + 2)
plt.plot(N, Z[0], label="LU") plt.plot(N, Z[0], label="LU")
plt.plot(N, Z[1], label="Jacobi") plt.plot(N, Z[1], label="Jacobi")
plt.plot(N, Z[2], label="inverse") plt.plot(N, Z[2], label="inverse")
@ -149,15 +175,10 @@ class Question1:
plt.xscale('symlog') plt.xscale('symlog')
plt.yscale('symlog') plt.yscale('symlog')
plt.title('time cost') plt.title('time cost')
plt.legend(loc='lower right') # plt.legend(loc='lower right')
plt.show() plt.show()
if __name__ == "__main__": if __name__ == "__main__":
q = Question1() q = Question1()
A, b = randomAb(3)
print(A)
print(np.linalg.det(A))
# print(q.solver2(A, b))
# print(q.solver4(A, b))
q.show() q.show()