# 7.3 선형연립방정식. Gauss 소거법

### (a) 유일한 해를 갖는 경우

x,y = var('x,y') eq1 = x + y == 1 eq2 = 2*x - y == 0 r=solve([eq1,eq2],x,y ) show(eq1) show(eq2) show(r)
 \newcommand{\Bold}[1]{\mathbf{#1}}x + y = 1 \newcommand{\Bold}[1]{\mathbf{#1}}2 \, x - y = 0 \newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = \left(\frac{1}{3}\right), y = \left(\frac{2}{3}\right)\right]\right]

### 경우 (a)의 그림

x,y = var('x,y') L1(x,y) = x + y - 1 P1=implicit_plot(L1, (-1/2,2), (-1/2,2),color='red', axes=true, plot_points=500) L2(x,y) = 2*x - y P2=implicit_plot(L2, (-1/2,2), (-1/2,2), axes=true, plot_points=500) P1+P2

### (b) 무한히 많은 해를 갖는 경우

x,y = var('x,y') eq1 = x + y == 1 eq2 = 2*x + 2*y == 2 r=solve([eq1,eq2],x,y ) show(eq1) show(eq2) show(r)
 \newcommand{\Bold}[1]{\mathbf{#1}}x + y = 1 \newcommand{\Bold}[1]{\mathbf{#1}}2 \, x + 2 \, y = 2 \newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = -r_{1} + 1, y = r_{1}\right]\right]

### 경우 (b)의 그림

x,y = var('x,y') L1(x,y) = x + y - 1 P1=implicit_plot(L1, (-1/2,2), (-1/2,2),color='red', axes=true, plot_points=500) L2(x,y) = 2*x + 2*y - 2 P2=implicit_plot(L2, (-1/2,2), (-1/2,2), axes=true, plot_points=500) P1+P2

### (c) 해가 없는 경우

x,y = var('x,y') eq1 = x + y == 1 eq2 = x + y == 0 r=solve([eq1,eq2],x,y ) show(eq1) show(eq2) show(r)
 \newcommand{\Bold}[1]{\mathbf{#1}}x + y = 1 \newcommand{\Bold}[1]{\mathbf{#1}}x + y = 0 \newcommand{\Bold}[1]{\mathbf{#1}}\left[\right]

### 경우 (c)의 그림

x,y = var('x,y') L1(x,y) = x + y - 1 P1=implicit_plot(L1, (-1/2,2), (-1/2,2),color='red', axes=true, plot_points=500) L2(x,y) = x + y P2=implicit_plot(L2, (-1/2,2), (-1/2,2), axes=true, plot_points=500) P1+P2

### 선형연립방정식

x,y,z = var('x,y,z') eq1 = x - y + z == 0 eq2 = -x + y - z == 0 eq3 = 10*y + 25*z == 90 eq4 = 20*x + 10*y == 80 show(eq1) show(eq2) show(eq3) show(eq4) r=solve([eq1,eq2,eq3,eq4],x,y,z) show(r)
 \newcommand{\Bold}[1]{\mathbf{#1}}x - y + z = 0 \newcommand{\Bold}[1]{\mathbf{#1}}-x + y - z = 0 \newcommand{\Bold}[1]{\mathbf{#1}}10 \, y + 25 \, z = 90 \newcommand{\Bold}[1]{\mathbf{#1}}20 \, x + 10 \, y = 80 \newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[x = 2, y = 4, z = 2\right]\right]

## Gauss 소거법에 의한 해

### 0단계. 첨가행렬 만들기

A= matrix([[1,-1,1],[-1,1,-1],[0,10,25],[20,10,0]]) b=vector([0,0,90,80]) Ab=A.augment(b) #Augmented matrix print "[A:b] = " show(Ab)
 [A:b] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 1 & -1 & 1 & 0 \\ -1 & 1 & -1 & 0 \\ 0 & 10 & 25 & 90 \\ 20 & 10 & 0 & 80 \end{array}\right) [A:b] =

### $$R_4 + (-20)R_1$$

 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 1 & -1 & 1 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 10 & 25 & 90 \\ 0 & 30 & -20 & 80 \end{array}\right)

### $$R_3 \leftrightarrow R_4$$

Ab.swap_rows(1,2) Ab.swap_rows(2,3) show(Ab)
 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 1 & -1 & 1 & 0 \\ 0 & 10 & 25 & 90 \\ 0 & 30 & -20 & 80 \\ 0 & 0 & 0 & 0 \end{array}\right)

### 2단계. 두 번째 행의 주축 아래를 $0$으로 만든다.

$$R_3 + (-3)R_2$$

 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 1 & -1 & 1 & 0 \\ 0 & 10 & 25 & 90 \\ 0 & 0 & -95 & -190 \\ 0 & 0 & 0 & 0 \end{array}\right)

### 후치환. 위삼각행렬 형태(행사다리꼴)가 되면 방정식으로 돌아가서 마지막 방정식에서 첫 번째 방정식까지 아래로부터 계산한다.

R=Ab[0:3,0:3] print "R=" show(R) c=Ab[0:3,3] print "c=" show(c) x=R.solve_right(c) print "x=" show(x)
 R= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & -1 & 1 \\ 0 & 10 & 25 \\ 0 & 0 & -95 \end{array}\right) c= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 0 \\ 90 \\ -190 \end{array}\right) x= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 2 \\ 4 \\ 2 \end{array}\right) R= c= x=

### 풀이) $A \mathbf{x} = \mathbf{b}$ 의 행렬식 형태로 쓴다.  그리고 첨가행렬 $[A:\mathbf{b}]$를 만든다.

RF = RealField(10) A= matrix(RF,[[3.0,2.0,2.0,-5.0],[0.6,1.5,1.5,-5.4],[1.2,-0.3,-0.3,2.4]]) b=vector([8.0,2.7,2.1]) Ab=A.augment(b) #Augmented matrix print "[A:b] = " show(Ab)
 [A:b] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrr} 3.0 & 2.0 & 2.0 & -5.0 & 8.0 \\ 0.60 & 1.5 & 1.5 & -5.4 & 2.7 \\ 1.2 & -0.30 & -0.30 & 2.4 & 2.1 \end{array}\right) [A:b] =

### $$R_3 - 0.4 R_1$$

 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrr} 3.0 & 2.0 & 2.0 & -5.0 & 8.0 \\ 0.00 & 1.1 & 1.1 & -4.4 & 1.1 \\ 0.00 & -1.1 & -1.1 & 4.4 & -1.1 \end{array}\right)

### $$R_3 + R_2$$

 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrr} 3.0 & 2.0 & 2.0 & -5.0 & 8.0 \\ 0.00 & 1.1 & 1.1 & -4.4 & 1.1 \\ 0.00 & 0.00 & 0.00 & 0.00 & 0.0020 \end{array}\right)

### 후치환. 위삼각행렬 형태(행사다리꼴)가 되면 방정식으로 돌아가서 마지막 방정식에서 첫 번째 방정식까지 아래로부터 계산한다.

R=Ab[0:3,0:4] print "R=" show(R) c=matrix(RF,3,1,[8,1.1,0]) print "c=" show(c) x=R.solve_right(c) print "x=" show(x)
 R= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 3.0 & 2.0 & 2.0 & -5.0 \\ 0.00 & 1.1 & 1.1 & -4.4 \\ 0.00 & 0.00 & 0.00 & 0.00 \end{array}\right) c= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 8.0 \\ 1.1 \\ 0.00 \end{array}\right) x= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 2.0 \\ 1.0 \\ 0.00 \\ 0.00 \end{array}\right) R= c= x=

### 풀이) $A \mathbf{x} = \mathbf{b}$ 의 행렬식 형태로 쓴다.  그리고 첨가행렬 $[A:\mathbf{b}]$를 만든다.

A= matrix(QQ,[[3,2,1],[2,1,1],[6,2,4]]) b=vector([3,0,6]) Ab=A.augment(b) #Augmented matrix print "[A:b] = " show(Ab)
 [A:b] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 3 & 2 & 1 & 3 \\ 2 & 1 & 1 & 0 \\ 6 & 2 & 4 & 6 \end{array}\right) [A:b] =

### $$R_3 -2 R_1$$

 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 3 & 2 & 1 & 3 \\ 0 & -\frac{1}{3} & \frac{1}{3} & -2 \\ 0 & -2 & 2 & 0 \end{array}\right)

### $$R_3 - 6 R_2$$

 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 3 & 2 & 1 & 3 \\ 0 & -\frac{1}{3} & \frac{1}{3} & -2 \\ 0 & 0 & 0 & 12 \end{array}\right)
 R= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 3 & 2 & 1 \\ 0 & -\frac{1}{3} & \frac{1}{3} \\ 0 & 0 & 0 \end{array}\right) c= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 3 \\ -2 \\ 12 \end{array}\right) Traceback (click to the left of this block for traceback) ... ValueError: matrix equation has no solutions R= c= Traceback (most recent call last): x=R.solve_right(c) File "", line 1, in File "/tmp/tmpHqmmtt/___code___.py", line 9, in x=R.solve_right(c) File "matrix2.pyx", line 358, in sage.matrix.matrix2.Matrix.solve_right (sage/matrix/matrix2.c:5361) File "matrix2.pyx", line 476, in sage.matrix.matrix2.Matrix._solve_right_general (sage/matrix/matrix2.c:6182) ValueError: matrix equation has no solutions