Guest User

Untitled

a guest
May 22nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.11 KB | None | 0 0
  1. Const
  2.      maxn = 10;
  3. Type
  4.     Data = Real;
  5.     Matrix = Array[1..maxn, 1..maxn] of Data;
  6.     Vector = Array[1..maxn] of Data;
  7. Procedure ReadSystem(n: Integer; var a: Matrix; var b: Vector);
  8. Var
  9.    i, j, r: Integer;
  10. Begin
  11.      for j:=1 to n do begin
  12.          for i:=1 to n do read(a[i,j]);
  13.          read(b[j])
  14.      end;
  15. End;
  16. Procedure WriteX(n :Integer; x: Vector);
  17. Var
  18.    i: Integer;
  19. Begin
  20.      For i := 1 to n do
  21.          Writeln('x', i, ' = ', x[i]);
  22. End;
  23. Function Seidel(n: Integer; a: Matrix; b: Vector; var x: Vector; e: Data)
  24. :Boolean;
  25. Var
  26.    i, j: Integer;
  27.    s1, s2, s, v, m: Data;
  28. Begin
  29.      For i := 1 to n do begin
  30.          s := 0;
  31.          For j := 1 to n do
  32.              If j <> i then
  33.                 s := s + Abs(a[i, j]);
  34.          If s >= Abs(a[i, i]) then begin
  35.             Seidel := false;
  36.             Exit;
  37.          end;
  38.      end;
  39.      Repeat
  40.          m := 0;
  41.          For i := 1 to n do begin
  42.              s1 := 0;
  43.              s2 := 0;
  44.              For j := 1 to i - 1 do
  45.                  s1 := s1 + a[i, j] * x[j];
  46.              For j := i to n do
  47.                  s2 := s2 + a[i, j] * x[j];
  48.              v := x[i];
  49.              x[i] := x[i] - (1 / a[i, i]) * (s1 + s2 - b[i]);
  50.              If Abs(v - x[i]) > m then
  51.                 m := Abs(v - x[i]);
  52.          end;
  53.      Until m < e;
  54.      Seidel := true;
  55. End;
  56. Var
  57.     n, i: Integer;
  58.     a: Matrix;
  59.     b, x: Vector;
  60.     e: Data;
  61. Begin
  62.       Writeln('Linear Equations Solution using Seidel Method...');
  63.       Writeln('Enter the number of equations:');
  64.       Repeat
  65.         Read(n);
  66.       Until (n > 0) and (n <= maxn);
  67.       Writeln('Enter the approximation level E:');
  68.       Repeat
  69.         Read(e);
  70.       Until (e > 0) and (e < 1);
  71.       Writeln('Enter the extended matrix A:');
  72.       ReadSystem(n, a, b);
  73.       Writeln;
  74.       For i := 1 to n do
  75.           x[i] := 0;
  76.       If Seidel(n, a, b, x, e) then begin
  77.          Writeln('Calculations resulted in...');
  78.          WriteX(n, x);
  79.       end
  80.       else
  81.           Writeln('Seidel`s method is not applicable to this system...');
  82.       Writeln;
  83.       read(n)
  84. End.
Add Comment
Please, Sign In to add comment