
小编介绍过python中使用scipy.linalg模块计算矩阵的行列式的方法,既然scipy.linalg模块可以进行线性计算,那是不是可以求解线性方程组,答案当然是可以的,使用scipy.linalg.solve()就可以简单的实现求解线性方程组,本文介绍python中使用scipy.linalg.solve()求解线性方程组的过程。
一、导入scipy.linalg模块
1 2 3 4 | import numpy as np #导入numpy库
from scipy import linalg as lg #导入scipy库的linalg模块
arr=np. array ([[1,2],[3,4]]) #创建方阵arr
b=np. array ([6,14]) #创建矩阵b
|
二、使用scipy.linalg.solve()求解线性方程组
使用格式
1 | print ( 'Sol:' ,lg.solve(arr,b)) #求方程组arr*x=b的解
|
使用实例
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 求解线性方程组
from scipy import linalg
import numpy as np
# x1 + x2 + 7*x3 = 2
# 2*x1 + 3*x2 + 5*x3 = 3
# 4*x1 + 2*x2 + 6*x3 = 4
A = np. array ([[1, 1, 7], [2, 3, 5], [4, 2, 6]]) # A代表系数矩阵
b = np. array ([2, 3, 4]) # b代表常数列
x = linalg.solve(A, b)
print (x)
|
输出
以上就是python中使用scipy.linalg.solve()求解线性方程组的过程,希望能帮助你解决问题哟~