Tóm tắt
Báo cáo trình bày Chương trình giải gần đúng hệ phương
trình tuyến tính tổng quát trên máy điện toán, trong đó có kết hợp việc tính hạng
của ma trận và tính định thức.
Thuật toán được sử dụng là phương pháp Gao-xơ mở rộng.
Sai số tính toán được xác định theo cấp độ chính xác của
hệ toán máy.
Chương
trình
{Giai he pttt tong quat, tinh hạng hay đinh thuc
của ma tran}
Program
Giai_hpttttq_hang_mt_va_dinh_thuc;
uses
crt;
const
p=20; w=0.0000000001;
type
sothuc=real;
var
viec: byte;
h,i,j,k,l,m,n,r,s,t,u,v: byte;
a,b,c,d: array[1..p,1..p] of sothuc;
x,y,z: array[1..p] of real; f,o,q: array[1..p] of
byte;
e,g,max: real; label nha; {khai bao nhan}
Proccedure
thutuc1;
Begin
{Vao cac he so cua he pttt dang Ax=b}
If viec=1 then
Begin
write(‘Nhap so Phuong trinh m=’, m:5); readln(m);
write(‘Nhap so an cua he pt n=’, n:5); readln(n);
for i:=1 to m do
for j:=1 to n
do
begin
write(‘Nhap
vao cac he so a[’,i,j, ‘]=’, a[i,j]:5:2); readln(a[i,j]);
b[i,j]:=a[i,j];
end;
readln;
{Nhap vao
mang 1 chieu, 1,2,…,n, tuong ung voi so an cua he pt}
For j:=1 to n
do
begin
f[j]:=j;
q[j]:=f[j];
end;
{Viet ra cac an va cac he so cua he pttt}
write(‘Cac
an va ma tran cac he so cua he Phuong trinh:’); writeln;
for u:=1 to n do write(‘x[’,u, ‘]=’,
x[s]:7:2); writeln;
for u:=1 to m do
begin
for v:=1 to n+1
do write(a[u,v]:7:2);
writeln;
end;
readln;
End;
If
viec=2 then
Begin
{Vao cac he so cua cua ma tran A}
write(‘Nhap so dong cua ma tran m=’, m:5); readln(m);
write(‘Nhap so cot cua he ma tran n=’, n:5);
readln(n);
for i:=1 to m do
for j:=1 to n
do
begin
write(‘Nhap
vao cac he so a[’,i,j, ‘]=’, a[i,j]:7:2); readln(a[i,j]);
b[i,j]:=a[i,j];
end;
{Them cot n+1 bang 0 vao A de lien ket voi viec 1 giai he phuong trinh}
for i:=1 to m do a[i,n+1]:=0; b[i,n+1]:= a[i,n+1];
readln;
{Viet ra cac phan tu cua ma tran A}
write(‘Ma tran da cho :’); writeln;
for u:=1 to m do
begin
for v:=1 to n do write(a[u,v]:7:2);
writeln;
end;
readln;
End;
If
viec=3 then
Begin
{Vao cac
he so cua cua ma tran A de tinh dinh thuc}
write(‘Nhap vao cap cua dinh thuc n=’, n:5);
readln(n); m:=n;
for i:=1 to m do
for j:=1 to n do
begin
write(‘Nhap vao cac he so a[’,i,j, ‘]=’, a[i,j]:7:2);
readln(a[i,j]);
b[i,j]:=a[i,j];
end;
{Them cot n+1 bang 0 vao A de lien ket
voi viec 1 giai he phuong trinh}
for i:=1 to m do a[i,n+1]:=0; b[i,n+1]:= a[i,n+1];
readln;
{Viet ra cac phan tu cua ma tran A cua
dinh thuc}
write(‘Dinh thuc da cho :’);
writeln;
for u:=1 to m do
begin
for v:=1 to n do write(a[u,v]:7:2);
writeln;
end;
readln;
End;
{Cheo hoa ma tran B}
g:=1;
{De tinh dinh thuc}
k:=n;
if m<n then k:=m;
for
r:=1 to k do
Begin
max:=abs(b[r,r]); s:=r; t:=r; d[r,r]:=b[s,t];
if max<>1 then
begin
for i:=r to m do
for j:=r to n do
begin
if
abs(b[i,j])=1 then
begin
if
abs(b[i,j])<>max then
begin
max:=
abs(b[i,j]); s:=i; t:=j; d[r,r]:=b[s,t];
end;
end;
end;
if max<>1 then
begin
for i:=r to m do
for j:=r to n do
begin
if abs(b[i,j])>max then
begin
if
abs(b[i,j])<>max then
begin
max:= abs(b[i,j]); s:=i; t:=j;
d[r,r]:=b[s,t];
end;
end;
end;
end;
end;
if max>0 then
begin
if t>r then
begin
z[r]:=y[r]; y[r]:=y[t]; y[t]:=z[r]; o[r]:=q[r];
q[r]:=q[t]; q[t]:=o[r]; g:=-1*g;
for i:=1 to m do
begin
c[i,r]:=b[i,r];b[i,r]:=b[i,t];b[i,t]:=c[i,r];
end;
end;
if s>r then
begin
g:=-1*g;
for j:=r to n+1 do
begin
c[r,j]:=b[r,j];
b[r,j]:=b[s,j]; b[s,j]:=c[r,j];
end;
end;
for j:=r to n+1 do b[r,j]:=b[r,j]/d[r,r];
if r<m then
begin
for i:=r+1 to m do
begin
c[i,r]:=b[i,r];
for j:=r to n+1 do
begin
b[i,j]:=b[i,j]-c[i,r]*b[r,j];
end;
end;
end;
if r>1 then
begin
for i:=r-1 downto 1 do
begin
c[i,r]:=b[i,r];
for j:=r to n+1 do
begin
b[i,j]:=b[i,j]-c[i,r]*b[r,j];
end;
end;
end;
end;
End;
{Viet ra cac an so cung ma tran cheo hoa}
write(‘Ma tran cheo hoa cua
ma tran A bo sung:’); writeln;
for u:=1 to m do
begin
for v:=1 to n+1 do write(b[u,v]:7:2);
writeln;
end;
readln;
{Tinh hang hay dinh thuc cua ma tran}
h:=0;
e:=0;
for
i:=1 to k do
begin
h:=h+trunc(b[i,i]);
g:=g*d[i,i];
end;
if
h<m then
begin
for
i:=h+1 to m do e:=e+abs(b[i,n+1]);
end;
if
viec = 2 then write(‘Hang cua ma tran A la
’, h:7);
if
viec = 3 then
begin
if h=n then write(‘Det(A)=’,g:10:10) else
write (‘Det(A)=0’);
end;
readln;
{Giai he phuong trinh tuyen tinh tong
quat va viet ra ket qua}
if
viec = 1 then
Begin
for s:=1 to n do write(‘y[’,s, ‘]=’, y[s]:7:2);
readln;
{for
s:=1 to n do write(‘q[’,s, ‘]=’, q[s]:7:2);
Readln;}
for i:=1 to n do
begin
for j:=1 to n do
begin
if j = q[i] then write(‘x[’,j, ‘]=y[’,i, ‘],’ );
end;
end;
readln;
{write(‘e=’, e:7:2); wreadln;}
if
e>0 then write(‘He phuong trinh vo nghiem.’); readln;
if
e=0 then
begin
if h=n
then
begin
write(‘He
phuong trinh co nghiem duy nhat.’); readln;
for
j:=1 to h do y[j]:=b[j,n+1];
for
i:=1 to n do
for j:=1 to n do
begin
if j = q[i] then write(‘x[’,j, ‘]=’,y[i]:7:7 );
readln;
end;
end;
end;
if h<n then
begin
write(‘He phuong trinh co vo so nghiem.’);
for i:=h+1 to n do
for
j:=1 to n do
begin
if j = q[i]
then write(‘Cac an nhan gia tri tuy y la x[’,j, ‘],’, y[i] ); readln;
end;
if
h=1 then
begin
for
j:=h+1 to n do y[h]:=b[h,n+1]-b[h,j]*y[j];
end;
if
h>1 then
begin
for
j:=h downto 1 do
begin
y[j]:=b[j,n+1]; z[j]:=y[j];
for u:=1 to n-j do
y[j]:=b[j,j+u]*z[j+u];
end;
end;
for i:=1 to h do
for j:=1 to n do
begin
if j
= q[i] then
Write(‘An tuy thuoc vao gia tri cua ’,n-h,‘
an tu do khac, khi cac an nay
bang 0, la: x[’,j, ‘]=’,y[i]:7:5);
readln;
end;
end;
end;
End;
Begin
clrscr;
write(‘viec1_giai hpttt,
viec2_tinhhmt_viec3_dinhthuc. ’,viec,); readln;
thutuc1;
End.