Matriz inversa en visual basic
Publicado por Camila (1 intervención) el 10/03/2006 15:59:04
Hola.... necesito urgente el algortimo de la matriz inversa en Visual Basic... puede ser por medio de Gauss Jordan o con la adjunta
Valora esta pregunta


0
Private Sub Command1_Click()
a11 = Val(a11.Text)
a21 = Val(a21.Text)
b12 = Val(b12.Text)
b22 = Val(b22.Text)
r11 = Val(r11.Text)
r12 = Val(r12.Text)
If a11 = 0 Then
a11 = Val(a11 + b12 * 1)
a21 = Val(a21 + b22 * 1)
x11 = 1
x21 = 1
r11 = Val(r11 + r12 * 1)
End If
x11 = Val(x11.Caption)
x21 = Val(x21.Caption)
y12 = Val(y12.Caption)
y22 = Val(y22.Caption)
a = (a11 * -b12 / a11) + b12
b = (a21 * -b12 / a11) + b22
c = (x11 * -b12 / a11) + y12
d = (x21 * -b12 / a11) + y22
ra = r11 * -(b12 / a11) + r12
e = (a * -(a21 / b)) + a11
f = (b * -(a21 / b)) + a21
g = (c * -(a21 / b)) + x11
h = (d * -(a21 / b)) + x21
rb = (ra * -(a21 / b)) + r11
i = e * (1 / e)
j = f * (1 / e)
k = g * (1 / e)
l = h * (1 / e)
rc = rb * (1 / e)
m = a * (1 / b)
n = b * (1 / b)
o = c * (1 / b)
p = d * (1 / b)
rd = ra * (1 / b)
Label5.Caption = Format(a11, "0.######")
Label6.Caption = Format(a21, "0.######")
Label7.Caption = Format(x11, "0.######")
Label8.Caption = Format(x21, "0.######")
Label9.Caption = Format(a, "0.######")
Label10.Caption = Format(b, "0.######")
Label11.Caption = Format(c, "0.######")
Label12.Caption = Format(d, "0.######")
Label13.Caption = Format(e, "0.######")
Label14.Caption = Format(f, "0.######")
Label15.Caption = Format(g, "0.######")
Label16.Caption = Format(h, "0.######")
Label17.Caption = Format(a, "0.######")
Label18.Caption = Format(b, "0.######")
Label19.Caption = Format(c, "0.######")
Label20.Caption = Format(d, "0.######")
Label21.Caption = Format(i, "0.######")
Label22.Caption = Format(j, "0.######")
Label23.Caption = Format(k, "0.######")
Label24.Caption = Format(l, "0.######")
Label25.Caption = Format(a, "0.######")
Label26.Caption = Format(b, "0.######")
Label27.Caption = Format(c, "0.######")
Label28.Caption = Format(d, "0.######")
Label29.Caption = Format(i, "0.######")
Label30.Caption = Format(j, "0.######")
Label31.Caption = Format(k, "0.######")
Label32.Caption = Format(l, "0.######")
Label33.Caption = Format(m, "0.######")
Label34.Caption = Format(n, "0.######")
Label35.Caption = Format(o, "0.######")
Label36.Caption = Format(p, "0.######")
Labela.Caption = r11
Labelb.Caption = ra
Labelc.Caption = rb
Labeld.Caption = ra
Labele.Caption = rc
Labelf.Caption = ra
Labelg.Caption = rc
Labelh.Caption = rd
Label57.Caption = Val(Labelg.Caption)
Label106.Caption = Val(Labelh.Caption)
xxx = Val((Text1.Text * Label57.Caption) + (Text2.Text * Label106.Caption))
Label109.Caption = Format(xxx, "0.######")
End Sub
Private Sub Command2_Click()
a11.Text = ""
a21.Text = ""
b12.Text = ""
b22.Text = ""
Label5.Caption = " "
Label6.Caption = " "
Label7.Caption = " "
Label8.Caption = " "
Label9.Caption = " "
Label10.Caption = " "
Label11.Caption = " "
Label12.Caption = " "
Label13.Caption = " "
Label14.Caption = " "
Label15.Caption = " "
Label16.Caption = " "
Label17.Caption = " "
Label18.Caption = " "
Label19.Caption = " "
Label20.Caption = " "
Label21.Caption = " "
Label22.Caption = " "
Label23.Caption = " "
Label24.Caption = " "
Label25.Caption = " "
Label26.Caption = " "
Label27.Caption = " "
Label28.Caption = " "
Label29.Caption = " "
Label30.Caption = " "
Label31.Caption = " "
Label32.Caption = " "
Label33.Caption = " "
Label34.Caption = " "
Label35.Caption = " "
Label36.Caption = " "
Label58.Caption = ""
Label59.Caption = ""
Label60.Caption = ""
Label61.Caption = ""
Label62.Caption = ""
Label63.Caption = ""
Label64.Caption = ""
Label65.Caption = ""
Label66.Caption = ""
Label67.Caption = ""
Label68.Caption = ""
Label69.Caption = ""
Label70.Caption = ""
Label71.Caption = ""
Label72.Caption = ""
Label73.Caption = ""
Label74.Caption = ""
Label75.Caption = ""
Label76.Caption = ""
Label77.Caption = ""
Labela.Caption = ""
Labelb.Caption = ""
Labelc.Caption = ""
Labeld.Caption = ""
Labele.Caption = ""
Labelf.Caption = ""
Labelg.Caption = ""
Labelh.Caption = ""
Label102.Caption = ""
Label103.Caption = ""
Label104.Caption = ""
Label105.Caption = ""
r11.Text = ""
r12.Text = ""
Label57.Caption = "X"
Label106.Caption = "Y"
Label109.Caption = ""
Text1.Text = ""
Text2.Text = ""
x11 = 1
x21 = 0
End Sub
Private Sub Command3_Click()
Label58.Caption = Val(a11.Text)
Label59.Caption = Val(Label31.Caption)
Label60.Caption = Val(a21.Text)
Label61.Caption = Val(Label35.Caption)
R1 = Val((Label58.Caption * Label59.Caption) + (Label60.Caption * Label61.Caption))
Label62.Caption = Format(R1, "0.###")
Label63.Caption = Val(b12.Text)
Label64.Caption = Val(Label31.Caption)
Label65.Caption = Val(b22.Text)
Label66.Caption = Val(Label35.Caption)
R2 = Val((Label63.Caption * Label64.Caption) + (Label65.Caption * Label66.Caption))
Label67.Caption = Format(R2, "0.###")
Label68.Caption = Val(a11.Text)
Label69.Caption = Val(Label32.Caption)
Label70.Caption = Val(a21.Text)
Label71.Caption = Val(Label36.Caption)
R3 = Val((Label68.Caption * Label69.Caption) + (Label70.Caption * Label71.Caption))
Label72.Caption = Format(R3, "0.###")
Label73.Caption = Val(b12.Text)
Label74.Caption = Val(Label32.Caption)
Label75.Caption = Val(b22.Text)
Label76.Caption = Val(Label36.Caption)
R4 = Val((Label73.Caption * Label74.Caption) + (Label75.Caption * Label76.Caption))
Label77.Caption = Format(R4, "0.###")
Label102.Caption = "1"
Label103.Caption = "0"
Label104.Caption = "1"
Label105.Caption = "0"
End Sub
Private Sub Command1_Click()
A1 = Val(Text1)
A2 = Val(Text2)
A3 = Val(Text3)
B1 = Val(Text4)
B2 = Val(Text5)
B3 = Val(Text6)
C1 = Val(Text7)
C2 = Val(Text8)
C3 = Val(Text9)
A4 = 1
A5 = 0
A6 = 0
b4 = 0
b5 = 1
b6 = 0
c4 = 0
c5 = 0
c6 = 1
If A1 = 0 Then
MsgBox "ERROR NO TIENE INVERSA"
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text8 = ""
Text9 = ""
Text1.SetFocus
Else
X1 = A1 / A1
X2 = A2 / A1
X3 = A3 / A1
X4 = 1 / A1
X5 = 0
X6 = 0
Y1 = B1 - B1 * X1
Y2 = B2 - B1 * X2
Y3 = B3 - B1 * X3
Y4 = 0 - B1 * X4
Y5 = 1 - B1 * X5
Y6 = 0 - B1 * X6
Z1 = C1 - C1 * X1
Z2 = C2 - C1 * X2
Z3 = C3 - C1 * X3
Z4 = 0 - C1 * X4
Z5 = 0 - C1 * X5
Z6 = 1 - C1 * X6
If X1 = 0 Or Y2 = 0 Or Z3 = 0 Then
MsgBox "ERROR NO TIENE INVERSA"
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text8 = ""
Text9 = ""
Text1.SetFocus
Else
E1 = Y1 / Y2
E2 = Y2 / Y2
E3 = Y3 / Y2
E4 = Y4 / Y2
E5 = Y5 / Y2
E6 = Y6 / Y2
D1 = X1 - X2 * E1
D2 = X2 - X2 * E2
D3 = X3 - X2 * E3
D4 = X4 - X2 * E4
D5 = X5 - X2 * E5
D6 = X6 - X2 * E6
F1 = Z1 - Z2 * E1
F2 = Z2 - Z2 * E2
F3 = Z3 - Z2 * E3
F4 = Z4 - Z2 * E4
F5 = Z5 - Z2 * E5
F6 = Z6 - Z2 * E6
If D1 = 0 Or E2 = 0 Or F3 = 0 Then
MsgBox "ERROR NO TIENE INVERSA"
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text8 = ""
Text9 = ""
Text1.SetFocus
Else
I1 = F1 / F3
I2 = F2 / F3
I3 = F3 / F3
I4 = F4 / F3
I5 = F5 / F3
I6 = F6 / F3
G1 = D1 - D3 * I1
G2 = D2 - D3 * I2
G3 = D3 - D3 * I3
G4 = D4 - D3 * I4
G5 = D5 - D3 * I5
G6 = D6 - D3 * I6
H1 = E1 - E3 * I1
H2 = E2 - E3 * I2
H3 = E3 - E3 * I3
H4 = E4 - E3 * I4
H5 = E5 - E3 * I5
H6 = E6 - E3 * I6
Text10 = CStr(G4)
Text11 = CStr(G5)
Text12 = CStr(G6)
Text13 = CStr(H4)
Text14 = CStr(H5)
Text15 = CStr(H6)
Text16 = CStr(I4)
Text17 = CStr(I5)
Text18 = CStr(I6)
End If
End If
End If
End Sub
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text6 = ""
Text7 = ""
Text8 = ""
Text9 = ""
Text10 = ""
Text11 = ""
Text12 = ""
Text13 = ""
Text14 = ""
Text15 = ""
Text16 = ""
Text17 = ""
Text18 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Dim A(10, 10), B(10, 10), C(10, 10) As Single 'arreglos de las matrices A y C operados C es el resultado
Dim m, n, p, q, i, j, k As Integer 'variables de las dimensiones y de los indices
Dim subtitulo As String
Private Sub Command1_Click()
Dim ancho, alto As Single 'para el cambio de variables de la dimencion del cuadro 1
Picture1.Cls
ancho = Picture1.ScaleWidth
alto = Picture1.ScaleHeight
'para capturar la primera matriz(A) de orden m,n
'lectura del tamaño de la matriz
m = Val(InputBox("numero de filas=", "matrices"))
n = Val(InputBox("numero de columnas=", "matrices"))
Text1.Text = "(" + Str(m) + "," + Str(n) + ")"
'lectura por filas de los elementos de A
For i = 1 To m
For j = 1 To n
subtitulo = "valor de A(" + Str(i) + "," + Str(j) + ")="
A(i, j) = Val(InputBox(subtitulo, "matrices"))
Picture1.CurrentX = ancho / (n + 1) * j
Picture1.CurrentY = alto / (m + 1) * i
Picture1.Print A(i, j)
Next j
Next i
End Sub
Private Sub Command2_Click()
Dim ancho, alto As Single 'para el cambio de variables de la dimencion del cuadro 2
Picture2.Cls
ancho = Picture2.ScaleWidth
alto = Picture2.ScaleHeight
'para capturar la primera matriz(A) de orden m,n
'lectura del tamaño de la matriz
p = Val(InputBox("numero de filas=", "matrices"))
q = Val(InputBox("numero de columnas=", "matrices"))
Text2.Text = "(" + Str(m) + "," + Str(n) + ")"
'lectura por filas de los elementos de A
For i = 1 To m
For j = 1 To n
subtitulo = "valor de B(" + Str(i) + "," + Str(j) + ")="
B(i, j) = Val(InputBox(subtitulo, "matrices"))
Picture2.CurrentX = ancho / (q + 1) * j
Picture2.CurrentY = alto / (p + 1) * i
Picture2.Print B(i, j)
Next j
Next i
End Sub
Private Sub Option1_Click()
Dim ancho, alto As Single
Picture3.Cls
ancho = Picture3.ScaleWidth
alto = Picture2.ScaleHeight
'suma de matrices
If m = p And n = p Then
'procedimiento para la suma
For i = 1 To m
For j = 1 To n
C(i, j) = A(i, j) + B(i, j)
Picture3.CurrentX = ancho / (n + 1) * j
Picture3.CurrentY = alto / (m + 1) * i
Picture3.Print C(i, j)
Next j
Next i
Else
Picture3.CurrentX = ancho / 3
Picture3.CurrentY = alto / 2
Picture3.Print "no compatible para suma"
End If
End Sub
Private Sub Option2_Click()
Picture3.Cls
ancho = Picture3.ScaleWidth
alto = Picture3.ScaleHeight
If m = p And n = q Then
'proceso de la multiplicacion
For i = 1 To m
For j = 1 To q
C(i, j) = A(i, j) - B(i, j)
For k = 1 To n
C(i, j) = C(i, j) + A(i, k) * B(k, j)
Next k
Picture3.CurrentX = ancho / (n + 1) * j
Picture3.CurrentY = alto / (m + 1) * i
Picture3.Print C(i, j)
Next j
Next i
Else
Picture3.CurrentX = ancho / 3
Picture3.CurrentY = alto / 2
Picture3.Print "no compatibles para substraccion"
End If
End Sub
Private Sub Option3_Click()
'multiplicacion de matrices
Dim ancho, alto As Single
Picture3.Cls
ancho = Picture3.ScaleWidth
alto = Picture3.ScaleHeight
If n = p Then
'proceso de la multiplicacion
For i = 1 To m
For j = 1 To q
C(i, j) = 0
For k = 1 To n
C(i, j) = C(i, j) + A(i, k) * B(k, j)
Next k
Picture3.CurrentX = ancho / (n + 1) * j
Picture3.CurrentY = alto / (m + 1) * i
Picture3.Print C(i, j)
Next j
Next i
Else
Picture3.CurrentX = ancho / 3
Picture3.CurrentY = alto / 2
Picture3.Print "no compatibles para multiplicacion"
End If
End Sub