[英]how to use python sympy to solve for 3 unknowns with 3 equations
i have 3 equations: 我有3个方程式:
165 ⋅ 2 ⋅ 𝑦1 = 0.310 ⋅ 2 ⋅ 𝑛 + 0.517 ⋅ 2 ⋅ 𝑛 165⋅2⋅𝑦1= 0.310⋅2⋅𝑛+ 0.517⋅2⋅𝑛
165 ⋅ 𝑦2 = 0.173 ⋅ 𝑛 + 0.517 ⋅ 𝑛 165⋅𝑦2= 0.173⋅𝑛+ 0.517⋅𝑛
𝑦1 + 𝑦2 = 1.0 𝑦1+𝑦2= 1.0
I reorganized the first 2 equations to equal Y1 and Y2 then entered them into python: 我将前两个方程重组为Y1和Y2,然后将它们输入python:
import sympy as sp
n,Y1,Y2 = sp.symbols('n Y1 Y2')
Y1= ((.310*2*n)+(.517*2*n))/(165*2)
Y2= ((.173*n)+(.517*n))/(165)
print ("Y1=", Y1,"Y2=", Y2,"n=", n)
my first question is did I set up the code right? 我的第一个问题是我设置的代码正确吗? or should I move the Y variables to the right side of the equation and label them as equation 1 and 2?
还是应该将Y变量移至方程式的右侧,并将其标记为方程式1和2?
secondly, while I tried entering the 3rd equation but got a "syntax error: can't assign to operator". 其次,当我尝试输入第三个方程式时,出现了“语法错误:无法分配给运算符”。 how would I incorporate the 3rd equation within the code?
如何将第三个方程式纳入代码中? would it act more like a "limit" (I know it's not a limit but I can't think of what its called at the moment)
它会更像一个“极限”吗(我知道这不是极限,但我现在无法想到它叫什么)
You can pass all three equations simultaneously and get the three variables directly using solve
as following: Pass the three equations where in Eq
you write the left hand side of the equation and the right hand side of the equation (or vice versa). 您可以同时传递所有三个方程式,并使用
solve
直接获得三个变量:传递三个方程式,其中在Eq
您写出方程式的左手边和方程式的右手边(反之亦然)。 The second argument of solve
is the list of variables to be solved. solve
的第二个参数是要求solve
的变量列表。
from sympy import *
n, Y1, Y2 = symbols('n Y1 Y2')
solve([Eq(((.310*2*n)+(.517*2*n))/(165*2), Y1), Eq(((.173*n)+(.517*n))/165, Y2),
Eq(Y1+Y2, 1)], [n, Y1, Y2])
> {n: 108.767303889255, Y1: 0.545154911008569, Y2: 0.454845088991430} # Answer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.