python如何求解线性方程组?

2025-12-01 0 94,638

小编介绍过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()求解线性方程组的过程,希望能帮助你解决问题哟~

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 python如何求解线性方程组? https://www.zhiup.top/2471.html

相关