怎样算高斯烟团模型的算法,编程的代码
解放军生活杂志
2024-11-17 17:41:34
最佳回答
% gauss.mfunction x = gauss(a,b)%the sizes of matrices a,b are supposed to be na x na and na x nb.%th** function solves ax = b by gauss elimination algorithm.na = size(a,2); [nb1,nb] = size(b);if nb1 ~= na, error('a and b must h**e compatible dimensions'); endn = na + nb; ab = [a(1:na,1:na) b(1:na,1:nb)]; % augmented matrixepss = eps*ones(na,1);for k = 1:na %scaled partial pivoting at ab(k,k) by eq.(2.2.20) [akx,kx] = max(abs(ab(k:na,k))./max(abs([ab(k:na,k + 1:na) epss(1:na - k + 1)]'))'); if akx < eps, error('singular matrix and no unique solution'); end mx = k + kx - 1; if kx > 1 % row change if necessary tmp_row = ab(k,k:n); ab(k,k:n) = ab(mx,k:n); ab(mx,k:n) = tmp_row; end % gauss forward elimination ab(k,k + 1:n) = ab(k,k+1:n)/ab(k,k); ab(k,k) = 1; %make each diagonal element one for m = k + 1: na ab(m,k+1:n) = ab(m,k+1:n) - ab(m,k)*ab(k,k+1:n); %eq.(2.2.5) ab(m,k) = 0; endend%backward substitution for a upper-triangular matrix eqation% h**ing all the diagonal elements equal to onex(na,:) = ab(na,na+1:n);for m = na-1: -1:1 x(m,:) = ab(m,na + 1:n)-ab(m,m + 1:na)*x(m + 1:na,:); %eq.(2.2.7)end例:>> a = [0 1 1;2 -1 -1;1 1 -1]; b = [2 0 1]';>> x = gauss(a,b)x = 1.00000000000000 1.00000000000000 1.00000000000000
baiduyixiajiukeyia 20210311