Download presentation
Presentation is loading. Please wait.
Published byJuan Carlos Padilla Modified over 5 years ago
13
Problema: resolver un sistema de ecuaciones lineales
a11x1 + a12x a1nxn = b1 a21x1 + a22x a2nxn = b2 . . . an1x1 + an2x annxn = bn import java.io.*; class Ecuaciones { static public void main(String[] args)throws IOException{ System.out.print("Numero de ecuaciones?"); BufferedReader I = new BufferedReader( new InputStreamReader(System.in)); int n = Integer.parseInt(I.readLine()); double[][] a = new double[n][n]; double[] b = new double[n];
14
//Lectura de los coeficientes y las soluciones
for(int i=0; i<n; ++i){ System.out.println("Ecuacion " + (i+1) + " :"); for(int j=0; j<n; ++j){ System.out.print("a(" + (i+1) + "," + (j+1) + ") ? "); a[i][j] = Integer.parseInt(I.readLine()); } System.out.print("b(" + (i+1) + ") ? "); b[i] = Integer.parseInt(I.readLine());
15
//llamada a método para triangularizar matriz de coeficientes
triangularizar(a,b,n); //calculo de resultados double[] x = new double[n]; for(int i=n-1; i>=0; --i){ double suma = 0; for(int j=i+1; j<n; ++j) suma += x[j]*a[i][j]; x[i] = (b[i]-suma)/a[i][i]; System.out.println("x(" + (i+1) + ") = " + x[i]); } } // main
16
static public void triangularizar(double[][]a, double[]b, int n){
for(int i=0; i<n; ++i){ //dividir fila i por primero de fila i double primero = a[i][i]; for(int j=i; j<n; ++j) a[i][j] /= primero; b[i] /= primero; //fila j = fila j - fila i * primero de fila j for(int j=i+1; j<n; ++j){ primero = a[j][i]; for(int k=i; k<n; ++k) a[j][k] -= a[i][k]*primero; b[j] -= b[i]*primero; } } // class Ecuaciones
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.