Ejemplo calculando determinante, matriz inversa y adjunta 2x2 java
En este ejemplo muestro un código en java con el que podrán calcular el determinante, matriz inversa y matriz adjunta para lo cual solo deberían de entender la lógica en como se resuelven así:
Determinante
Si A= a b
c d
Si A/delim{|}A{|})
seria:
delim{|}{A}{|} = ad - bc
Matriz inversa
2º) Hallamos la matriz adjunta transpuesta, con este método práctico:■ Intercambiamos entre sí los elementos de la diagonal principal, y■ Cambiamos el signo de los elementos de la diagonal secundaria (dejándolos en su lugar)
3º) Dividimos la matriz adjunta transpuesta por el determinante de la matriz original, y así obtenemos la inversa A⁻¹
[4 -2]
[ 0 1]
A⁻¹ = –––––
4
A⁻¹ = [1 -1/2] ◄ RESPUESTA
[ 0 1/4]
Matriz adjunta
Matriz algo simple en la cual de manera simplificada:
I a b I
I c d I
entonces la matriz adjunta será:
I d -c I
I -b a I
I c d I
entonces la matriz adjunta será:
I d -c I
I -b a I
Aquí el código:
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.util.StringTokenizer;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.util.StringTokenizer;
import java.awt.event.*;
public class Matrices2x2 extends JFrame implements ActionListener{
private JTextField[][] textfield1=new JTextField[100][100];
private int X=0,Y=0;
private JLabel total,lm,Matrizinversa;
private JLabel[][] MI = new JLabel[100][100];
private JButton boton2,boton3,boton4,boton5;
private JPanel contentPane;
public Matrices2x2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setBounds(0,0,400,300);
lm=new JLabel("total");
lm.setBounds(235,240,100,30);
add(lm);
total=new JLabel("0");
total.setBounds(300,240,60,30);
add(total);
boton2=new JButton("Determinante");
boton2.setBounds(350,210,125,30);
add(boton2);
boton4=new JButton("M. Inversa");
boton4.setBounds(350,240,125,30);
add(boton4);
boton5=new JButton("M. Adjunta");
boton5.setBounds(350,270,125,30);
add(boton5);
boton2.addActionListener(this);
boton4.addActionListener(this);
boton5.addActionListener(this);
for(int i=0;i<100;i++){
MI[i][i]=new JLabel();
MI[i][i].setText("");
MI[i][i].setBounds(i,i,60,30);
add(MI[i][i]);
MI[i][i].setVisible(false);
Matrizinversa = new JLabel();
Matrizinversa.setBounds(i,i,i,i);
Matrizinversa.setVisible(false);
}
//colocando textfield
int x=0,y=0,A,B=40;
for(x=0; x<2;x++){
System.out.println("for x");
A=80;
for( y=0; y<2;y++){
System.out.println("for y");
System.out.println("1");
textfield1[x][y] =new JTextField("0");
System.out.println("2");
textfield1[x][y].setBounds(B,A,30,20);
System.out.println("3");
add(textfield1[x][y]);
A=A+40;
}
B=B+40;
}
}
public void actionPerformed(ActionEvent e) {
//determinage 2x2
if (e.getSource()==boton2) {
double Total=0;
for(int gh=0;gh<=100;gh++){
for(int gh2=0;gh2<=100;gh2++){
try
{
MI[gh][gh2].setVisible(false);
Matrizinversa.setVisible(false);
}catch(Exception ee){}
}
}
String aa =textfield1[0][0].getText();
double a= Double.parseDouble(aa);
double b= Double.parseDouble(textfield1[0][1].getText());
double c= Double.parseDouble(textfield1[1][0].getText());
double d= Double.parseDouble(textfield1[1][1].getText());
Total = a*d-b*c;
total.setText(String.valueOf(Total));
add(total);
}
//Matriz inversda2x2
if (e.getSource()==boton4)
{
double Total,Tinversa =0;
for(int gh=0;gh<=100;gh++){
for(int gh2=0;gh2<=100;gh2++)
{
try
{
MI[gh][gh2].setVisible(false);
Matrizinversa.setVisible(false);
}catch(Exception ee){}
}}
String aa =textfield1[0][0].getText();
double a= Double.parseDouble(aa);
double b= Double.parseDouble(textfield1[0][1].getText());
double c= Double.parseDouble(textfield1[1][0].getText());
double d= Double.parseDouble(textfield1[1][1].getText());
Total = a*d-b*c;
if(Total!=0)
{
Tinversa=(a*d-b*c);
//MI[0][0]=new JLabel();
Matrizinversa = new JLabel(".");
Matrizinversa.setText("Resultado:");
Matrizinversa.setBounds(220,145,70,30);
Matrizinversa.setVisible(true);
add(Matrizinversa);
MI[0][0].setText(String.valueOf(d/Tinversa)+"");
MI[0][0].setBounds(220,180,60,30);
MI[0][0].setVisible(true);
add(MI[0][0]);
MI[1][0]=new JLabel();
MI[1][0].setText(String.valueOf(-c/Tinversa));
MI[1][0].setBounds(280,180,60,30);
add(MI[1][0]);
MI[0][1]=new JLabel();
MI[0][1].setText(String.valueOf(-b/Tinversa));
MI[0][1].setBounds(220,210,60,30);
add(MI[0][1]);
MI[1][1]=new JLabel();
MI[1][1].setText(String.valueOf(a/Tinversa));
MI[1][1].setBounds(280,210,60,30);
add(MI[1][1]);
}else
{
total.setText("Error det. da 0");
add(total);
}
}
//Matriz adjunta 2x2
if (e.getSource()==boton5)
{
for(int gh=0;gh<=100;gh++){
for(int gh2=0;gh2<=100;gh2++)
{
try
{ MI[gh][gh2].setVisible(false);
Matrizinversa.setVisible(false);
}catch(Exception ee){}
}
}
String aa =textfield1[0][0].getText();
double a= Double.parseDouble(aa);
double b= Double.parseDouble(textfield1[0][1].getText());
double c= Double.parseDouble(textfield1[1][0].getText());
double d= Double.parseDouble(textfield1[1][1].getText());
Matrizinversa = new JLabel(".");
Matrizinversa.setText("Resultado:");
Matrizinversa.setBounds(220,145,70,30);
Matrizinversa.setVisible(true);
add(Matrizinversa);
MI[0][0].setText(String.valueOf(d));
MI[0][0].setBounds(220,180,60,30);
MI[0][0].setVisible(true);
add(MI[0][0]);
MI[1][0]=new JLabel();
MI[1][0].setText("-"+String.valueOf(b));
MI[1][0].setBounds(280,180,60,30);
add(MI[1][0]);
MI[0][1]=new JLabel();
MI[0][1].setText("-"+String.valueOf(c));
MI[0][1].setBounds(220,210,60,30);
add(MI[0][1]);
MI[1][1]=new JLabel();
MI[1][1].setText(String.valueOf(a));
MI[1][1].setBounds(280,210,60,30);
add(MI[1][1]);
}
}
public static void main(String[] ar) {
Matrices2x2 formulario=new Matrices2x2();
formulario.setBounds(0,0,500,400);
formulario.setVisible(true);
}
}

Comentarios
Publicar un comentario