Chuong02-lapJacobi

Embed Size (px)

Citation preview

  • 8/10/2019 Chuong02-lapJacobi

    1/3

    /** JACOBI ITERATIVE ALGORITHM 7.1** To solve Ax = b given an initial approximation x(0).** INPUT: the number of equations and unknowns n; the entries* A(I,J), 1

  • 8/10/2019 Chuong02-lapJacobi

    2/3

    /* procedure completed unsuccessfully */ else OUTPUT(N, X2, K, TOL); } return 0;}

    void INPUT(int *OK, double *X1, double A[][11], double *TOL, int *N, int *NN){ int I, J; char AA; char NAME[30]; FILE *INP;

    printf("This is the Jacobi Method for Linear Systems.\n"); *OK = false; printf("The array will be input from a text file in the order:\n"); printf("A(1,1), A(1,2), ..., A(1,n+1), A(2,1), A(2,2), ..., A(2,n+1),\n"); printf("..., A(n,1), A(n,2), ..., A(n,n+1)\n\n"); printf("Place as many entries as desired on each line, but separate ");

    printf("entries with\n"); printf("at least one blank.\n"); printf("The initial approximation should follow in same format.\n\n\n"); printf("Has the input file been created? - enter Y or N.\n"); scanf("%c",&AA); if ((AA == 'Y') || (AA == 'y')) { printf("Input the file name in the form - drive:name.ext\n"); printf("for example: A:DATA.DTA\n"); scanf("%s", NAME); INP = fopen(NAME, "r"); *OK = false; while (!(*OK)) { printf("Input the number of equations - an integer.\n"); scanf("%d", N); if (*N > 0) { for (I=1; I

  • 8/10/2019 Chuong02-lapJacobi

    3/3

    else printf("The program will end so the input file can be created.\n");}

    void OUTPUT(int N, double *X2, int K, double TOL){ int I, J, FLAG; char NAME[30]; FILE *OUP;

    printf("Choice of output method:\n"); printf("1. Output to screen\n"); printf("2. Output to text file\n"); printf("Please enter 1 or 2.\n"); scanf("%d", &FLAG); if (FLAG == 2) { printf("Input the file name in the form - drive:name.ext\n"); printf("for example: A:OUTPUT.DTA\n"); scanf("%s", NAME);

    OUP = fopen(NAME, "w"); } else OUP = stdout; fprintf(OUP, "JACOBI ITERATIVE METHOD FOR LINEAR SYSTEMS\n\n"); fprintf(OUP, "The solution vector is :\n"); for (I=1; I= 0) return val; else return -val;}