Yes, there is many Gauss code out there. I plan to write it on next post about it. The dynamic Gauss Elimination code that could be implemented to any size of matrices.
But for now, let just settle on this.
https://youtu.be/csiFpdsrzzQ
The main code is in this procedure
procedure tform1.gauss; var temp:real; begin //normalize diagonal on first row temp:=b[1,1]; b[1,1]:=b[1,1]/temp; b[1,2]:=b[1,2]/temp; b[1,3]:=b[1,3]/temp; b[1,4]:=b[1,4]/temp; //zeroing b[2,1] temp:=b[2,1]; b[2,1]:=b[2,1]-b[1,1]*temp; b[2,2]:=b[2,2]-b[1,2]*temp; b[2,3]:=b[2,3]-b[1,3]*temp; b[2,4]:=b[2,4]-b[1,4]*temp; //normalize diagonal on second row temp:=b[2,2]; b[2,2]:=b[2,2]/temp; b[2,3]:=b[2,3]/temp; b[2,4]:=b[2,4]/temp; //zeroing b[3,1] temp:=b[3,1]; b[3,1]:=b[3,1]-b[1,1]*temp; b[3,2]:=b[3,2]-b[1,2]*temp; b[3,3]:=b[3,3]-b[1,3]*temp; b[3,4]:=b[3,4]-b[1,4]*temp; //zeroing b[3,2] temp:=b[3,2]; b[3,2]:=b[3,2]-b[1,2]*temp; b[3,3]:=b[3,3]-b[1,3]*temp; b[3,4]:=b[3,4]-b[1,4]*temp; //normalize diagonal on third row temp:=b[3,3]; b[3,3]:=b[3,3]/temp; b[3,4]:=b[3,4]/temp; //back subtitution x[3]:=b[3,4]; x[2]:=b[2,4]-b[2,3]*x[3]; x[1]:=b[1,4]-b[1,3]*x[3]-b[1,2]*x[2]; end;
At first glance, we all knew that this must be simple loop. Yeah, it is, just normal loop, thanks God, no recursive needed, :)
My full code's like this;
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids; type TForm1 = class(TForm) StringGrid1: TStringGrid; StringGrid2: TStringGrid; Button1: TButton; StringGrid3: TStringGrid; procedure nilaiAwal; procedure gauss; procedure bacaMatrik; procedure tulisMatrik(e:integer); procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; const n=3; var Form1: TForm1; a,b : array[1..n,1..n+1]of real; x : array[1..n]of real; implementation {$R *.dfm} procedure tform1.nilaiAwal; var i,j:integer; begin randomize; //isi matrik for i:=1 to n do begin for j:=1 to n do begin if i=j then a[i,j]:=1+random(9) else a[i,j]:=0; end; a[i,4]:=1+random(9); end; end; procedure tform1.bacaMatrik; var i,j:integer; begin for i:=1 to n do begin for j:=1 to n+1 do begin a[i,j]:=strToFloat(stringgrid1.Cells[j-1,i-1]); b[i,j]:=a[i,j]; end; end; end; procedure tform1.tulisMatrik(e:integer); var i,j:integer; begin for i:=1 to n do begin for j:=1 to n+1 do begin case e of 1:stringgrid1.Cells[j-1,i-1]:=floatToStr(a[i,j]); 2:stringgrid2.Cells[j-1,i-1]:=floatToStr(b[i,j]); end; end; stringgrid3.Cells[0,i-1]:=floatToStr(x[i]); end; end; procedure tform1.gauss; var temp:real; begin //normalize diagonal on first row temp:=b[1,1]; b[1,1]:=b[1,1]/temp; b[1,2]:=b[1,2]/temp; b[1,3]:=b[1,3]/temp; b[1,4]:=b[1,4]/temp; //zeroing b[2,1] temp:=b[2,1]; b[2,1]:=b[2,1]-b[1,1]*temp; b[2,2]:=b[2,2]-b[1,2]*temp; b[2,3]:=b[2,3]-b[1,3]*temp; b[2,4]:=b[2,4]-b[1,4]*temp; //normalize diagonal on second row temp:=b[2,2]; b[2,2]:=b[2,2]/temp; b[2,3]:=b[2,3]/temp; b[2,4]:=b[2,4]/temp; //zeroing b[3,1] temp:=b[3,1]; b[3,1]:=b[3,1]-b[1,1]*temp; b[3,2]:=b[3,2]-b[1,2]*temp; b[3,3]:=b[3,3]-b[1,3]*temp; b[3,4]:=b[3,4]-b[1,4]*temp; //zeroing b[3,2] temp:=b[3,2]; b[3,2]:=b[3,2]-b[1,2]*temp; b[3,3]:=b[3,3]-b[1,3]*temp; b[3,4]:=b[3,4]-b[1,4]*temp; //normalize diagonal on third row //normalize diagonal on second row temp:=b[3,3]; b[3,3]:=b[3,3]/temp; b[3,4]:=b[3,4]/temp; //back subtitution x[3]:=b[3,4]; x[2]:=b[2,4]-b[2,3]*x[3]; x[1]:=b[1,4]-b[1,3]*x[3]-b[1,2]*x[2]; end; procedure TForm1.FormCreate(Sender: TObject); begin nilaiAwal; tulisMatrik(1); end; procedure TForm1.Button1Click(Sender: TObject); begin bacaMatrik; gauss; tulisMatrik(2); end; end.
1 comment:
//zeroing b[3,2]
temp:=b[3,2];
b[3,2]:=b[3,2]-b[1,2]*temp; => b[3,2]:=b[3,2]-b[2,2]*temp;
b[3,3]:=b[3,3]-b[1,3]*temp; => b[3,3]:=b[3,3]-b[2,3]*temp;
b[3,4]:=b[3,4]-b[1,4]*temp; => b[3,4]:=b[3,4]-b[2,4]*temp;
Post a Comment