Java - Metodo jacobi podrian ayudarme pasarlo en el lenguaje Java, esta en lenguaje python

 
Vista:
Imágen de perfil de Anonimo

Metodo jacobi podrian ayudarme pasarlo en el lenguaje Java, esta en lenguaje python

Publicado por Anonimo (5 intervenciones) el 03/03/2023 19:24:32
f1 =lambda x,y,z: (1+y+z)/3
f2=lambda x,y,z: (3+x-z)/3
f3=lambda x,y,z: (7-2*x-y)/4

#Vector Inicial
x0 = 0
y0 = 0
z0 = 0
count=1

#Leemos la tolerancia de Error
e = float(input('Error de Tolerancia: '))

#Implementacion del metodo Jacobi
print('\n-\tx\ty\tz\n')

condition = True

print('%d\t%0.4f\t%0.4f\t%0.4f\n' %((count-1), x0, y0, z0))
while condition:
x1 = f1(x0,y0,z0)
y1=f2(x0,y0,z0)
z1=f3(x0,y0,z0)
print('%d\t%0.4f\t%0.4f\t%0.4f\n' %(count, x1,y1,z1))
e1=abs(x0-x1)
e2=abs(y0-y1);
e3=abs(z0-z1);

count +=1
x0=x1
y0=y1
z0=z1

condition =e1>e and e2>e and e3>e

print('\nSolución: x=%0.3f,y=%0.3f, z=%0.3f\n'%(x1,y1,z1))
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Richtofen

Metodo jacobi podrian ayudarme pasarlo en el lenguaje Java, esta en lenguaje python

Publicado por Richtofen (18 intervenciones) el 04/03/2023 21:30:45
Con esto te debería valer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class Jacobi {
 
  private static double f1(double y, double z) {
    return (1 + y + z) / 3;
  }
 
  private static double f2(double x, double z) {
    return (3 + x - z) / 3;
  }
 
  private static double f3(double x, double y) {
    return (7 - 2 * x - y) / 4;
  }
 
  public static void main(String[] args) {
    double x0 = 0, x1 = 0;
    double y0 = 0, y1 = 0;
    double z0 = 0, z1 = 0;
    int count = 1;
    try {
      // Leemos la tolerancia del error
      System.out.print("Introduzca la tolerancia del error: ");
      float e = Float.parseFloat(System.console().readLine().trim());
      boolean condition = true;
      // Implementacion del metodo Jacobi
      System.out.println("\n-\tx\ty\tz\n");
      System.out.println(count + "\t" + x0 + "\t" + y0 + "\t" + z0 + "\n");
      while (condition) {
        x1 = f1(y0, z0);
        y1 = f2(x0, z0);
        z1 = f3(x0, y0);
        System.out.println(count + "\t" + x1 + "\t" + y1 + "\t" + z1 + "\n");
        double e1 = Math.abs(x0 - x1);
        double e2 = Math.abs(y0 - y1);
        double e3 = Math.abs(z0 - z1);
        count++;
        x0 = x1;
        y0 = y1;
        z0 = z1;
        condition = (e1 > e) && (e2 > e) && (e3 > e);
      }
      System.out.println("\nSolución: x = " + x1 + ", y = " + y1 + ", z = " + z1 + "\n");
    } catch (NumberFormatException e) {
      System.err.println("ERROR: La entrada no es un numero decimal: " + e.getMessage());
    }
 
  }
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar