高斯消元法求逆矩阵

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

  

时间: 2024-12-19 13:45:23

高斯消元法求逆矩阵的相关文章

(模板)求逆矩阵luoguP4783

题目链接:https://www.luogu.org/problem/P4783 题意:求矩阵的逆. 思路:高斯消元法求矩阵的逆,n为400,卡常,我是开了O2优化才AC的.. AC代码: #include<cstdio> #include<cctype> #include<algorithm> #define R register int using namespace std; const int maxn=405; const int MOD=1e9+7; int

选主元的高斯-约旦(Gauss-Jordan)消元法解线性方程组/求逆矩阵

做数据结构课设时候查的资料,主要是看求逆矩阵方面的知识的. 选主元的高斯-约当(Gauss-Jordan)消元法在很多地方都会用到,例如求一个矩阵的逆矩阵.解线性方程组(插一句:LM算法求解的一个步骤),等等.它的速度不是最快的,但是它非常稳定(来自网上的定义:一个计算方法,如果在使用此方法的计算过程中,舍入误差得到控制,对计算结果影响较小,称此方法为数值稳定的),同时它的求解过程也比较清晰明了,因而人们使用较多.下面我就用一个例子来告诉你Gauss-Jordan法的求解过程吧.顺便再提及一些注

使用lapack库求逆矩阵

废话不多说,直接上代码: #include <string> #include "lapacke.h" #include "lapack_aux.h" int main(int argc,char** argv) { setlocale(LC_ALL,""); double a[] = { 3,-1,-1, 4,-2,-1, -3,2,1 }; int m = 3; int n = 3; int lda = 3; int ipiv[3

matlab中求逆矩阵的高斯消元法实现的代码

function qiuni =INV_GET(a)N=length(a);M=eye(N);%得到上三角矩?for i=1:N max=a(i,i); A=i; for j=i+1:N if(abs(a(j,i))>abs(max))%找最大值 max=a(j,i); A=j; end end for m=1:N temp1=a(i,m);%交换最大值值所在的行和当前行 a(i,m)=a(A,m); a(A,m)=temp1; temp2=M(i,m); M(i,m)=M(A,m); M(A,

求逆矩阵【模板】

题目 P4783 求一个 $N \times N$ 的矩阵的逆矩阵.答案对 $10^9+7$ 取模.若不可逆,输出 "No Solution". 分析 由线性代数的知识,求矩阵A的逆矩阵时, 只需在A的右边补充一个单位矩阵,进行初等行变换,当A变成单位矩阵时,右边的就是A的逆矩阵. 简单的证明:$AE\rightarrow E{A}'$ 代码 //来自https://blog.csdn.net/qq_43653202/article/details/99976316 #include&

利用初等变换求逆矩阵和解矩阵方程

求行列式,验证矩阵可逆 原文地址:https://www.cnblogs.com/YC-L/p/12234164.html

高斯消元法求特征值

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

高斯消元法求矩阵的行列式

A=[1,-1,1,-4;5,-4,3,12;2,1,1,11;2,-1,7,-1] Adet=1 %开始消元过程 for k=1:(length(A)) a=A(k,k) Adet = Adet.*a for i=1:(length(A)) A(k,i)=A(k,i)/a end 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) end end end Adet A = 1 -1 1 -4

求逆矩阵

转自http://blog.csdn.net/abcjennifer/article/details/6693612 #include<stdio.h> #define N 10 int getA(int arcs[N][N],int n)//按第一行展开计算|A| { if(n==1) { return arcs[0][0]; } int ans = 0; int temp[N][N]; int i,j,k; for(i=0;i<n;i++) { for(j=0;j<n-1;j+