⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alg072.txt

📁 Numerical Anaysis 8th Edition Burden and Faires (Maple Source)
💻 TXT
字号:
> restart;
> # GAUSS-SEIDEL ITERATIVE TECHNIQUE ALGORITHM 7.2
> #
> # To solve Ax = b given an initial approximation x(0).
> #
> # INPUT:   the number of equations and unknowns n; the entries
> #          A(I,J), 1<=I, J<=n, of the matrix A; the entries
> #          B(I), 1<=I<=n, of the inhomogeneous term b; the
> #          entries XO(I), 1<=I<=n, of x(0); tolerance TOL;
> #          maximum number of iterations N.
> #
> #  OUTPUT: the approximate solution X(1),...,X(n) or a message
> #          that the number of iterations was exceeded.
> alg072 := proc() local AA, OK, NAME, INP, N, I, J, A, X1, TOL, NN, K, ERR, S, FLAG, OUP;
> printf(`This is the Gauss-Seidel Method for Linear Systems.\n`);
> 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),..., A(n,1), A(n,2), ..., A(n,n+1)\n`);
> printf(`Place as many entries as desired on each line, but separate\n`);
> printf(`entries with `);
> printf(`at least one blank.\n\n\n`);
> printf(`The initial approximation should follow in same format\n`);
> printf(`Has the input file been created? - enter Y or N.\n`);
> AA := scanf(`%c`)[1];
> OK := FALSE;
> if AA = "Y" or AA = "y" then
> printf(`Input the file name in the form - drive:\\name.ext\n`);
> printf(`for example:   A:\\DATA.DTA\n`);
> NAME := scanf(`%s`)[1];
> INP := fopen(NAME,READ,TEXT);
> OK := FALSE;
> while OK = FALSE do
> printf(`Input the number of equations - an integer.\n`);
> N := scanf(`%d`)[1];
> if N > 0 then
> for I from 1 to N do
> for J from 1 to N+1 do
> A[I-1,J-1] := fscanf(INP, `%f`)[1];
> od;
> od;
> for I from 1 to N do
> X1[I-1] := fscanf(INP, `%f`)[1];
> od;
> OK := TRUE;
> fclose(INP);
> else
> printf(`The number must be a positive integer\n`);
> fi;
> od;
> OK := FALSE;
> while OK = FALSE do
> printf(`Input the tolerance.\n`);
> TOL := scanf(`%f`)[1];
> if TOL > 0 then
> OK := TRUE;
> else
> printf(`Tolerance must be a positive.\n`);
> fi;
> od;
> OK := FALSE;
> while OK = FALSE do
> printf(`Input maximum number of iterations.\n`);
> NN := scanf(`%d`)[1];
> if NN > 0 then
> OK := TRUE;
> else
> printf(`Number must be a positive integer.\n`);
> fi;
> od;
> else
> printf(`The program will end so the input file can be created.\n`);
> fi;
> if OK = TRUE then
> # Step 1
> K := 1;
> OK := FALSE;
> # Step 2
> while OK = FALSE and K <= NN do
> # ERR is used to test accuracy - it measures the infinity norm
> ERR := 0;
> # Step 3
> for I from 1 to N do
> S := 0;
> for J from 1 to N do
> S := S-A[I-1,J-1]*X1[J-1];
> od;
> S := (S+A[I-1,N])/A[I-1,I-1];
> if abs(S) > ERR then
> ERR  := abs(S);
> fi;
> X1[I-1] := X1[I-1] + S;
> od;
> # Step 4
> if ERR <= TOL then
> # Process is completed successfully.
> OK := TRUE;
> else
> # Step 5
> K := K+1;
> # Step 6 is not used since only one vector is needed.
> fi;
> od;
> if OK = FALSE then
> printf(`Maximum Number of Iterations Exceeded.\n`);
> # Step 7
> # Process completed unsuccessfully.
> else
> 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`);
> FLAG := scanf(`%d`)[1];
> if FLAG = 2 then
> printf(`Input the file name in the form - drive:\\name.ext\n`);
> printf(`for example:   A:\\OUTPUT.DTA\n`);
> NAME := scanf(`%s`)[1];
> OUP := fopen(NAME,WRITE,TEXT);
> else
> OUP := default;
> fi;
> fprintf(OUP, `GAUSS-SEIDEL METHOD FOR LINEAR SYSTEMS\n\n`);
> fprintf(OUP, `The solution vector is :\n`);
> for I from 1 to N do
> fprintf(OUP, ` %11.8f`, X1[I-1]);
> od;
> fprintf(OUP, `\nusing %d iterations\n`, K);
> fprintf(OUP, `with Tolerance  %.10e in infinity-norm\n`, TOL);
> if OUP <> default then
> fclose(OUP):
> printf(`Output file %s created successfully`,NAME);
> fi;
> fi;
> fi;
> RETURN(0);
> end;
> alg072();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -