How to find the solutions to a set of linear equations?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
John D'Errico
le 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
le 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
Réponses (1)
Matt J
le 18 Oct 2020
Modifié(e) : Matt J
le 18 Oct 2020
You can easily trap the point where NaNs are introduced using
>>dbstop if naninf
9 commentaires
Matt J
le 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?
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!