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

6 ビュー (過去 30 日間)
JoBrr
JoBrr 2020 年 10 月 18 日
編集済み: Matt J 2020 年 10 月 20 日
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
  2 件のコメント
John D'Errico
John D'Errico 2020 年 10 月 19 日
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
Rik 2020 年 10 月 20 日
Question originally posted by aw007 retrieved from Google cache:
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

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2020 年 10 月 18 日
編集済み: Matt J 2020 年 10 月 18 日
You can easily trap the point where NaNs are introduced using
>>dbstop if naninf
  9 件のコメント
Matt J
Matt J 2020 年 10 月 19 日
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?
JoBrr
JoBrr 2020 年 10 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by