Given a square system of n linear equations:
where:
Then A can be decomposed into a diagonal component D, and the remainder R:
The element-based formula is thus:
Note that the computation of xi(k+1) requires each element in x(k) except itself. Unlike the Gauss–Seidel method, we can't overwrite xi(k) with xi(k+1), as that value will be needed by the rest of the computation. The minimum amount of storage is two vectors of size n.
So here is the code:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
const e = 0.000001;
var n,i,j,k,m,i1,j1,l:integer; a:array[1..5,1..5]of extended;
b:array[1..5,1..5]of extended; tk:array[1..5,1..5]of extended;
q,p,d,r,c,s,sign,ab,eb:real; t:array[1..5,1..5]of extended;
cm: array[1..5,1..5]of extended;
begin
writeln('Input n '); // Matrix size
read(n);
writeln('Input matrix A ');
// Matrix input
for i:=1 to n do
for j:=1 to n do
read(a[i,j]);
ab:=a[1,2];
eb:=1;
for i1:=1 to n do
for j1:=1 to n do
if i1<>j1 then
begin
if abs(a[i1,j1])>ab then
begin
i:=i1;
j:=j1
end;
end;
for i1:=1 to n do
for j1:=1 to n do
if i1=j1 then
begin
t[i1,i1]:=1;
tk[i1,i1]:=1;
end
else
t[i1,j1]:=0;
while eb>e do
begin //---1
p:=2*a[i,j];
q:=a[i,i]-a[j,j];
d:=sqrt(p*p+q*q);
sign:=p*q;
if sign<>0 then
sign:=sign/abs(sign)
else
sign:=0;
//---2
if (q<>0)and((abs(p))>=(abs(q))) then
begin
r:=abs(q)/(2*d);
c:=sqrt(0.5+r);
s:=(sqrt(0.5-r))*sign
end
else
if (q<>0)and ((abs(p))<(abs(q))) then
begin
r:=abs(q)/(2*d);
c:=sqrt(0.5+r);
s:=(abs(p))*sign/(2*c*d)
end
else
begin
c:=sqrt(2)/2;
s:=c
end;
// ----3
b[i,i]:=c*c*a[i,i]+s*s*a[j,j]+2*c*s*a[i,j];
b[j,j]:=s*s*a[i,i]+c*c*a[j,j]-2*c*s*a[i,j];
b[i,j]:=0; //---4
b[j,i]:=0;
//----5
for m:=1 to n do
if (m<>i)and(m<>j) then
begin
b[i,m]:=c*a[m,i]+s*a[m,j];
b[m,i]:=b[i,m];
b[j,m]:=-s*a[m,i]+c*a[m,j];
b[m,j]:=b[j,m];
end;
//--6
for m:=1 to n do
for l:=1 to n do
if (m<>i)and(m<>j)and(l<>i)and(l<>j) then
begin
b[m,l]:=a[m,l];
b[l,m]:=b[m,l]
end;
//---matrix T
for i1:=1 to n do
for j1:=1 to n do
if i1=j1 then
begin
tk[i1,i1]:=1;
cm[i1,i1]:=0;
end
else
begin
cm[i1,j1]:=0;
tk[i1,j1]:=0
end;
tk[i,i]:=c;
tk[j,j]:=c;
tk[j,i]:=-s;
tk[i,j]:=s;
for i1:=1 to n do
for j1:=1 to n do
for k:=1 to n do
cm[i1,j1]:=cm[i1,j1]+tk[i1,k]*t[k,j1];
for i1:=1 to n do
for j1:=1 to n do
begin
t[i1,j1]:=cm[i1,j1];
end ;
eb:=b[1,2];
for i1:=1 to n do
for j1:=1 to n do
if i1<>j1 then
begin
if abs(b[i1,j1])>eb then
begin
eb:=abs(b[i1,j1]);
i:=i1;
j:=j1;
end;
end;
eb:=abs(eb);
for i1:=1 to n do
for j1:=1 to n do
a[i1,j1]:=b[i1,j1];
end;
writeln('Vlasni Paru');
for i1:=1 to n do
begin
for j1:=1 to n do
write(b[i1,j1]:4:4,' ');
writeln;
end;
writeln;
writeln('Vectors');
for i1:=1 to n do
begin
for j1:=1 to n do
writeln('X',j1,'=', t[i1,j1]:2:4);
writeln('----------');
end;
readln;
readln;
end.
No comments:
Post a Comment