0% found this document useful (0 votes)
15 views

Program Gauss Const Nmax 10 Type

The document describes two programs - Program Cramer and Program Gauss. Program Cramer uses Cramer's rule to solve a system of linear equations by calculating the determinant of the coefficient matrix and transforming the matrix. Program Gauss uses Gaussian elimination to directly solve the system of linear equations by putting the coefficient matrix into reduced row echelon form and then back-substituting to find the solutions. Both programs read in a coefficient matrix and vector of constants, perform the respective algorithms to solve for the vector of unknowns, and output the solutions.

Uploaded by

Anastacia Cenusa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Program Gauss Const Nmax 10 Type

The document describes two programs - Program Cramer and Program Gauss. Program Cramer uses Cramer's rule to solve a system of linear equations by calculating the determinant of the coefficient matrix and transforming the matrix. Program Gauss uses Gaussian elimination to directly solve the system of linear equations by putting the coefficient matrix into reduced row echelon form and then back-substituting to find the solutions. Both programs read in a coefficient matrix and vector of constants, perform the respective algorithms to solve for the vector of unknowns, and output the solutions.

Uploaded by

Anastacia Cenusa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program Cramer;

type mat=array [1..4, 1..4] of real;


vec=array [1..4] of real;
vs=array [1..4] of real;
var a: mat;
b: vec;
sol: vs;
p,n:integer;
de:real;
Function cdet (x:mat; t:integer) :real;
var i,j,k,l : integer;
s: real;
minor:mat;
begin
if t=1 then cdet:=x[1,1]
else
begin s:=0;
for k:=1 to t do
begin
for i:=1 to t-1 do
for j:=1 to k-1 do minor[i,j]:=x[i+1,j];
for i:=1 to t-1 do
for j:=k to t-1 do minor [i, j]:=x[i+1, j+1];
if odd(k) then s:=s+x[1,k]*cdet(minor, t-1)
else s:=s-x[1,k]*cdet(minor, t-1);
end;
cdet:=s;
end;
end;
function transforma (x:mat; t,l:integer):real;
var i: integer;
begin for i:=1 to t do x[i, l]:=b[i];
transforma:=cdet (x,t);
end;
begin
n:=4;
a[1,1]:=8.7; a[1,2]:=1.1; a[1,3]:=-0.5; a[1,4]:=0.4; b[1]:=10.2;
a[2,1]:=1.1; a[2,2]:=9.6; a[2,3]:=1.2; a[2,4]:=0.4; b[2]:=10.2;
a[3,1]:=-0.5; a[3,2]:=1.2; a[3,3]:=14.1; a[3,4]:=1.3; b[3]:=10.2;
a[4,1]:=0.4; a[4,2]:=0.4; a[4,3]:=1.3; a[4,4]:=13.6; b[4]:=10.2;
de:=cdet(a,n);
if de<>0 then
begin for p:=1 to n do sol [p]:=transforma (a,n,p)/de;
for p:=1 to n do writeln('x[',p,']=',sol[p]:20:18);
end
else writeln('calculul imposibil');
readln;
end.

Program Gauss;
const nmax=10;
type

mat=array[1..nmax,1..nmax] of real;
vect=array[1..nmax] of real;
var
a:mat;
s:vect;
i,n:integer;
Procedure citeste (var x:mat; var t:integer);
var
i,j:integer;
begin
write ('dati marimea tabel= ');
readln (t);
for i:=1 to t do
begin
for j:=1 to t do read (x[i,j]);
write ('dati datele elementelor= ');
readln (x[i,t+1]);
write ('dati rezultatul= ');
readln (x[i,t+1]);
end;
end;
Procedure direct (var x:mat;t:integer);
label linie_urmatoare;
var
i,j,k,l:integer;
r:real;
begin
for i:=1 to t-1 do
begin
if x[i,i]=0 then
begin
k:=i;
for j:=i+1 to n do if x[j,i]<>0 then k:=j;
if k=i then goto linie_urmatoare
else
for j:=1 to t+1 do
begin
r:=x[i,j];
x[i,j]:=x[k,j];
x[k,j]:=r;
end;
end;
for j:=i+1 to t do
begin r:=-x[j,i]/x[i,i];
for k:=i to i+1 do x[j,k]:=x[j,k]+x[i,k]*r;
end;
linie_urmatoare: end;
end;
procedure invers (var q:vect);
var i,j:integer;
s:real;
begin
for i:=n downto 1 do
begin
s:=0;
for j:=i+1 to n do
s:=s+a[i,j]*q[j];
if a[i,i]<>then q[i]:=(a[i,n+1]-s)/a[i,i] else q[i]:=0;
end;
end;
begin citeste(a,n);
direct(a,n);
invers(s);
for i:=1 to n do
writeln('x[',i,']=',s[i]:0:3);
readln;
end.

You might also like