Where is the error in this numerical method?

Asked

Viewed 28 times

-3

I’m hours away from finding my mistake in this Jacobi method, I’ve checked with friends, and I can’t find where that mistake could be. The expected outputs are approximately: 0.78, -1.96 , 1.03.

public class metodo_5 {
    public static double dif,e=0.001;
    public static int i=0, j=0, k=0, n=2;
    
    public static double[][] A = new double[][]{
         { 10, 2,3 },{ 1, 5, 1 },{ 2, 3,10 },
        };
    public static double[] b = { 7 ,-8 , 6 };
    public static double[] Xk1 = { 1 , 1 , 1 };
    public static double[] Xk = { 0 , 0 , 0 };
    public static double[][] C = new double[][]{
         { 0, 0,0 },{ 0, 0, 0 },{ 0, 0,0 },
        };
    public static double[] G = { 0, 0, 0 };
    
    public static double[][] geraC (double[][] A){
        for(i=0; i<=n;i++) {
            for(j=0; j<=n;j++) {
                if(i==j) {
                    C[i][j] = 0;
                }
                else {
                    C[i][j] = (A[i][j]/A[i][i])*(-1);
                }   
            }   
        }
    return C;
    }
    public static double[] geraG (double[] b){
        for(i=1; i<=n;i++) {
                G[i] = (b[i]/A[i][i]);
        }   
        
    return G;
    }   
    public static double difmod (double[] Xk1, double[] Xk){
    dif=0;
    for(i=0; i<=n;i++) {
        dif = dif + Math.pow(Xk1[i]-Xk[i],2);
    }
    return Math.sqrt(dif);
    }

    public static void main(String[] args) {
        
        C = geraC(A);
        G = geraG(b);
        do {
            k++;
            for(i=0;i<=n;i++) {
                Xk[i]=Xk1[i];
                Xk1[i]=0;
             }
            for(i=0;i<=n;i++) {
                for(j=0;j<=n;j++) {
                    Xk1[i]= Xk1[i]+(C[i][j]*Xk[j]);
                }
                Xk1[i]=Xk1[i]+G[i];
            }
            
            dif = difmod(Xk1,Xk);
        }while ( dif > e);
        
        System.out.println("Xk1 e igual a :");
        System.out.println(+Xk1[0]);
        System.out.println(+Xk1[1]);
        System.out.println(+Xk1[2]);
        System.out.println("iteracoes: "+k);
        
    }
}
  • 1

    @Felipegambini Stacksnippet should not be used for code formation, please read help before using anything: https://answall.com/editing-help

  • @Thank you very much for your guidance! I beg your pardon.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.