简体   繁体   English

如何使用Python解决2个未知数的一个线性代数方程

[英]How to solve one linear algebra equation with 2 unknowns using Python

Trying to solve problem of 2 investments with 2% and 4% annual interest respectively. 试图解决分别年利率为2%和4%的两项投资的问题。 If $10,000 total was originally invested and the account had $10,250 at year end, what was the principal in each investment. 如果最初投资总额为10,000美元,并且该帐户的年末余额为10,250美元,那么每笔投资的本金是多少。 I've tried using the solve function 我试过使用solve函数

This is the equation I came up with to represent the problem. 这是我想出的代表问题的方程式。 1.04x+1.02y=10000 * 1.025 1.04x + 1.02y = 10000 * 1.025

However I'm not sure that I'm properly placing the coefficients into the arrays. 但是我不确定是否正确地将系数放入数组中。

This is the code I set up: 这是我设置的代码:

import numpy as np

A = np.array([[1.0,1.0],[104,102]])
Y = np.array([[1.025],[10000.0]])
np.linalg.solve(A, Y) 

I'd expect the results to be $7500 and $2500 which satisfy the equation and up to $10,250, however running my code I get: 我希望结果是满足方程式的$ 7500和$ 2500,最高可达$ 10,250,但是运行我的代码,我得到:

array([[ 4947.725],
       [-4946.7  ]])

I think you misswrote your matrix system: 我认为您错用了矩阵系统:

import numpy as np

A = np.array([[1.0,1.0],[1.04,1.02]])
Y = np.array([[10000.0],[10250.0]])
np.linalg.solve(A, Y)
>>>array([[2500.], [7500.]])

Which is what you expected 你所期望的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM