Sympy 在求解方程时返回 ConditionSet 对象,而 TINspire 返回数值
这是我的 python 代码:
import sympy as sym
x = sym.Symbol('x')
f = -0.077 * sym.Pow(x - 35.80, 2) + 100
f_prime = f.diff(x)
f_integrate = sym.integrate(sym.sqrt(1 + sym.Pow(f_prime, 2)), x)
我希望能够编写这个积分并用 Sympy 求解它(因此,我想隔离变量 b)。
更新 现在这是我的代码。
f_integrate = sym.integrate(sym.sqrt(1 + sym.Pow(f_prime, 2))-c, (x, 0, b))
g = 100
for i in range(10, 72):
bi = sym.nsolve(f_integrate.subs(c,i), g)
g = bi
return g
这段代码适用于我编写的初始函数 (f(x)= -0.077 * sym.Pow(x - 35.80, 2) + 100),但对于几乎任何其他函数,我收到此错误:
Traceback (most recent call last):
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 543, in <module>
drawManager(line.copy())
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 199, in drawManager
t = createT(FU, f, x, distanceNextSyllable)
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 107, in createT
print(getPosition(f, x, 0, distanceNextSyllable))
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 95, in getPosition
bi = sym.nsolve(f_integrate.subs(c,i), g)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\sympy\utilities\decorator.py", line 88, in func_wrapper
return func(*args, **kwargs)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\sympy\solvers\solvers.py", line 2937, in nsolve
x = sympify(findroot(f, x0, **kwargs))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\mpmath\calculus\optimization.py", line 985, in findroot
raise ValueError('Could not find root within given tolerance. '
ValueError: Could not find root within given tolerance. (5.9604644775390625e-8 > 2.16840434497100886801e-19)
Try another starting point or tweak arguments.
Here is my python code:
import sympy as sym
x = sym.Symbol('x')
f = -0.077 * sym.Pow(x - 35.80, 2) + 100
f_prime = f.diff(x)
f_integrate = sym.integrate(sym.sqrt(1 + sym.Pow(f_prime, 2)), x)
I want to be able to write this integral and solve it with Sympy (so, I want to isolate the variable b).
UPDATE
Here is now my code.
f_integrate = sym.integrate(sym.sqrt(1 + sym.Pow(f_prime, 2))-c, (x, 0, b))
g = 100
for i in range(10, 72):
bi = sym.nsolve(f_integrate.subs(c,i), g)
g = bi
return g
This code work with the initial function I wrote (f(x)= -0.077 * sym.Pow(x - 35.80, 2) + 100), but with pretty much any other function, I got this error:
Traceback (most recent call last):
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 543, in <module>
drawManager(line.copy())
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 199, in drawManager
t = createT(FU, f, x, distanceNextSyllable)
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 107, in createT
print(getPosition(f, x, 0, distanceNextSyllable))
File "Dragon_Ball_-_Film_3_-_Effect - Test avec les bézier.py", line 95, in getPosition
bi = sym.nsolve(f_integrate.subs(c,i), g)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\sympy\utilities\decorator.py", line 88, in func_wrapper
return func(*args, **kwargs)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\sympy\solvers\solvers.py", line 2937, in nsolve
x = sympify(findroot(f, x0, **kwargs))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\mpmath\calculus\optimization.py", line 985, in findroot
raise ValueError('Could not find root within given tolerance. '
ValueError: Could not find root within given tolerance. (5.9604644775390625e-8 > 2.16840434497100886801e-19)
Try another starting point or tweak arguments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sympy 解决方案似乎是正确的,至少 wolframalpha.com 给出了相同的积分结果。
The sympy solution seems to be correct, at least wolframalpha.com gives the same result for the integral.
看起来您正在使用旧版本的 SymPy,它允许编写
integrate(Eq(x, y), x)
。现在执行此操作的方法是编写integrate(x - y, x)
。对方程执行此操作,并使用nsolve
求解b
的数值,在提供 c 的值后,如下所示:如果您有 c 的范围要测试的值,请使用给定 c 的最后一个解决方案作为下一个 ceg 的猜测,以计算 [10, 409] 中 c 的 b
(这在速度相当慢的计算机上大约需要 12 秒。)
It looks like you are using an older version of SymPy that allowed one to write
integrate(Eq(x, y), x)
. The way to do this now is to writeintegrate(x - y, x)
. Doing so for your equation, and usingnsolve
to solve for the numerical value ofb
, after supplying a value for c, looks like this:If you have a range of c values to test, use the last solution for a given c as the guess for the next c. e.g. to calculate b for c in [10, 409]
(This takes about 12 seconds on a fairly slow computer.)