How to find the solutions to a set of linear equations?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
John D'Errico
am 19 Okt. 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
am 20 Okt. 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
Antworten (1)
Matt J
am 18 Okt. 2020
Bearbeitet: Matt J
am 18 Okt. 2020
You can easily trap the point where NaNs are introduced using
>>dbstop if naninf
9 Kommentare
Matt J
am 19 Okt. 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?
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!