日期:11-14
作者:老李
按列选主元策略的高斯消去法
目标
编写函数,实现列选主元策略的高斯消去法
过程
代码如下:
function [ X, U ] = colGaussElim( A, b ) %colGaussElim :Gauss elimination method of column selection principal element % Input - :A is the coefficient matrix(n*n) % - :b is the value of equations(n*1) % Output - :X is the solutions of equaions(n*1) % - :U is the coefficient matrix after elimination M = [A b];%增广矩阵 [n, ~] = size(A); X = zeros(n,1); %消去 for i = 1:n-1 %换主元 N = abs(M); [~, e] = max(N((i:n),i));%找出该列中主元绝对值最大的元素所在行的索引 e=e+i-1; temp = M(i,:); M(i,:) = M(e,:); M(e,:) = temp;%替换结束 if M(i,i) == 0 error('奇异阵'); end %消去 for j = i+1:n %计算得到乘子:m m = M(j,i)/M(i,i); M(j,:) = M(j,:)-m*M(i,:); end end U = M(:,(1:n)); %回代过程 bn = M(:,n+1); X(n) = bn(n)/M(n,n); for k = 1:n-1 X(n-k) = (bn(n-k)-M(n-k,(n-k+1:n))*X(n-k+1:n))/(n-k); end end
效果
输入变量
A = [4 5 6 2;0 4 7 3; 6 8 1 1; 3 2 5 1] b = [4;8;6;1]
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214834.html原文链接:https://javaforall.net
