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

学习 时间:2026-04-04 18:35:33 阅读:9574
高斯列主元消元法的matlab程序

最佳回答

留胡子的香氛

慈祥的高跟鞋

2026-04-04 18:35:33

两年前写的代码 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 agreeif m ~= bm error('A and b size mismatch!');endAug = [A b]; % Argumented Matrixnb = 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 elementfor 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); endend %Backward Substitution x = zeros(size(b));% Calculating last x valuex(n) = Aug(n,nb) / Aug(n, n);% Again, Aug(p,p) is the pivot% Moving through x values backwardsfor p = n-1:-1:1 x(p) = (Aug(p, nb) - Aug(p, p+1:n)*x(p+1:n) ) / Aug(p,p);endx; % x is yr result

最新回答共有2条回答

  • 忧郁的煎蛋
    回复
    2026-04-04 18:35:33

    两年前写的代码 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 agreeif m ~= bm error('A and b size mismatch!');endAug = [A b]; % Argumented Matrixnb = 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 elementfor 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); endend %Backward Substitution x = zeros(size(b));% Calculating last x valuex(n) = Aug(n,nb) / Aug(n, n);% Again, Aug(p,p) is the pivot% Moving through x values backwardsfor p = n-1:-1:1 x(p) = (Aug(p, nb) - Aug(p, p+1:n)*x(p+1:n) ) / Aug(p,p);endx; % x is yr result

上一篇 惊奇的什么填空

下一篇 They were difficult for me before they became my habbits.此句中