3 views (last 30 days)

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

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

Matt J
on 19 Oct 2020

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/618163-how-to-find-the-solutions-to-a-set-of-linear-equations#comment_1072048

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/618163-how-to-find-the-solutions-to-a-set-of-linear-equations#comment_1072048

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/618163-how-to-find-the-solutions-to-a-set-of-linear-equations#comment_1072633

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/618163-how-to-find-the-solutions-to-a-set-of-linear-equations#comment_1072633

Sign in to comment.