Sympy 在求解方程时返回 ConditionSet 对象,而 TINspire 返回数值

发布于 2025-01-10 14:16:55 字数 2021 浏览 0 评论 0原文

这是我的 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).
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2025-01-17 14:16:55

sympy 解决方案似乎是正确的,至少 wolframalpha.com 给出了相同的积分结果。

The sympy solution seems to be correct, at least wolframalpha.com gives the same result for the integral.

机场等船 2025-01-17 14:16:55

看起来您正在使用旧版本的 SymPy,它允许编写 integrate(Eq(x, y), x)。现在执行此操作的方法是编写integrate(x - y, x)。对方程执行此操作,并使用 nsolve 求解 b 的数值,在提供 c 的值后,如下所示:

>>> from sympy.abc import b, c
>>> fc = integrate(sqrt(1 + f_prime**2)-c, (x,0,b))
>>> nsolve(fc.subs(c, 10), 111)
186.052268133373

如果您有 c 的范围要测试的值,请使用给定 c 的最后一个解决方案作为下一个 ceg 的猜测,以计算 [10, 409] 中 c 的 b

g = 100
for i in range(10, 410):
    bi = nsolve(fc.subs(c,i), g)
    g = bi

(这在速度相当慢的计算机上大约需要 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 write integrate(x - y, x). Doing so for your equation, and using nsolve to solve for the numerical value of b, after supplying a value for c, looks like this:

>>> from sympy.abc import b, c
>>> fc = integrate(sqrt(1 + f_prime**2)-c, (x,0,b))
>>> nsolve(fc.subs(c, 10), 111)
186.052268133373

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]

g = 100
for i in range(10, 410):
    bi = nsolve(fc.subs(c,i), g)
    g = bi

(This takes about 12 seconds on a fairly slow computer.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文