实验八:求解线性方程组

本博文源于python解决线性方程组的求解问题。主要使用sympy包。涉及的题目类型有解齐次线性方程组与线性

1.求解齐次线性方程组

y = { x 1 + 2 x 2 + 2 x 3 + x 4 = 0 2 x 1 + x 2 − 2 x 3 − 2 x 4 = 0 x 1 − x 2 − 4 x 3 − 3 x 4 = 0 y=\left\{ \begin{array}{rcl} x_1+2x_2+2x_3+x_4=0\\ 2x_1+x_2-2x_3-2x_4=0\\ x_1-x_2-4x_3-3x_4=0\\ \end{array} \right. y=x1+2x2+2x3+x4=02x1+x22x32x4=0x1x24x33x4=0

>>> from sympy import *
>>> init_printing(use_unicode=True)
>>> A = Matrix([[1,2,2,1],[2,1,-2,-2],[1,-1,-4,-3]])
>>> A.rank()
2
>>> A.rref()[0]1  0  -2  -5/3⎤
⎢              ⎥
⎢0  1  2   4/3 ⎥
⎢              ⎥
⎣0  0  0    0>>>

2.求解线性方程组的解

y = { 2 x 1 − x 2 + 3 x 3 = 5 3 x 1 + x 2 − 5 x 3 = 5 4 x 1 − x 2 + x 3 = 9 y=\left\{ \begin{array}{rcl} 2x_1-x_2+3x_3=5\\ 3x_1+x_2-5x_3=5\\ 4x_1-x_2+x_3=9\\ \end{array} \right. y=2x1x2+3x3=53x1+x25x3=54x1x2+x3=9

>>> from sympy import *
>>> init_printing(use_unicode=True)
>>> A = Matrix([[2,-1,3],[3,1,-5],[4,-1,1]])
>>> A1 = Matrix([[2,-1,3,5],[3,-1,5,5],[4,-1,1,9]])
>>> A.rank()
3
>>> A1.rank()
3
>>> A1 = Matrix([[2,-1,3,5],[3,1,-5,5],[4,-1,1,9]])
>>> A1.rref()[0]1  0  0  2 ⎤
⎢           ⎥
⎢0  1  0  -1⎥
⎢           ⎥
⎣0  0  1  0>>>

Guess you like

Origin blog.csdn.net/m0_37149062/article/details/120736836