准确地说,误差是:
x2new= solve(n.subs(x1,0).subs(x3,0))
AttributeError: 'int' object has no attribute 'subs'
这是我的代码:
from sympy import symbols, Eq, solve, sympify
x, y, z= symbols('x y z')
a= 6*x - 2*y + z - 11
b= -2*x + 7 *y + 2*z - 5
c= x + 2*y - 5*z + 1
m=Eq(sympify(a))
n=Eq(sympify(b))
o=Eq(sympify(c))
x1=solve(m.subs(y,0).subs(z,0))
x2=solve(n.subs(x,0).subs(z,0))
x3=solve(o.subs(x,0).subs(y,0))
n= 2
for i in range(n):
x1new= solve(m.subs(x2,0).subs(x3,0))
x2new= solve(n.subs(x1,0).subs(x3,0))
x3new= solve(o.subs(x1,0).subs(x2,0))
x1= x1new
x2= x2new
x3= x3new
i= i+1
print(x1new)
print(x2new)
print(x3new)
它抱怨是因为它需要
SymPy
个数字,而不是内置的pythonint
如果仔细查看代码,您会注意到您意外地覆盖了变量
n
:如果您只是重命名for循环计数器变量,它将正常工作:
其结果是:
相关问题 更多 >
编程相关推荐