Title: How to code a linear system soluion
Question: How to code a linear system soluion (AX=B) use dynamic array
Answer:
AX=B
where A is a symetric matrix sizes n x n
B is a load case matrix sizes n x m
X is a solution of AX=B
Step1:use procedure REDUCE to reduce any symetric matrix to an upper triangular matrix.
Step2:use LOWTRI to produce a lower matrix from step1*no need to recalculate from prereduce matrix*
Step3:use FORSUB to forward substitution
Step4:use BAKSUB to backsubstition
Fianlly u get the answers
procedure TForm2.REDUCE(var Mat:Matrix;nJR:integer);
var i,j,k : integer;
C : double;
nHigh : integer;
begin
nHigh:=high(Mat);
for i:=0 to nJR-1 do
for j:=i to nHigh-1 do
begin
C:=-(Mat[j+1,i]/Mat[i,i]);
for k:=i to nHigh do
Mat[j+1,k]:=Mat[j+1,k]+C*Mat[i,k];
end;
end;
procedure TForm2.LOWTRI(var Mat:Matrix);
var i,j : integer;
nHigh,nLow : integer;
begin
nHigh:=high(Mat);
nLow:=Low(Mat);
for j:=nLow to nHigh do
for i:=j+1 to nHigh do
Mat[i,j]:=Mat[j,i]/Mat[j,j];
end;
Function TForm2.FORSUB(L:Matrix;B:Matrix):Matrix;
var i,j,k:integer;
Y:Matrix;
nHigh : integer;
C:double;
begin
setlength(Y,high(B)+1,m);
nHigh:=high(L);
For i:=Low(Y) to m-1 do begin
Y[0,i]:=B[0,i];
for k:=1 to nHigh do begin
C:=0;
For j:=0 to k-1 do C:=L[k,j]*Y[j,i]+C;
Y[k,i]:=B[K,i]-C;
end;
end;
Result:=Y;
end;
Function TForm2.BAKSUB(U:Matrix;Y:Matrix):Matrix;
var i,j,k:integer;
X:Matrix;
nHigh : integer;
C:double;
begin
setlength(X,high(Y)+1,m);
nHigh:=high(U);
For i:=low(X) to m-1 do begin
X[nHigh,i]:=Y[nHigh,i]/U[nHigh,nHigh];
For k:=nHigh-1 downto 0 do begin
C:=0;
For j:=k+1 to nHigh do C:=U[k,j]*X[j,i]+C;
X[k,i]:=(Y[k,i]-C)/U[k,k];
end;
end;
Result:=X;
end;