永发信息网

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

答案:1  悬赏:20  手机版
解决时间 2021-07-23 19:26
  • 提问者网友:爱唱彩虹
  • 2021-07-23 04:05
高斯列主元消元法的matlab程序
最佳答案
  • 五星知识达人网友:污到你湿
  • 2021-07-23 04:49

两年前写的代码 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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯