-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homework_4.py
122 lines (70 loc) · 1.62 KB
/
Homework_4.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#This example uses the banded function provided by the author to solve systems of equations involving a sparse matrix
#with a multidiagonal band.
import numpy as np
import banded as b
import pylab as py
bands=5
#specify number of unknowns, the amount of Voltage nodes we are calculating
N=6
A=np.zeros((bands,N))
v=np.zeros(N)
v[0]=5
v[1]=5
A[0,:]=-1
A[1,:]=-1
A[2,:]=4
A[2,0]=3
A[2,N-1]=3
A[3,:]=-1
A[4,:]=-1
V=np.round(b.banded(A,v,2,2),decimals=3);
i=np.linspace(1,6,6)
print('Voltage at each successive node starting from the first: ',V)
py.scatter(i,V)
py.title('Voltage at each node')
py.show()
#same solution using Gaussian Elimination and backsubsitution to Check
#Example 6.1
import numpy as np
A=np.array([[3,-1,-1,0,0,0],
[-1,4,-1,-1,0,0],
[-1,-1,4,-1,-1,0],
[0,-1,-1,4,-1,-1],
[0,0,-1,-1,4,-1],
[0,0,0,-1,-1,3]],float)
v=np.array([5,5,0,0,0,0],float)
N=len(v)
for m in range(N):
div=A[m,m]
A[m,:]/=div
v[m]/=div
for i in range(m+1,N):
mult=A[i,m]
A[i,:]-=mult*A[m,:]
v[i] -= mult*v[m]
x=np.empty(N,float)
for m in range(N-1,-1,-1):
x[m]=v[m]
for i in range(m+1,N):
x[m] -=A[m,i]*x[i]
print(x)
##Now run for N=10,000 Voltage Nodes
bands=5
#specify number of unknowns, the amount of Voltage nodes we are calculating
N=10000
A=np.zeros((bands,N))
v=np.zeros(N)
v[0]=5
v[1]=5
A[0,:]=-1
A[1,:]=-1
A[2,:]=4
A[2,0]=3
A[2,N-1]=3
A[3,:]=-1
A[4,:]=-1
V=np.round(b.banded(A,v,2,2),decimals=3)
i=np.linspace(1,10000,10000)
py.scatter(i,V)
py.title('Voltage at each node')
py.show()