From ada86c242047b6106f8ac147f0244e46ab08c304 Mon Sep 17 00:00:00 2001 From: AryDaniel Date: Fri, 4 Jul 2025 22:00:00 -0700 Subject: [PATCH 1/4] feat(newton): implement Newton-Raphson method --- .idea/misc.xml | 6 + .idea/vcs.xml | 6 + .../evaluation1-secant-newton.java | 106 ++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 src/evaluation1/evaluation1-secant-newton.java diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7c623dd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c8397c9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/evaluation1/evaluation1-secant-newton.java b/src/evaluation1/evaluation1-secant-newton.java new file mode 100644 index 0000000..2fa301c --- /dev/null +++ b/src/evaluation1/evaluation1-secant-newton.java @@ -0,0 +1,106 @@ +package evaluation1; +import java.util.*; +import java.text.DecimalFormat; + +public class evaluation1-secant-newton { + static DecimalFormat f = new DecimalFormat("#0.0000"); + static Scanner input = new Scanner(System.in); + + public static void main(String[] args) { + String pregunta; + int metodo; + + imprimirPantallaInicio("de su eleccion"); + System.out.println("Ingrese la pregunta: "); + pregunta = input.nextLine(); + + System.out.print("Selecione el metodo para realizar el problema: " + + "\n(1)->Metodo Newton-Raphson" + + "\n(2)->Metodo Secante" + + "\n(10)->Salir" + + "\nOpcion-> "); + metodo = input.nextInt(); + + while(true) { + switch(metodo) { + case 1: + System.out.println("Se ha seleccionado Metodo de Newton-Raphson, ingrese los siguientes datos: "); + System.out.println(f.format(metodoNewton(pregunta))); + break; + case 2: + System.out.println("Se ha seleccionado Metodo de la Secante, ingrese los siguientes datos: "); + break; + case 10: + System.out.println("Saliendo..."); + return; + default: + System.out.println("Seleccione un metodo valido (1,2,10)..."); + } + + System.out.println("Selecione el metodo para realizar el problema: " + + "\n(1)->Metodo Newton-Raphson" + + "\n(2)->Metodo Secante" + + "\n(10)->Salir"); + metodo = input.nextInt(); + + } + + + + + } + + static void imprimirPantallaInicio(String tipo) { + System.out.println("\n\n\t\tInstituto Tecnológico de Culiacán\n" + + "\t\tIng. En Sistemas Computacionales\n" + + "Audelo Salas Diego Alejandro\n" + + "Brandon Gael Uriarte Lopez\n" + + "Raices de una Ecuación. Método de " + tipo + "\n" + + "Métodos númericos\n" + + "De 13:00 a 14:00 horas. " + "\n" + + "\nEste programa ejecuta el proceso de cálculo de Racies de una ecuación utilizando el método de " + +tipo+"\n"); + } + + static double metodoNewton(String pregunta) { + int contador = 1, tCalculos; + double x1,fx1,fx2,dfx1,x2, error; + System.out.print("Ingrese x1 -> "); + x1 = input.nextDouble(); + System.out.print("\nIngrese el error -> "); + error = input.nextDouble(); + System.out.print("\nIngrese el total de calculos -> "); + tCalculos = input.nextInt(); + + imprimirPantallaInicio("Newton-Raphson"); + + fx1=(-0.000234)*(Math.pow((x1-80),2))+1.5; + dfx1 = -0.000468*x1 + 0.03744; + System.out.println("PREGUNTA: " + pregunta); + System.out.println("| Nc\t|\tx1\t|\tf(x1)\t|\tf'(x1)\t|\tx2\t|\tf(x2)\t|"); + + do{ + + x2 = x1 - (fx1/dfx1); + fx2 = ((-0.000234)*(Math.pow((x2-80),2)))+1.5; + System.out.println("| "+ contador +"\t| "+f.format(x1)+"\t| "+f.format(fx1)+"\t| "+f.format(dfx1)+"\t| "+f.format(x2)+"\t| "+f.format(fx2)+"\t|"); + if(Math.abs(fx2) > error) { + contador ++; + x1=x2; + fx1=fx2; + dfx1 = -0.000468*x2 + 0.03744; + }else if (contador > tCalculos){ + System.out.println("Se han superado la cantidad de cálculos necesarios..."); + return -1; + }else { + System.out.println("La Raíz de la Ecuación = " + x2); + return x2; + } + + }while(Math.abs(fx2) > error && contador < tCalculos); + + + + return 0; + } +} From 3b147cb6b971506672212e52f8a9e7f7d70bb806 Mon Sep 17 00:00:00 2001 From: AryDaniel Date: Fri, 4 Jul 2025 22:12:13 -0700 Subject: [PATCH 2/4] feat(evaluation1): add secant method implementation --- .../evaluation1-secant-newton.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/evaluation1/evaluation1-secant-newton.java b/src/evaluation1/evaluation1-secant-newton.java index 2fa301c..5d75874 100644 --- a/src/evaluation1/evaluation1-secant-newton.java +++ b/src/evaluation1/evaluation1-secant-newton.java @@ -29,6 +29,7 @@ public static void main(String[] args) { break; case 2: System.out.println("Se ha seleccionado Metodo de la Secante, ingrese los siguientes datos: "); + System.out.println(f.format(metodoSecante(pregunta))); break; case 10: System.out.println("Saliendo..."); @@ -103,4 +104,52 @@ static double metodoNewton(String pregunta) { return 0; } + static double metodoSecante(String pregunta) { + + int contador = 1, tCalculos; + double x1, x2 , x3, error; + double fx1, fx2,fx3; + + System.out.print("\nIngrese x1 -> "); + x1 = input.nextDouble(); + System.out.print("\nIngrese x2 -> "); + x2 = input.nextDouble(); + System.out.print("\nIngrese el error -> "); + error = input.nextDouble(); + System.out.print("\nIngrese el total de calculos -> "); + tCalculos = input.nextInt(); + imprimirPantallaInicio("la Secante"); + + + fx1=((-0.000234)*(Math.pow((x1-80),2)))+1.5; + fx2=((-0.000234)*(Math.pow((x2-80),2)))+1.5; + + System.out.println("PREGUNTA: " + pregunta); + + System.out.println("| Nc\t|\tx1\t|\tx2\t|\tf(x1)\t|\tf(x2)\t|\tx3\t|\tf(x3)\t|"); + + do { + + x3=x1-((x1-x2)*fx1)/(fx1-fx2); + fx3=((-0.000234)*(Math.pow((x3-80),2)))+1.5; + System.out.println("| "+ contador +"\t| "+f.format(x1)+"\t| "+f.format(x2)+"\t| "+f.format(fx1)+"\t| "+f.format(fx2)+"\t| "+f.format(x3)+"\t| "+f.format(fx3)+"\t|"); + if(Math.abs(fx3) > error && contador <= tCalculos) { + contador++; + x1=x2; + x2=x3; + fx1=fx2; + fx2=fx3; + }else if (contador > tCalculos){ + System.out.println("Se han superado la cantidad de cálculos necesarios..."); + return -1; + }else { + System.out.println("La Raíz de la Ecuación = " + x3); + return x3; + } + }while(Math.abs(fx2) > error && contador <= tCalculos); + + System.out.println("No se ha encontrado la raíz en el rango proupuesto (" + x1+ " - " + x2 + ")"); + + return -1; + } } From 6eccf7f3a87af7ec03b48033b0508472087a6b90 Mon Sep 17 00:00:00 2001 From: AryDaniel Date: Thu, 31 Jul 2025 23:40:46 -0700 Subject: [PATCH 3/4] feat(gauss-jordan): implement Gauss-Jordan elimination method --- .../evaluation2-gaussseidel-jordan.java | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/evaluation2/evaluation2-gaussseidel-jordan.java diff --git a/src/evaluation2/evaluation2-gaussseidel-jordan.java b/src/evaluation2/evaluation2-gaussseidel-jordan.java new file mode 100644 index 0000000..7d3b160 --- /dev/null +++ b/src/evaluation2/evaluation2-gaussseidel-jordan.java @@ -0,0 +1,152 @@ +package com.mycompany.mavenproject3; + +import java.text.DecimalFormat; +import java.util.*; + +public class Mavenproject3 { + public static void main(String[] args) { + String pregunta; + DecimalFormat df = new DecimalFormat("#.000000"); + System.out.println("\t\tInstituto Tecnol\u00f3gico de Culiac\u00e1n"); + System.out.println("\t\tIngenieria en Sistemas "); + System.out.println("{nombre}"); + System.out.println("M\u00e9todos n\u00famericos"); + System.out.println("{horario}"); + + Scanner leer = new Scanner(System.in); + + int metodo; + System.out.println("Teclee la pregunta"); + pregunta = leer.nextLine(); + + System.out.print(""" + Selecione el metodo para realizar el problema: + (1)->Metodo Gauss Jordan + (2)->Metodo Gauss Seidel + (0)->Salir + Opcion-> """); + metodo = leer.nextInt(); + while (true) { + switch (metodo) { + case 1: + System.out.println("------------------------------------------------------------------"); + System.out.println(""" + \t\tInstituto Tecnol\u00f3gico de Culiac\u00e1n + \t\tIngenieria en Sistemas + Brandon Gael Uriarte Lopez + M\u00e9todos n\u00famericos + De 13:00 a 14:00 horas. + """); + System.out.println("------------------------------------------------------------------"); + int Orden = 0; + + System.out.println("Se ha seleccionado Metodo Gauss Jordan, ingrese los siguientes datos: "); + System.out.println("Elija el orden de la matriz"); + Orden = leer.nextInt(); + String[] concepto = new String[Orden]; + + for (int i = 0; i < Orden; i++) { + System.out.println("ingrese el concepto de " + (i + 1)); + concepto[i] = leer.next(); + } + System.out.println("------------------------------------------------------------------"); + + double matriz[][] = new double[Orden][Orden + 1]; + for (int i = 0; i < Orden; i++) { + for (int j = 0; j < Orden + 1; j++) { + System.out.println("inserte el dato " + "[" + (i + 1) + "]" + "[" + (j + 1) + "]"); + matriz[i][j] = leer.nextDouble(); + } + } + System.out.println("------------------------------------------------------------------"); + System.out.println("PREGUNTA: " + pregunta); + System.out.println("Matriz inicial"); + for (int i = 0; i < Orden; i++) { + for (int j = 0; j < Orden + 1; j++) { + System.out.print("[" + matriz[i][j] + "] "); + } + System.out.println(" "); + } + double piv = 0; + double Ecero = 0; + // n son las filas, filas izquierda derecha + for (int k = 0; k < Orden - 1; k++) { + + piv = matriz[k][k]; + for (int f = k + 1; f < Orden; f++) { + Ecero = matriz[f][k]; + for (int c = k; c < Orden + 1; c++) { + matriz[f][c] = (piv * matriz[f][c]) - (Ecero * matriz[k][c]); + } + } + } + System.out.println("------------------------------------------------------------------"); + System.out.println("matriz ceros diagonal abajo"); + for (int i = 0; i < Orden; i++) { + for (int j = 0; j < Orden + 1; j++) { + System.out.print("[" + matriz[i][j] + "] "); + } + System.out.println(" "); + } + + double factor = 0; + for (int k = Orden - 1; k >= 1; k--) { + piv = matriz[k][k]; + for (int f = 0; f < k; f++) { + factor = matriz[f][k] / piv; + for (int c = k - 1; c < Orden + 1; c++) { + matriz[f][c] = matriz[f][c] - (factor * matriz[k][c]); + } + } + } + System.out.println("------------------------------------------------------------------"); + System.out.println("Matriz ceros diagonal arriba"); + for (int i = 0; i < Orden; i++) { + for (int j = 0; j < Orden + 1; j++) { + System.out.print("[" + matriz[i][j] + "] "); + } + System.out.println(" "); + } + + for (int f = 0; f < Orden; f++) { + matriz[f][Orden] = matriz[f][Orden] / matriz[f][f]; + matriz[f][f] = matriz[f][f] / matriz[f][f]; + } + System.out.println("------------------------------------------------------------------"); + System.out.println("Matriz identidad"); + for (int i = 0; i < Orden; i++) { + for (int j = 0; j < Orden + 1; j++) { + System.out.print("[" + matriz[i][j] + "] "); + } + System.out.println(); + } + System.out.println("Resultados: "); + for (int i = 0; i < Orden; i++) { + System.out.println(concepto[i] + " = " + matriz[i][Orden]); + } + System.out.println("------------------------------------------------------------------"); + System.out.println("------------------------------------------------------------------"); + System.out.print(""" + Selecione el metodo para realizar el problema: + (1)->Metodo Gauss Jordan + (2)->Metodo Gauss Seidel + (0)->Salir + Opcion-> """); + metodo = leer.nextInt(); + System.out.println("------------------------------------------------------------------"); + System.out.println("------------------------------------------------------------------"); + + break; + + + case 2: + + case 0: + System.out.println("Saliendo..."); + return; + default: + System.out.println("Seleccione un metodo valido (1,2,0)..."); + } + } + } +} \ No newline at end of file From 741f80785dfed0cec748167090586bd81510d9e2 Mon Sep 17 00:00:00 2001 From: AryDaniel Date: Thu, 31 Jul 2025 23:42:09 -0700 Subject: [PATCH 4/4] feat(gauss-seidel): develop iterative Gauss-Seidel method for solving linear systems --- .../evaluation2-gaussseidel-jordan.java | 111 ++++++++++++++++-- 1 file changed, 103 insertions(+), 8 deletions(-) diff --git a/src/evaluation2/evaluation2-gaussseidel-jordan.java b/src/evaluation2/evaluation2-gaussseidel-jordan.java index 7d3b160..a85ae58 100644 --- a/src/evaluation2/evaluation2-gaussseidel-jordan.java +++ b/src/evaluation2/evaluation2-gaussseidel-jordan.java @@ -30,13 +30,11 @@ public static void main(String[] args) { switch (metodo) { case 1: System.out.println("------------------------------------------------------------------"); - System.out.println(""" - \t\tInstituto Tecnol\u00f3gico de Culiac\u00e1n - \t\tIngenieria en Sistemas - Brandon Gael Uriarte Lopez - M\u00e9todos n\u00famericos - De 13:00 a 14:00 horas. - """); + System.out.println("\t\tInstituto Tecnol\u00f3gico de Culiac\u00e1n"); + System.out.println("\t\tIngenieria en Sistemas "); + System.out.println("{nombre}"); + System.out.println("M\u00e9todos n\u00famericos"); + System.out.println("{horario}"); System.out.println("------------------------------------------------------------------"); int Orden = 0; @@ -137,13 +135,110 @@ public static void main(String[] args) { System.out.println("------------------------------------------------------------------"); break; + case 2: + System.out.println("------------------------------------------------------------------"); + System.out.println("\t\tInstituto Tecnol\u00f3gico de Culiac\u00e1n"); + System.out.println("\t\tIngenieria en Sistemas "); + System.out.println("{nombre}"); + System.out.println("M\u00e9todos n\u00famericos"); + System.out.println("{horario}"); + System.out.println("------------------------------------------------------------------"); + System.out.println("Se ha seleccionado Metodo de Gauss Seidel, ingrese los siguientes datos: "); + int orden = 0; + System.out.println("Elija el orden de la matriz"); + orden = leer.nextInt(); + double[] Vant = new double[orden]; + double[] Vact = new double[orden]; + String[] Concepto = new String[orden]; + double[][] Matriz = new double[orden][orden + 1]; + for (int i = 0; i < orden; i++) { + System.out.println("ingrese el concepto de " + (i + 1)); + Concepto[i] = leer.next(); + } + for (int i = 0; i < orden; i++) { + System.out.println("ingrese el valor inicial " + (i + 1) + ":"); + Vant[i] = leer.nextDouble(); + } + System.out.print("Ingrese el Error: "); + Double Error = leer.nextDouble(); + System.out.print("Ingrese el maximo de calculos: "); + int Tc = leer.nextInt(); + System.out.println("------------------------------------------------------------------"); + for (int i = 0; i < orden; i++) { + for (int j = 0; j < orden + 1; j++) { + System.out.println("inserte el dato " + "[" + (i + 1) + "]" + "[" + (j + 1) + "]"); + Matriz[i][j] = leer.nextDouble(); + } + } + System.out.println("------------------------------------------------------------------"); + System.out.println("PREGUNTA: " + pregunta); + int nc = 0; + double ErrorTotal = 0; + for (int f = 0; f < orden; f++) { + Vact[f] = 0; + } + System.out.println(""); + do { + for (int f = 0; f < orden; f++) { + double Suma = 0; + double Coef = Matriz[f][f]; + Suma = Suma + Matriz[f][orden]; + for (int c = 0; c < orden; c++) { + if (f == c) { + } else { + if (c < f) { + Suma = Suma + ((Matriz[f][c] * -1) * Vact[c]); + } else { + Suma = Suma + ((Matriz[f][c] * -1) * Vant[c]); + } + } + } + Suma = Suma / Coef; + Vact[f] = Suma; + } + ErrorTotal = 0; + for (int p = 0; p < orden; p++) { + ErrorTotal = ErrorTotal + Math.abs(Math.abs(Vact[p]) - Math.abs(Vant[p])); + } + if (nc == 0) { + for (int i = 0; i < orden; i++) { + System.out.print(" " + Concepto[i]); + } + } + System.out.println(""); + nc = nc + 1; - case 2: + System.out.print(nc + " "); + for (int p = 0; p < orden; p++) { + System.out.print(df.format(Vant[p]) + " "); + Vant[p] = Vact[p]; + } + System.out.println(df.format(ErrorTotal)); + } while (ErrorTotal > Error && nc < Tc); + if (ErrorTotal <= Error) { + System.out.println("\nResultados:"); + for (int p = 0; p < orden; p++) { + System.out.println(Concepto[p] + "= " + df.format(Vant[p])); + } + } else { + System.out.println("El proceso supero el numero maximo de calculos y no encontro la mejor aproximacion."); + } + System.out.println("------------------------------------------------------------------"); + System.out.println("------------------------------------------------------------------"); + System.out.print(""" + Selecione el metodo para realizar el problema: + (1)->Metodo Gauss Jordan + (2)->Metodo Gauss Seidel + (0)->Salir + Opcion-> """); + metodo = leer.nextInt(); + break; case 0: System.out.println("Saliendo..."); return; + default: System.out.println("Seleccione un metodo valido (1,2,0)..."); }