python - 求解两个变量的方程组 - Python

标签 python python-3.x

我刚开始学习python,学习了变量、输入和基础数学。

我被要求写一个具有参数的数学练习:

ax+by=c, dx+ey=f

a, b, c, d,e, f - the user input and than the program result and write the answear for x, y

我做到了:

number1 = float(input('Insert a number1: '))

number2 = float(input('Insert a number2: '))

number3 = float(input('Insert a number3: '))

number4 = float(input('Insert a number4: '))

number5 = float(input('Insert a number:5 '))

number6 = float(input('Insert a number6: '))

我不会写一个有两个变量的方程

x=number1+2.5*number2-number3 #(it should be looked like ax+by=c)

y=number5+2.5*number6-number4

ax+by=c AND dx+ey=f ==> x=(-by+ey-f+c)(a-d)

我也不知道为什么不能在print里面写变量:

print('the value of x, y is') print((x))

最佳答案

你可以把上面的等式写成matrix形式。

writing in matrix form

您可以使用此方法轻松找到 (x,y) 的答案。你只需要解决这个矩阵方程。

solution to x and y

您可以使用 numpy 找到答案。 (或者你只需​​要自己实现矩阵求逆和乘法)

import numpy as np

A = np.array([[a, b], [d, e]])
B = np.array([[c], [f]])
print(np.linalg.inv(A) @ B)

关于python - 求解两个变量的方程组 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757280/

相关文章:

python - 在列表中查找连续数字的序列

python - Bokeh 服务器的入口点

python - 循环二值图像像素

python - 如何构建我的 Python 包?

python - 以固定时间间隔计算总和

python - 这两个代码块(如果有)的时间复杂度有什么区别,为什么?

python - 不在列表中 - 列表理解

python - 'Connection aborted .', ConnectionResetError(104, ' Connection reset by peer')通过 Python 将 Selenium 与 ChromeDriver 和 Chrome 结合使用

Python:遍历子列表

python - pygame.错误: mixer not initialized How To Fix?