高斯消元法求逆矩阵

A=[1,-1,1,-4;5,-4,3,12;2,1,1,11;2,-1,7,-1]
E=[1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1]
b=[2,4,3,0]'
%开始消元过程
for k=1:(length(A))
   a=A(k,k)
   for i=1:(length(A))
      A(k,i)=A(k,i)/a
      E(k,i)=E(k, i)/a
   end
   b(k,1)=b(k,1)/ a
   for i=k+1:(length(A))
      c=-A(i,k)
	  for j=1: (length(A))
		A(i,j)=A(i,j)+c.*A(k,j)
        E(i,j)=E(i,j)+c.*E(k,j)
	  end
	  b(i,1)= b(i,1)+ c.* b(k,1)
   end
end
%开始回代过程
for i=(length(A)):-1:1
	for j=i-1:-1:1
	   c=-A(j,i)
	   for k=(length(A)):-1:1
		  A(j,k)= A(j,k)+c.*A(i,k)
          E(j,k)= E(j,k)+c.*E(i,k)
	   end
 b(j,1)= b(j,1)+ c.* b(i,1)
	end
end
E

A =

     1    -1     1    -4
     5    -4     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


b =

     2
     4
     3
     0


a =

     1


A =

     1    -1     1    -4
     5    -4     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     5    -4     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     5    -4     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     5    -4     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


b =

     2
     4
     3
     0


c =

    -5


A =

     1    -1     1    -4
     0    -4     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
     0     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     0     1     3    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
     0     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     0     1    -2    12
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
     0     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     2     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
     0     0     1     0
     0     0     0     1


b =

     2
    -6
     3
     0


c =

    -2


A =

     1    -1     1    -4
     0     1    -2    32
     0     1     1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3     1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    11
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
     0     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     2    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
     0     0     0     1


b =

     2
    -6
    -1
     0


c =

    -2


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0    -1     7    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     7    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5    -1


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


b =

     2
    -6
    -1
    -4


a =

     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    -2     0     1     0
    -2     0     0     1


b =

     2
    -6
    -1
    -4


c =

    -3


A =

     1    -1     1    -4
     0     1    -2    32
     0     3    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    13     0     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0    -1    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5    19
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
    -2     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
    -2     0     0     1


b =

     2
    -6
    17
    -4


c =

    -1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     1     5     7


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
     3     0     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     0     5     7


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
     3    -1     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     0     7     7


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
     3    -1     0     1


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     0     7   -25


E =

     1     0     0     0
    -5     1     0     0
    13    -3     1     0
     3    -1     0     1


b =

     2
    -6
    17
     2


a =

     5


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     0     7   -25


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -3.0000    1.0000         0
    3.0000   -1.0000         0    1.0000


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     5   -77
     0     0     7   -25


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    1.0000         0
    3.0000   -1.0000         0    1.0000


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     1   -77
     0     0     7   -25


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
    3.0000   -1.0000         0    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0    7.0000  -25.0000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
    3.0000   -1.0000         0    1.0000


b =

    2.0000
   -6.0000
    3.4000
    2.0000


c =

    -7


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0    7.0000  -25.0000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
  -15.2000   -1.0000         0    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0    7.0000  -25.0000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
  -15.2000    3.2000         0    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0         0  -25.0000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
  -15.2000    3.2000   -1.4000    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0         0   82.8000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
  -15.2000    3.2000   -1.4000    1.0000


b =

    2.0000
   -6.0000
    3.4000
  -21.8000


a =

   82.8000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0         0   82.8000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
   -0.1836    3.2000   -1.4000    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0         0   82.8000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
   -0.1836    0.0386   -1.4000    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0         0   82.8000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
   -0.1836    0.0386   -0.0169    1.0000


A =

    1.0000   -1.0000    1.0000   -4.0000
         0    1.0000   -2.0000   32.0000
         0         0    1.0000  -15.4000
         0         0         0    1.0000


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000         0
   -0.1836    0.0386   -0.0169    0.0121


b =

    2.0000
   -6.0000
    3.4000
   -0.2633


c =

   15.4000


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000    0.2000    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.6000   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
    2.6000   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1    -4
     0     1    -2    32
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000    1.0000         0         0
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


b =

    2.0000
   -6.0000
   -0.6546
   -0.2633


c =

   -32


A =

     1    -1     1    -4
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000    1.0000         0   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1    -4
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000    1.0000    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1    -4
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
   -5.0000   -0.2367    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1    -4
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0         0
    0.8744   -0.2367    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


b =

    2.0000
    2.4251
   -0.6546
   -0.2633


c =

     4


A =

     1    -1     1     0
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000         0         0    0.0483
    0.8744   -0.2367    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1     0
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000         0   -0.0676    0.0483
    0.8744   -0.2367    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1     0
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    1.0000    0.1546   -0.0676    0.0483
    0.8744   -0.2367    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1     0
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0676    0.0483
    0.8744   -0.2367    0.5411   -0.3865
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


b =

    0.9469
    2.4251
   -0.6546
   -0.2633


c =

     2


A =

     1    -1     1     0
     0     1    -2     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0676    0.0483
    0.8744   -0.2367    0.5411   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0676    0.0483
    0.8744   -0.2367    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0676    0.0483
    0.8744   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     1     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0676    0.0483
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


b =

    0.9469
    1.1159
   -0.6546
   -0.2633


c =

    -1


A =

     1    -1     1     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0676   -0.1377
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1546   -0.0072   -0.1377
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.2657    0.1594   -0.0072   -0.1377
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.4928    0.1594   -0.0072   -0.1377
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


b =

    1.6014
    1.1159
   -0.6546
   -0.2633


c =

     1


A =

     1    -1     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.4928    0.1594   -0.0072   -0.1522
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1    -1     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.4928    0.1594    0.4130   -0.1522
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.4928   -0.0870    0.4130   -0.1522
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


A =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


E =

    0.9130   -0.0870    0.4130   -0.1522
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121


b =

    2.7174
    1.1159
   -0.6546
   -0.2633


E =

    0.9130   -0.0870    0.4130   -0.1522
    0.4203   -0.2464    0.4203   -0.0145
   -0.2271   -0.0048   -0.0604    0.1860
   -0.1836    0.0386   -0.0169    0.0121

  

posted @ 2016-11-18 20:23  木卜木卜木卜木卜木卜  阅读(1453)  评论(0编辑  收藏  举报