# How to find the solutions to a set of linear equations?

3 views (last 30 days)
aw007 on 18 Oct 2020
Edited: Matt J on 20 Oct 2020
This question was flagged by John D'Errico
Matt J restored:
Hi All
I am currently trying to find the solutions to a set of linear equations by using Gauss elimination with partial pivoting in MATLAB
But when I run my script, I get the output of NaN. Why might this be so and how can I fix this issue?
Any form of help would be appreciated.
My code is shown below.
%Augmented matrix
Aug=[ 0 0 -2 0 0 0 0 0 0;
0 -0.8509 0 3 0 0 0 0 0;
0 0 0 0 0.8509 2 0 0 0;
1 0 0 0 -0.5253 0 0 0 0;
0 0.8509 0 0 0 0 -2 0.8509 0;
0 -0.8509 0 -4 0 0 0 0 12;
0 0 -1 0 0 3 0 -0.5253 0;
0 0 0 0 0 0 0 3 0];
[row,col]=size(Aug);%Obtain the size of A
%Conduct partial pivoting
[maxval,ind]=max(Aug((1:end),1)); %Find the max value and its index
if abs(maxval)>abs(Aug(1,1))
temp=Aug(ind,:);
Aug(ind,:)=Aug(1,:);
Aug(1,:)=temp;
end
%Find the upper triangular matrix
for p=1:row-1
for i=p+1:row
l(i,p)=Aug(i,p)/Aug(p,p);
Aug(i,:)= Aug(i,:)-Aug(p,:)* l(i,p);
end
end
%Acquire coefficient matrix and column vector
for i=1:row
for p=1:col-1
coeff(i,p)=Aug(i,p);
colvec(i)=Aug(i,col);
end
end
colvec=colvec'; %transpose
%Back substitution to obtain results
sol(row)=colvec(row)/coeff(row,row);
tot=0;
for i=row-1:-1:1
for p=i+1:row
tot=tot+coeff(i,p)*sol(p);
sol(i)=(colvec(i)-tot)/coeff(i,i)
end
tot=0;
end

John D'Errico on 19 Oct 2020
Just becauae you personally are not interested in the question anylonger, does not mean that others would not benefit from the answer. Effectively, you insult the person who spent the time answering your question,.
Rik on 20 Oct 2020
How to find the solutions to a set of linear equations?
Hi All
I am currently trying to find the solutions to a set of linear equations by using Gauss elimination with partial pivoting in MATLAB
But when I run my script, I get the output of NaN. Why might this be so and how can I fix this issue?
Any form of help would be appreciated.
My code is shown below.
%Augmented matrix
Aug=[ 0 0 -2 0 0 0 0 0 0;
0 -0.8509 0 3 0 0 0 0 0;
0 0 0 0 0.8509 2 0 0 0;
1 0 0 0 -0.5253 0 0 0 0;
0 0.8509 0 0 0 0 -2 0.8509 0;
0 -0.8509 0 -4 0 0 0 0 12;
0 0 -1 0 0 3 0 -0.5253 0;
0 0 0 0 0 0 0 3 0];
[row,col]=size(Aug);%Obtain the size of A
%Conduct partial pivoting
[maxval,ind]=max(Aug((1:end),1)); %Find the max value and its index
if abs(maxval)>abs(Aug(1,1))
temp=Aug(ind,:);
Aug(ind,:)=Aug(1,:);
Aug(1,:)=temp;
end
%Find the upper triangular matrix
for p=1:row-1
for i=p+1:row
l(i,p)=Aug(i,p)/Aug(p,p);
Aug(i,:)= Aug(i,:)-Aug(p,:)* l(i,p);
end
end
%Acquire coefficient matrix and column vector
for i=1:row
for p=1:col-1
coeff(i,p)=Aug(i,p);
colvec(i)=Aug(i,col);
end
end
colvec=colvec'; %transpose
%Back substitution to obtain results
sol(row)=colvec(row)/coeff(row,row);
tot=0;
for i=row-1:-1:1
for p=i+1:row
tot=tot+coeff(i,p)*sol(p);
sol(i)=(colvec(i)-tot)/coeff(i,i)
end
tot=0;
end

Matt J on 18 Oct 2020
Edited: Matt J on 18 Oct 2020
You can easily trap the point where NaNs are introduced using
>>dbstop if naninf

aw007 on 19 Oct 2020
I intended to calculate the upper triangular matrix and I have been using this same portion of code to do so. I don't understand why it doesn't work with my current matrix. Are you familar with Gauss elimination with partial pivoting?
Matt J on 19 Oct 2020
I am only vaguely familiar with partial pivoting, but even if A were made upper triangular, I don't see how that would avoid the possibility that one of the elements A(p,p) would be equal to zero. What's supposed to happen if the whole matrix is zero to begin with?
aw007 on 19 Oct 2020
Thats fine. If not sure what would happen if the whole matrix is zero to begin with, as then the solutions would just be zero.

R2020a

### Community Treasure Hunt

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

Start Hunting!