How can I reduce the time for this Gaussian Elimination Code

5 views (last 30 days)
I am relatively new to MATlab and have been given a code, however I think the time that the code runs could be reduced by a significant amount. When I ran the profile command on this code just about 80% of the time used comes from one line of code. The code is a little bit on the long side, but the line that I am talking about is %Step 6
TRUE = 1;
FALSE = 0;
fprintf(1,'This is Gaussian Elimination to solve a linear system.\n');
fprintf(1,'The array shall be written in the code in advance.\n');
fprintf(1,'Has the array been written? - enter Y or N.\n');
AA = input(' ','s');
OK = FALSE;
if AA == 'Y' || AA == 'y'
while OK == FALSE
% Matrix dimension N
M = 146;
N = 4*M;
A = zeros(N,N+1);
X = zeros(1,N);
for I = 1:N
for J = 1:N
if I==J,
A(I,J) = 2;
end;
if J==I+1,
A(I,J)=-1;
end;
if J==I-1,
A(I,J)=-1;
end;
end;
end;
% right-hand-side
A(1,N+1)=1;
for I = 2:N-1
A(I,N+1)=0;
end;
A(N,N+1)=1;
OK = TRUE;
end;
else
fprintf(1,'The program will end so the matrix can be written.\n');
end;
if OK == TRUE
fprintf(1,'The code is executing now.\n');
t = cputime; %Starting time
% STEP 1
% Elimination Process
NN = N-1;
M = N+1;
ICHG = 0;
I = 1;
while OK == TRUE && I <= NN
% STEP 2
% use IP instead of p
IP = I;
while abs(A(IP,I)) <= 1.0e-20&& IP <= N
IP = IP+1;
end;
if IP == M
OK = FALSE;
else
% STEP 3
if IP ~= I
for JJ = 1:M
C = A(I,JJ);
A(I,JJ) = A(IP,JJ);
A(IP,JJ) = C;
end;
ICHG = ICHG+1;
end;
% STEP 4
JJ = I+1;
for J = JJ:N
% STEP 5
% use XM in place of m(J,I)
XM = A(J,I)/A(I,I);
% STEP 6
for K = JJ:M
A(J,K) = A(J,K) - XM * A(I,K);
end;
% Multiplier XM could be saved in A(J,I).
A(J,I) = 0;
end;
end;
I = I+1;
end;
if OK == TRUE
% STEP 7
if abs(A(N,N)) <= 1.0e-20
OK = FALSE;
else
% STEP 8
% start backward substitution
X(N) = A(N,M) / A(N,N);
% STEP 9
for K = 1:NN
I = NN-K+1;
JJ = I+1;
SUM = 0;
for KK = JJ:N
SUM = SUM - A(I,KK) * X(KK);
end;
X(I) = (A(I,M)+SUM) / A(I,I);
end;
eltime = cputime-t; %total elapse time
% STEP 10
% procedure completed successfully
fprintf(1,'Choice of output method:\n');
fprintf(1,'1. Output to screen\n');
fprintf(1,'2. Output to text file\n');
fprintf(1,'Please enter 1 or 2.\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\name.ext\n');
fprintf(1,'for example: A:\\OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP, 'GAUSSIAN ELIMINATION\n\n');
fprintf(OUP, 'The reduced system - output by rows:\n');
% for I = 1:N
% for J = 1:M
% fprintf(OUP, ' %11.8f', A(I,J));
% end;
% fprintf(OUP, '\n');
% end;
fprintf(OUP, '\n\nHas solution vector:\n');
for I = 1:N
fprintf(OUP, ' %12.8f\n', X(I));
end;
fprintf (OUP, '\n\nwith %d row interchange(s)\n', ICHG);
fprintf(OUP, 'cpu time \n');
fprintf(OUP, '%12.8f\n',eltime);
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully \n',NAME);
end;
end;
end;
if OK == FALSE
fprintf(1,'System has no unique solution\n');
end;
end;
Thank you for any help!

Answers (1)

Torsten
Torsten on 29 Mar 2016
n=4*146;
S=sparse(gallery('tridiag',n,-1,2,-1));
b=sparse(1;zeros(n-2,1);1);
x=S\b;
should do the same as your program.
Best wishes
Torsten.
  3 Comments
Connor Oliver
Connor Oliver on 29 Mar 2016
Ok, I ended up getting it to work with a little more knowledge on sparse command than I had previously, so thank you. This is the code I ended up using, that does give the right values.
n=4*146;
S=sparse(gallery('tridiag',n,-1,2,-1));
i=[1,n];
j=[1,1];
s=[1,1];
b=sparse(i,j,s,n,1);
x=S\b;
Torsten
Torsten on 30 Mar 2016
And does it execute faster than the code you've been given ?
Best wishes
Torsten.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!