• 技术文章 >Python技术 >Python基础教程

    python如何求解线性方程组?

    宋雪维宋雪维2021-02-19 10:06:35原创8580

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

    专题推荐:python
    品易云
    上一篇:python中scipy.special模块如何使用? 下一篇:python中scipy.linalg.inv()功能是什么?

    相关文章推荐

    • python中setWindowIcon设置窗体图标• python中scipy.special模块如何使用?• python中如何使用scipy.linalg模块计算矩阵的行列式?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网