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

  • 相关阅读:
    Java--泛型
    Java--文档注释
    Java--网络编程
    Java--序列化
    Java--多线程编程
    Java--ConcurrentHashMap原理分析
    HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别
    面向接口编程详解
    面向接口编程
    base64 数据加密
  • 原文地址:https://www.cnblogs.com/WeyneChen/p/6694132.html
Copyright © 2011-2022 走看看