PageRenderTime 76ms CodeModel.GetById 72ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/MatrizLU.pas

http://github.com/marlonsd/Pascalzim--CLP-
Pascal | 137 lines | 104 code | 33 blank | 0 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
  6Program MatrizLU;
  7Type matriz = Record
  8	a: array[1..100 , 1..100] of real;
  9	upper: array [1..100 , 1..100] of real;
 10	lower: array [1..100 , 1..100] of real;
 11End;
 12
 13
 14 Var mat: matriz;
 15     i, j, n : integer;
 16     flag: boolean;
 17
 18
 19
 20 Procedure decompLU ();
 21	Var temp: real;
 22		k: integer;
 23	
 24	Begin 				
 25		For i:= 1 to n do
 26			Begin
 27				For j:= 1 to n do
 28					Begin
 29					if (i <= j) then
 30						Begin
 31							temp := 0;
 32							For k := 1 to i do
 33								Begin
 34								temp := temp + (mat.lower[i, k]*mat.upper[k, j]);
 35								End;
 36							mat.upper[i, j] := mat.a[i, j] - temp;	
 37						End
 38						Else
 39							Begin
 40								temp := 0;
 41								For k := 1 to j do
 42									Begin
 43									temp := temp + (mat.lower[i, k] * mat.upper[k, j]);
 44									End;
 45								If (mat.upper[j, j] = 0) then
 46									Begin
 47										writeln('Erro... Divisao por Zero');
 48										flag:=true;
 49										exit;
 50									End
 51									Else
 52										Begin
 53											mat.lower[i, j] := (mat.a[i, j] - temp) / mat.upper [j, j];
 54										End;
 55							End;
 56						
 57					
 58					End;
 59			End;
 60
 61		
 62	End;	
 63		
 64 Begin
 65	
 66	Repeat
 67		write('Tamanho da Matriz: ');
 68		readln(n);
 69		if (n > 100) or (n <= 0) then
 70			writeln('erro... valores validos entre 1 e 100.');
 71			
 72	Until ((n <= 100) and (n > 0)); 
 73		
 74	
 75  writeln('Matriz A: ');
 76  // Leitura da matriz A e inicializacao de matris LU
 77
 78  For i := 1 to n do
 79    Begin
 80		For j := 1 to n do
 81
 82		Begin
 83			write('A [',i, ',',j, ']: ');
 84			readln(mat.a[i, j]);
 85			mat.lower[i, j] := 0;
 86			mat.upper[i, j] := 0;
 87		End;
 88    mat.lower[i, i] := 1;
 89	End;
 90	
 91	// Decomposicao LU
 92	flag:=false;
 93	decompLU();
 94
 95  // Impressao das matrizes
 96  if (flag) then
 97	exit;
 98
 99writeln('');
100	
101  writeln('Matriz Comp: ');
102  
103  For i:= 1 to n do
104	Begin
105		writeln('');
106		For j:= 1 to n do 
107			write(mat.a[i,j]:2:1,'  ');
108	End;
109  writeln('');
110  writeln('');
111  
112  writeln('Matriz Low: ');
113
114  For i:= 1 to n do
115	Begin
116		writeln('');
117		For j:= 1 to n do
118			write(mat.lower[i,j]:2:1,'  ');
119	End;
120  
121  writeln('');
122  writeln('');
123  writeln('Matriz Up: ');
124
125   For i:= 1 to n do
126	Begin
127		writeln('');
128		For j:= 1 to n do
129			write(mat.upper[i,j]:2:1,'  ');
130	End;
131
132 End.
133
134
135
136
137