[心得] 高斯-喬登消去法
最近剛好碰到這方面的作業,內容是用 while 和 for 寫的
不過沒有偵錯功能,還請大家批評指教
function a=GJ_elimi(b)
% Gauss-Jordan elimination
% b contains coefficients of a, b, ... and constant C for each column
coeffi=b(:,1:end-1);
L=length(coeffi);
const=b(:,end);
% upper matrix form
S=1;
while S<=L-1
for i=S+1:L
k=coeffi(i,S)/coeffi(S,S);
coeffi(i,:)=coeffi(i,:)-coeffi(S,:)*k;
const(i)=const(i)-const(S)*k;
end
S=S+1;
end
% reduced row echelon form
S=L;
while S>=2
for i=S-1:-1:1
k=coeffi(i,S)/coeffi(S,S);
coeffi(i,:)=coeffi(i,:)-coeffi(S,:)*k;
const(i)=const(i)-const(S)*k;
end
S=S-1;
end
% make diagonal terms '1'
N=diag(coeffi);
coeffi=coeffi./repmat(N,1,L);
const=const./N;
a=[coeffi const];
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.63.249
※ 編輯: ht15trep 來自: 140.112.63.249 (06/06 19:00)