高斯列主元消元法的matlab程序

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/15 02:05:54
高斯列主元消元法的matlab程序
xTn@ˢjܺeV2슄Aq{ltJ"(P$H$*@IIn {Ͻ̙4Q~=!"

高斯列主元消元法的matlab程序
高斯列主元消元法的matlab程序

高斯列主元消元法的matlab程序
两年前写的代码 comment 是给的,里面代码是我自己按照comment写的,希望能有帮助.
clc;
clear all;
A=[X X X X X ; X X X X X ;...];
b=[X X X X X]';
[m, n] = size(A);
% Non-square A, exit with error()
if m ~= n
error('A needs to be square!');
end
% b is not a column vectorQ, exit
[bm, bn] = size(b);
if bn ~= 1
error('b must be a column vector!');
end
% A and b sizes must agree
if m ~= bm
error('A and b size mismatch!');
end
Aug = [A b]; % Argumented Matrix
nb = n+1; % Column index of b values in Aug
% Forward Elimination
% Aug(1,1), Aug(2,2) ... etc
% So, Aug(p,p) is the current pivot element
for p = 1:n-1
for r = p+1:n
factor = Aug(r,p) / Aug(p,p);
Aug(r,p:nb) = Aug(r,p:nb) - factor*Aug(p,p:nb);
end
end
%Backward Substitution
x = zeros(size(b));
% Calculating last x value
x(n) = Aug(n,nb) / Aug(n, n);
% Again, Aug(p,p) is the pivot
% Moving through x values backwards
for p = n-1:-1:1
x(p) = (Aug(p, nb) - Aug(p, p+1:n)*x(p+1:n) ) / Aug(p,p);
end
x; % x is yr result