Listing program eliminasi Gauss tanpa memperhatikan elemen pivot
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit1: TEdit;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
Button1: TButton;
Button2: TButton;
Button3: TButton;
StringGrid3: TStringGrid;
BitBtn1: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ordo:integer;
A:array[1..100,1..100]of real;
B,X:array[1..100]of real;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k:integer;
m,sigma:real;
begin
groupbox3.Visible:=true;
for i:=1 to ordo do
for j:=1 to ordo do
A[i,j]:=strtofloat(stringgrid1.Cells[j,i]);
for i:=1 to ordo do
B[i]:=strtofloat(stringgrid2.Cells[1,i]);
for k:=1 to ordo-1 do
for i:=k+1 to ordo do
begin
m:=A[i,k]/A[k,k];
for j:=k to ordo do
A[i,j]:=a[i,j]-m*A[k,j];
B[i]:=b[i]-m*B[k];
end;
X[ordo]:=B[ordo]/A[ordo,ordo];
for k:=ordo-1 downto 1 do
begin
sigma:=0;
for j:=k+1 to ordo do
sigma:=sigma+A[k,j]*X[j];
X[k]:=(b[k]-sigma)/A[k,k];
end;
stringgrid3.RowCount:=2;
stringgrid3.ColCount:=ordo+1;
stringgrid3.Cells[0,0]:='i';
stringgrid3.Cells[0,1]:='x(i)';
for i:=1 to ordo do
begin
stringgrid3.Cells[i,0]:=inttostr(i);
stringgrid3.Cells[i,1]:=floattostr(x[i]);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
groupbox2.Visible:=true;
stringgrid1.Visible:=true;
stringgrid2.Visible:=true;
ordo:=strtoint(edit1.Text);
stringgrid1.ColCount:=ordo+1;
stringgrid1.RowCount:=ordo+1;
stringgrid1.Cells[0,0]:='i/j';
for i:=1 to ordo do
begin
stringgrid1.Cells[0,i]:=inttostr(i);
stringgrid1.Cells[i,0]:=inttostr(i);
end;
stringgrid2.ColCount:=2;
stringgrid2.Cells[0,0]:='i';
stringgrid2.RowCount:=ordo+1;
stringgrid2.Cells[1,0]:='b[i]';
for i:=1 to ordo do
stringgrid2.Cells[0,i]:=inttostr(i);
end;
procedure TForm1.Button3Click(Sender: TObject);
var i:integer;
begin
for i:=1 to ordo do
stringgrid1.Rows[i].Clear;
stringgrid2.Cols[1].Clear;
stringgrid2.Rows[1].Clear;
groupbox2.Visible:=false;
groupbox3.Visible:=false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
groupbox2.Visible:=false;
groupbox3.Visible:=false;
end;
end.
Listing program eliminasi Gauss degan memperhatikan elemen pivot
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit1: TEdit;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
Button1: TButton;
Button2: TButton;
Button3: TButton;
StringGrid3: TStringGrid;
BitBtn1: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ordo:integer;
A:array[1..100,1..100]of real;
B,X:array[1..100]of real;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k,t,s,r:integer;
m,sigma,pivot,tampung:real;
singular:boolean;
begin
groupbox3.Visible:=true;
for i:=1 to ordo do
for j:=1 to ordo do
A[i,j]:=strtofloat(stringgrid1.Cells[j,i]);
for i:=1 to ordo do
B[i]:=strtofloat(stringgrid2.Cells[1,i]);
k:=1;
singular:=false;
while(k<=ordo-1)and(not singular) do
begin
pivot:=A[k,k];
r:=k;
for t:=k+1 to ordo do
if Abs(A[t,k])>Abs(pivot) then
begin
pivot:=A[t,k];
r:=t;
end;
if pivot=0 then
singular:=true
else
if r>k then
begin
for s:=1 to ordo do
begin
tampung:=A[k,s];
A[k,s]:=A[r,s];
A[r,s]:=tampung;
end;
tampung:=B[k];
B[k]:=B[r];
B[r]:=tampung;
end;
for i:=k+1 to ordo do
begin
m:=A[i,k]/A[k,k];
for j:=k to ordo do
A[i,j]:=a[i,j]-m*A[k,j];
B[i]:=b[i]-m*B[k];
end;
k:=k+1;
end;
X[ordo]:=B[ordo]/A[ordo,ordo];
for k:=ordo-1 downto 1 do
begin
sigma:=0;
for j:=k+1 to ordo do
sigma:=sigma+A[k,j]*X[j];
X[k]:=(b[k]-sigma)/A[k,k];
end;
stringgrid3.RowCount:=2;
stringgrid3.ColCount:=ordo+1;
stringgrid3.Cells[0,0]:='i';
stringgrid3.Cells[0,1]:='x(i)';
for i:=1 to ordo do
begin
stringgrid3.Cells[i,0]:=inttostr(i);
stringgrid3.Cells[i,1]:=floattostr(x[i]);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
groupbox2.Visible:=true;
stringgrid1.Visible:=true;
stringgrid2.Visible:=true;
ordo:=strtoint(edit1.Text);
stringgrid1.ColCount:=ordo+1;
stringgrid1.RowCount:=ordo+1;
stringgrid1.Cells[0,0]:='i/j';
for i:=1 to ordo do
begin
stringgrid1.Cells[0,i]:=inttostr(i);
stringgrid1.Cells[i,0]:=inttostr(i);
end;
stringgrid2.ColCount:=2;
stringgrid2.Cells[0,0]:='i';
stringgrid2.RowCount:=ordo+1;
stringgrid2.Cells[1,0]:='b[i]';
for i:=1 to ordo do
stringgrid2.Cells[0,i]:=inttostr(i);
end;
procedure TForm1.Button3Click(Sender: TObject);
var i:integer;
begin
for i:=1 to ordo do
stringgrid1.Rows[i].Clear;
stringgrid2.Cols[1].Clear;
stringgrid2.Rows[1].Clear;
groupbox2.Visible:=false;
groupbox3.Visible:=false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
groupbox2.Visible:=false;
groupbox3.Visible:=false;
end;
end.
No comments:
Post a Comment
Kalau berkenan, dikoment donk untuk menjadi masukan buad Admin