zoukankan      html  css  js  c++  java
  • numpy 解一道简单数学题

    题目

    A group took a trip on a bus, at 3 per child and 3.20 per adult for a total of 118.40.

    They took the train back at 3.50 per child and 3.60 per adult for a total of 135.20.

    How many children, and how many adults?

    求解过程

    设有x1个children,x2个adults,线性方程组为:

    [3x_1+3.2x_2 = 118.4 ]

    [3.5x_1+3.6x_2 = 135.2 ]

    矩阵形式表示为:

    [egin{bmatrix} 3 & 3.2 \ 3.5 & 3.6 end{bmatrix} egin{bmatrix} x_1 \ x_2 \ end{bmatrix} = egin{bmatrix} 118.4 \ 135.2 end{bmatrix} ]

    化为:

    [egin{bmatrix} x_1 \ x_2 end{bmatrix} = { egin{bmatrix} 3 & 3.2 \ 3.5 & 3.6 end{bmatrix} }^{-1} egin{bmatrix} 118.4 \ 135.2 end{bmatrix} ]

    接下来就是使用numpy求解了

    代码

    import numpy as np
    
    
    a = np.matrix('3. 3.2;3.5,3.6')
    b = np.matrix('118.4;135.2')
    
    inverse_a = np.linalg.inv(a)
    print(inverse_a)
    
    result = inverse_a * b
    print(result)
    
    

    结果

    [inverse\_a = egin{bmatrix} -9 & 8 \ 8.75 & -7.5 end{bmatrix} ]

    [result = egin{bmatrix} 16 \ 22 end{bmatrix} ]

    也就是说是16个children,22个adults

  • 相关阅读:
    简单理解Socket
    进程间8种通信方式详解
    底部漂浮DIV
    Table样式
    QQ授权登录
    C#_批量插入数据到Sqlserver中的四种方式
    Asp.Net_单点登录
    html之meta详解
    程序员常用工具
    工厂模式理解
  • 原文地址:https://www.cnblogs.com/WeyneChen/p/6694132.html
Copyright © 2011-2022 走看看