Anda di halaman 1dari 14

UNIVERSIDAD NACIONAL DE INGENIERÍA

FACULTAD DE INGENIERÍA ELECTRICA Y


ELECTRONICA

MA713N
PROGRAMACION DIGITAL

ALUMNO: DIEGO EDUARDO SAMANEZ TUCTO

CODIGO: 20171162J

PROFESOR: TRINI CASTILLO BELSUZARRI


PROBLEMA
ELIMINACION DE GAUSS

RESULTADO
run:
¿Cuantas variables tiene tu sistema?

Ingresa la constante de la posicion: A[1][1]

Ingresa la constante de la posicion: A[1][2]

Ingresa la constante de la posicion: A[1][3]

Ingresa la constante de la posicion: A[1][4]

Ingresa la constante de la posicion: A[2][1]

Ingresa la constante de la posicion: A[2][2]

6
Ingresa la constante de la posicion: A[2][3]

Ingresa la constante de la posicion: A[2][4]

Ingresa la constante de la posicion: A[3][1]

Ingresa la constante de la posicion: A[3][2]

Ingresa la constante de la posicion: A[3][3]

Ingresa la constante de la posicion: A[3][4]

1.0x + 2.0y + 3.0z = 4.0

5.0x + 6.0y + 7.0z = 8.0

9.0x + 1.0y + 2.0z = 3.0

Renglon 1 entre el pivote

1.000 2.000 3.000 4.000

5.000 6.000 7.000 8.000

9.000 1.000 2.000 3.000

Haciendo ceros

1.000 2.000 3.000 4.000

0.000 -4.000 -8.000 -12.000

0.000 -17.000 -25.000 -33.000

Renglon 2 entre el pivote

1.000 2.000 3.000 4.000

-0.000 1.000 2.000 3.000


0.000 -17.000 -25.000 -33.000

Haciendo ceros

1.000 0.000 -1.000 -2.000

-0.000 1.000 2.000 3.000

0.000 0.000 9.000 18.000

Renglon 3 entre el pivote

1.000 0.000 -1.000 -2.000

-0.000 1.000 2.000 3.000

0.000 0.000 1.000 2.000

Haciendo ceros

1.000 0.000 0.000 0.000

-0.000 1.000 0.000 -1.000

0.000 0.000 1.000 2.000

La variable X1 es: 0.0

La variable X2 es: -1.0

La variable X3 es: 2.0

BUILD SUCCESSFUL (total time: 11 seconds)

CODIGO GAUSS
package gauss;

import java.util.Scanner;

public class Gauss {

static void MostrarMatriz(float matriz[][]) {

for (int x = 0; x < matriz.length; x++) {


for (int y = 0; y < matriz[0].length; y++) {

System.out.printf(" %8.3f" , matriz[x][y]);

System.out.println("");

static void pivote(float matriz[][], int piv) {

float temp = 0;

temp = matriz[piv][piv];

for (int j = 0; j < matriz[0].length; j++) {

matriz[piv][j] = matriz[piv][j] / temp;

static void hacerceros(float matriz[][], int piv) {

for (int x = 0; x < matriz.length; x++) {

if (x != piv) {

float c = matriz[x][piv];

for (int z = 0; z < matriz[0].length; z++) {

matriz[x][z] = ((-1 * c) * matriz[piv][z]) + matriz[x][z];

public static void main(String args[]) {

Scanner leer = new Scanner(System.in);

int cantvar = 0, piv = 0;

float matriz[][];
System.out.println("\n¿Cuantas variables tiene tu sistema?");

cantvar = leer.nextInt();

matriz = new float[cantvar][cantvar + 1];

// llenando matriz

for (int x = 0; x < matriz.length; x++) {

for (int y = 0; y < matriz[0].length; y++) {

System.out.println("Ingresa la constante de la posicion: A[" + (x + 1) + "][" + (y + 1) + "]");

matriz[x][y] = leer.nextFloat();

int y=0;

for (int x = 0; x < matriz.length; x++) {

System.out.println(matriz[x][y]+"x + "+matriz[x][y+1]+"y + "+matriz[x][y+2]+"z "+" = " +


matriz[x][y+3]);

System.out.println();

for (int a = 0; a < matriz.length; a++) {

pivote(matriz, piv);

System.out.println("\tRenglon " + (a + 1) + " entre el pivote");

MostrarMatriz(matriz);

System.out.println("");

System.out.println("\tHaciendo ceros");

hacerceros(matriz, piv);
MostrarMatriz(matriz);

System.out.println("");

piv++;

for (int x = 0; x < matriz.length; x++) {

System.out.println("La variable X" + (x + 1) + " es: " + matriz[x][cantvar]);

PROBLEMA
DETERMINANTE DE UNA MATRIZ

run:

Digite el orden de la matriz :

Digite el numero en la posicion [0][0]

Digite el numero en la posicion [0][1]

Digite el numero en la posicion [0][2]

1
Digite el numero en la posicion [1][0]

Digite el numero en la posicion [1][1]

Digite el numero en la posicion [1][2]

Digite el numero en la posicion [2][0]

Digite el numero en la posicion [2][1]

Digite el numero en la posicion [2][2]

1.000 0.000 1.000

1.000 2.000 3.000

4.000 6.000 5.000

El determinante es : -10.0

4.000 6.000 5.000

0.000 -1.500 -0.250

0.000 0.000 1.667

BUILD SUCCESSFUL (total time: 6 seconds)

CODIGO DET
package determinante;

import java.util.Scanner;
public class Det {
public static double[][] intercambiarFilasMatriz(double[][] matriz, int i, int j) {
double temp;
for (int k = 0; k < matriz[0].length; k++) {
temp = matriz[i][k];
matriz[i][k] = matriz[j][k];
matriz[j][k] = temp;
}
return matriz;
}

public static void imprimirMatriz(double[][] matriz) {


for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[0].length; j++) {
System.out.print(matriz[i][j] + " ");
}
System.out.println("");
}
}

public static int pivoteMatriz(double[][] matriz, int i) {


int piv = i;
double v = matriz[i][i];
for (int j = i + 1; j < matriz.length; j++) {
if (Math.abs(matriz[j][i]) > v) {
v = matriz[j][i];
piv = j;
}
}
return piv;
}

/**
*
* @param m es una matriz cuadrada
* @return
*/
public static int eliminaAdelante(double[][] m) {
int numeroIntercambios = 0;
int filaPivote = -1;
for (int columnaEvaluada = 0; columnaEvaluada < m.length; columnaEvaluada++) {
filaPivote = pivoteMatriz(m, columnaEvaluada);
if (filaPivote != columnaEvaluada) {
m = intercambiarFilasMatriz(m, columnaEvaluada, filaPivote);
numeroIntercambios++;
}
for (int i = columnaEvaluada + 1; i < m.length; i++) {
for (int j = columnaEvaluada + 1; j < m.length; j++) {
if (m[columnaEvaluada][columnaEvaluada] == 0) {
System.out.println("Error, división por cero");
System.exit(1);
} else {
m[i][j] = m[i][j] - (m[i][columnaEvaluada] * m[columnaEvaluada][j] /
m[columnaEvaluada][columnaEvaluada]);
}
}
m[i][columnaEvaluada] = 0;
}
}
return numeroIntercambios;
}

public static double det(double[][] m) {


int numeroIntercambios = eliminaAdelante(m);
double det = 1;
for (int i = 0; i < m.length; i++) {
det *= m[i][i];
}
if (numeroIntercambios % 2 == 1) {
det = -det;
}
return det;
}
/*
void triangular(float a[][]){
int i,j,k,n;
float t;
for(k=1;k<=a.length;k++){
for(i=k;i<a.length;i++){
for(j=i+1;j<=a.length;j++){
t=a[j][i] / a[i][i];
a[j][k]=a[j][k]-t*a[i][k];
}
}
}
}
float determinante ( float a[][], int n){
int i,j;float DET;
DET=1;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==j){
DET = a[i][j]*DET;
}
}
}
return DET;
}
*/
public static void main(String[] args ){
System.out.println("Digite el orden de la matriz :");
Scanner sca =new Scanner(System.in);
int n= sca.nextInt();

double[][] mat = new double[n][n] ;


for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
System.out.println("Digite el numero en la posicion ["+ i+ "]["+j+"]" );
mat[i][j]=sca.nextFloat();

}
}

for (int i = 0; i < mat.length; i++) {


for (int j = 0; j < mat[0].length; j++) {
System.out.printf("%10.3f",mat[i][j]);

}
System.out.println();
}

System.out.println();
System.out.println();

System.out.println("El determinante es : "+Det.det(mat) );

for (int i = 0; i < mat.length; i++) {


for (int j = 0; j < mat[0].length; j++) {
System.out.printf("%10.3f",mat[i][j]);

}
System.out.println();
}

Anda mungkin juga menyukai