-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path119.bas
More file actions
152 lines (136 loc) · 2.4 KB
/
Copy path119.bas
File metadata and controls
152 lines (136 loc) · 2.4 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
'''gaussj.bas
''version 1.0
''by adolfo leon sepulveda
'' 20/08/2004
''solve a nxn equation system with gauss jordan method
''It''s equivatent to LinEqn function of SmallBASIC
''example:
'' 0x+1y=1
'' 1x+1y=2
''
''Input:
'' Nro Equat: 2
'' a(1,1) : ? 0
'' a(1,2) : ? 1
'' b(1) : ? 1
'' a(2,1) : ? 1
'' a(2,1) : ? 1
'' b(2) : ? 2
'' result:
'' x=1
'' y=1
'' invert matrix:
'' -1 1
'' 1 0
n=0
dim a(50,50)
dim x(50)
main
End ''program
sub main()
readata a,x,n
gaussj a,x,n
Printinversa a,n
Printsolution x,n
end
sub readata( byref a, byref x, byref n)
print "gauss jordan"
print "nro equat: ";
input n
print "coef y term indep "
for i=0 to n-1
for j=0 to n
if j<>n then
print "a(";i+1;", ";j+1;") : ";
else
print "b(";i+1;") : ";
endif
input a(i,j)
next
x(i)=a(i,n)
next
end
sub gaussj(byref a, byref b,n)
local mayor,tmp,pivinv
dim indco(50),indfi(50),piv(50)
local i,j,k,t,h,col,fil
for j = 0 to n-1
piv(j)=0
next j
for i = 0 to n-1
mayor = 0.0
for j = 0 to n-1
if piv(j) <> 1 then
for k = 0 to n-1
if piv(k) = 0 then
if abs(a(j,k)) >= mayor then
mayor = abs(a(j,k))
fil = j
col = k
endif
elseif piv(k) > 1 then
print "error: singular matrix"
Exit Sub
endif
next
endif
next
piv(col) = piv(col)+1
if fil <> col then
for t = 0 to n-1
tmp = a(fil,t)
a(fil,t) = a(col,t)
a(col,t) = tmp
next
tmp = b(fil)
b(fil) = b(col)
b(col) = tmp
endif
indfi(i) = fil
indco(i) = col
if a(col,col) = 0 then
print "error: singular matrix"
Exit Sub
endif
pivinv= 1.0/a(col,col)
a(col,col) = 1.0
for t=0 to n-1
a(col,t) = a(col,t)*pivinv
next
b(col) = b(col)*pivinv
for h = 0 to n-1
if h <> col then
tmp = a(h,col)
a(h,col) = 0.0
for t = 0 to n-1
a(h,t) = a(h,t) - a(col,t)*tmp
next
b(h) = b(h) - b(col)*tmp
endif
next
next
for t = n-1 to 0
if indfi(t) <> indco(t) then
for k = 0 to n - 1
tmp = a(k,indfi(t)) = a(k,indco(t))
a(k,indco(t)) = tmp
next
endif
next
end
sub Printsolution(x,n)
print "solucion"
for j=0 to n-1
print "x";j+1;"=";x(j)
next
end
sub Printinversa(a,n)
print "matriz inversa"
for i=0 to n-1
for j=0 to n-1
print using "##0.00"; a(i,j);
next
print
next
end
'