SymPy 无法解决简单的三角方程。

huangapple go评论48阅读模式
英文:

Sympy failing to solve simple trig equations

问题

我正在尝试解决一组非线性方程,但出现了一些奇怪的行为。下面,我已将问题简化到对我而言失败的最简单情况。我试图解决的方程在变量 ftheta 中,其中有一个参数 g

  1. f*sin(theta) == 0
  2. f*cos(theta) - g == 0

我的Sympy代码如下,但未能找到任何解决方案。我还在代码中验证了解决方案存在(theta=0f=g)。有两个“技巧”可以使求解器工作:首先,将 g=1(但我希望根据参数来解决)。或者,使用 e2alt 而不是 e2 可以得到一个有用的方程。

from sympy import *

g = symbols('g', Positive=True)
f = symbols('f', Real=True)
theta = symbols('theta', Real=True)

e1 = f*sin(theta) 
e2 = f*cos(theta) - g
e2alt = f*(cos(theta) - g/f) 

equations = [e1, e2]
# equations = [e1, e2alt]  # 使用这个实际上可以工作
variables = [theta, f]
soln = solve(equations, 
              variables, 
              dict=True)

print('方程:')
print(equations)
print('找到的解决方案:')
print(soln)

handSoln = {theta:0, f:g}
print('手动找到的解决方案')
print(handSoln)
print('在手动找到的解决方案处求值')
print([eq.subs(handSoln) for eq in equations])

编辑 - 为了明确,我的问题是:如何让Sympy成功解决这样的方程?我正在处理的实际问题比上述更复杂,我希望能够自动获得解决方案,而无需深入研究每种情况。

英文:

I'm trying to solve a system of nonlinear equations, and am having some weird behavior. Below, I've boiled things down to the simplest case that fails for me. The equations I'm trying to solve are in the variables f and theta, with a parameter g:

  1. f*sin(theta) == 0
  2. f*cos(theta) - g == 0

My Sympy code is below, which fails to find any solution. I verify that a solution exists (theta=0, f=g) in the code as well. There are two 'tricks' I can use to get the solver to work: first, set g=1 (but I'd like to solve in terms of the parameter). Alternatively, using e2alt instead of e2 yields a useful equation.

from sympy import *

g = symbols('g', Positive=True)
f = symbols('f', Real=True)
theta = symbols('theta', Real=True)

e1 = f*sin(theta) 
e2 = f*cos(theta) - g
e2alt = f*(cos(theta) - g/f) 

equations = [e1, e2]
# equations = [e1, e2alt]  # using this actually works
variables = [theta, f]
soln = solve(equations, 
              variables, 
              dict=True)

print('Equations:')
print(equations)
print('Found solutions:')
print(soln)

handSoln = {theta:0, f:g}
print('Hand-found solutions')
print(handSoln)
print('Evaluating at hand-found solutions')
print([eq.subs(handSoln) for eq in equations])

Edit -- for clarity, my question is: how can I get sympy to successfully solve such equations? The actual problems I am working with are more complicated than the above, and I want the solutions automatically, without digging into each case.

答案1

得分: 2

使用小写的假设 real,而不是 Real 等等...,然后用指数形式重写你的方程式:

>>> solve([i.rewrite(exp) for i in equations], variables, dict=True)
[{f: -g, theta: pi}, {f: g, theta: 0}]
英文:

Use lowercase assumptions real, not Real, etc... and then rewrite your equations in terms of exponentials:

>>> solve([i.rewrite(exp) for i in equations], variables, dict=True)
[{f: -g, theta: pi}, {f: g, theta: 0}]

huangapple
  • 本文由 发表于 2023年6月16日 06:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定