品易云推流 关闭
文章详情页
文章 > Python基础教程 > python如何求解线性方程组?

python如何求解线性方程组?

头像

宋雪维

2021-02-19 10:06:359195浏览 · 0收藏 · 0评论

小编介绍过python中使用scipy.linalg模块计算矩阵的行列式的方法,既然scipy.linalg模块可以进行线性计算,那是不是可以求解线性方程组,答案当然是可以的,使用scipy.linalg.solve()就可以简单的实现求解线性方程组,本文介绍python中使用scipy.linalg.solve()求解线性方程组的过程。

一、导入scipy.linalg模块

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()求解线性方程组

使用格式

print('Sol:',lg.solve(arr,b)) #求方程组arr*x=b的解

使用实例

#  求解线性方程组

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)

输出

[0.6  0.35 0.15]

以上就是python中使用scipy.linalg.solve()求解线性方程组的过程,希望能帮助你解决问题哟~

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图