Nugroho's blog.: Manual Gauss Elimination on 3x3 Matrices in Delphi

Thursday, April 20, 2017

Manual Gauss Elimination on 3x3 Matrices in Delphi

I use this code in order to find its pattern.

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:

Unknown said...


//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;

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)