-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path112.bas
More file actions
198 lines (152 loc) · 3.2 KB
/
Copy path112.bas
File metadata and controls
198 lines (152 loc) · 3.2 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'''Solution of equations'' systems
''with cramer''s method
''by Adolfo Leon Sepulveda
''Sep/30/2004
''version 1.0
main
end
sub main()
dim m_mat(50,50), a_mat(50,50)
dim v_vec(50), x(50)
local i, j, k, n
local den
print "Solution to Equations'' Systems with cramer''s method"
print "Num equations ";
input n
print "Input coef: "
readmat n, n, m_mat
print "Input constants: "
readvec n, v_vec
print
print "Equation System:"
PrintEqSystem m_mat,v_vec,n
print
den = det(n, m_mat)
print "Determinant: "; den
print
print "Solution: "
for i = 0 to n-1
a_mat = m_mat '' copia m_mat en a_mat
cambio_columna n, n, i, a_mat, v_vec
print "x(";i;")="; det(n, a_mat) / den
next i
inversa n, m_mat, a_mat
print
print "Inverse matrix of coeficients:"
matprint a_mat, n, n
end
sub readmat(filas,columnas, byref m_mat)
local i, j
for i = 0 to filas - 1
for j = 0 to columnas - 1
print "a(";i+1;", ";j+1;") : ";
input m_mat(i,j)
next j
next i
end ''readmat
sub readvec(n, byref v_vec)
dim v_vec(50)
local i
for i = 0 to n -1
print "b(";i+1;") : ";
input v_vec(i)
next i
end ''readvec
''get determinant
func det(n,a_mat)
local i, j, k, t, f, nxt
local piv, cn, big, temp, term
f = 1
for i = 0 to n - 2
big = 0
for k = i to n - 1
term = abs(a_mat(k,i))
if term > big then
big = term
t = k
endif
next k
if ( i - t != 0 ) then f = -f
for j = 0 to n - 1
temp = a_mat(i,j)
a_mat(i,j) = a_mat(t,j)
a_mat(t,j) = temp
next j
piv = a_mat(i,i)
nxt = i + 1
for j = nxt to n - 1
if ( piv != 0 ) then cn = a_mat(j,i) / piv
for k = i to n + 1
a_mat(j,k) = a_mat(j,k) - cn * a_mat(i,k)
next k
next j
next i
temp = 1.0
for i = 0 to n - 1
temp = temp * a_mat(i,i)
next i
det = temp * f
end
sub cambio_columna(filas, columnas, destino, byref m_mat, nueva_columna)
local i
for i = 0 to filas -1
m_mat(i,destino) = nueva_columna(i)
next i
end
sub matprint(m_mat, f, c)
local i, j
for i = 0 to f - 1
for j = 0 to c-1
print using "###,###.00";m_mat(i,j);
next j
print
next i
end
sub intercambio(n,k,byref a_mat, byref c_vec)
local e,j
for j = 0 to n-1
e = c_vec(j)
c_vec(j) = a_mat(j,k)
a_mat(j,k) = e
next j
end ''intercambio
sub solucion(n, a_mat, coef, byref x)
local k,d
d = det(n, a_mat)
for k =0 to n-1
intercambio n, k, a_mat, coef
x(k) = det(n, a_mat) / d
intercambio n, k, a_mat, coef
next k
end
''get invert matrix
sub inversa(n, m_mat, byref a_mat)
dim i_vec(50), tmp_vec(50)
local j, k
for j = 0 to n-1
i_vec(j) = 0.0
next j
if det(n,m_mat) = 0.0 then
print "Singular Matrix"
exit sub
endif
for j = 0 to n-1
i_vec(j) = 1.0
solucion n, m_mat, i_vec, tmp_vec
cambio_columna n, n, j, a_mat, tmp_vec
i_vec(j) = 0.0
next j
end ''inversa
sub PrintEqSystem(a,b,n)
local i,j
for i = 0 to n-1
for j= 0 to n-1
if j = n-1 then
print using "###,###.00"; a(i,j);
print using "###,###.00"; b(i)
else
print using "###,###.00"; a(i,j);
endif
next j
next i
end'