我在spyder:AttributeError中得到了这个信息:“int”对象没有属性“subs”

2024-04-27 00:56:23 发布

您现在位置:Python中文网/ 问答频道 /正文

准确地说,误差是:

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)

Tags: int误差eqattributeerrorsubsprintx1x2
1条回答
网友
1楼 · 发布于 2024-04-27 00:56:23

它抱怨是因为它需要SymPy个数字,而不是内置的python int

如果仔细查看代码,您会注意到您意外地覆盖了变量n

n=Eq(sympify(b))
n= 2

如果您只是重命名for循环计数器变量,它将正常工作:

from sympy import symbols, Eq, solve, sympify, Integer

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), 0)
n=Eq(sympify(b), 0)
o=Eq(sympify(c), 0)

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))

counter = 2
for i in range(counter):
    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)

其结果是:

[{x: y/3 - z/6 + 11/6}]
[{x: 7*y/2 + z - 5/2}]
[{x: -2*y + 5*z - 1}]
[{x: y/3 - z/6 + 11/6}]
[{x: 7*y/2 + z - 5/2}]
[{x: -2*y + 5*z - 1}]

相关问题 更多 >