我刚开始学习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
形式。
您可以使用此方法轻松找到 (x,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/