-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw4.py
More file actions
53 lines (42 loc) · 1.1 KB
/
hw4.py
File metadata and controls
53 lines (42 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress
def meanerror(n):
A = np.random.rand(n,n)
x = np.random.rand(n)
b = A.dot(x)
y = np.linalg.solve(A,b)
dx = np.mean(abs(x-y))
print(x,y,dx)
return dx
meanerror(5)
"""
#test_n = [10,20,50,100,200,500,1000,2000]
test_n = [2,5,50,100, 200]
result = [[],[],[],[],[],[],[],[]]
running_list = [[],[],[],[],[],[],[],[]]
for i in range(len(test_n)):
result[i]=[]
index = []
total = 0
for j in range(1000):
result[i].append(meanerror(test_n[i]))
index.append(j+1)
for k in range(len(index)):
total += result[i][k]
running_list[i].append(total/(k+1))
#print(running_list[i][k], index[k]
"""
"""
for i in range(len(index)):
x = running_list[i]
y = index
fig, ax = plt.subplots()
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_adjustable("datalim")
ax.scatter(x,y)
ax.set_xlabel('average mean error')
ax.set_ylabel('number of iterations')
plt.show()
"""