PageRenderTime 18ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/MatrizLU.pas

http://github.com/marlonsd/Pascalzim--CLP-
Pascal | 137 lines | 97 code | 33 blank | 7 comment | 3 complexity | 08e6c91c74c958ea0ccbe6d244666bfd MD5 | raw file
  1. // -------------------------------------------------------------
  2. // Trabalho Conceito e Linguagens de Programacao
  3. // Integrantes: Andre Peil, Daniel Retzlaff, Marlon Dias
  4. // -------------------------------------------------------------
  5. Program MatrizLU;
  6. Type matriz = Record
  7. a: array[1..100 , 1..100] of real;
  8. upper: array [1..100 , 1..100] of real;
  9. lower: array [1..100 , 1..100] of real;
  10. End;
  11. Var mat: matriz;
  12. i, j, n : integer;
  13. flag: boolean;
  14. Procedure decompLU ();
  15. Var temp: real;
  16. k: integer;
  17. Begin
  18. For i:= 1 to n do
  19. Begin
  20. For j:= 1 to n do
  21. Begin
  22. if (i <= j) then
  23. Begin
  24. temp := 0;
  25. For k := 1 to i do
  26. Begin
  27. temp := temp + (mat.lower[i, k]*mat.upper[k, j]);
  28. End;
  29. mat.upper[i, j] := mat.a[i, j] - temp;
  30. End
  31. Else
  32. Begin
  33. temp := 0;
  34. For k := 1 to j do
  35. Begin
  36. temp := temp + (mat.lower[i, k] * mat.upper[k, j]);
  37. End;
  38. If (mat.upper[j, j] = 0) then
  39. Begin
  40. writeln('Erro... Divisao por Zero');
  41. flag:=true;
  42. exit;
  43. End
  44. Else
  45. Begin
  46. mat.lower[i, j] := (mat.a[i, j] - temp) / mat.upper [j, j];
  47. End;
  48. End;
  49. End;
  50. End;
  51. End;
  52. Begin
  53. Repeat
  54. write('Tamanho da Matriz: ');
  55. readln(n);
  56. if (n > 100) or (n <= 0) then
  57. writeln('erro... valores validos entre 1 e 100.');
  58. Until ((n <= 100) and (n > 0));
  59. writeln('Matriz A: ');
  60. // Leitura da matriz A e inicializacao de matris LU
  61. For i := 1 to n do
  62. Begin
  63. For j := 1 to n do
  64. Begin
  65. write('A [',i, ',',j, ']: ');
  66. readln(mat.a[i, j]);
  67. mat.lower[i, j] := 0;
  68. mat.upper[i, j] := 0;
  69. End;
  70. mat.lower[i, i] := 1;
  71. End;
  72. // Decomposicao LU
  73. flag:=false;
  74. decompLU();
  75. // Impressao das matrizes
  76. if (flag) then
  77. exit;
  78. writeln('');
  79. writeln('Matriz Comp: ');
  80. For i:= 1 to n do
  81. Begin
  82. writeln('');
  83. For j:= 1 to n do
  84. write(mat.a[i,j]:2:1,' ');
  85. End;
  86. writeln('');
  87. writeln('');
  88. writeln('Matriz Low: ');
  89. For i:= 1 to n do
  90. Begin
  91. writeln('');
  92. For j:= 1 to n do
  93. write(mat.lower[i,j]:2:1,' ');
  94. End;
  95. writeln('');
  96. writeln('');
  97. writeln('Matriz Up: ');
  98. For i:= 1 to n do
  99. Begin
  100. writeln('');
  101. For j:= 1 to n do
  102. write(mat.upper[i,j]:2:1,' ');
  103. End;
  104. End.