共解函数与多重解

2024-09-30 18:34:28 发布

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

我用python编写了这个小测试程序,以了解solve和{}是如何工作的。在

from sympy import *

theta = Symbol('theta')
phi = Symbol('phi')

def F(theta,phi):
    return sin(theta)*cos(phi)+cos(phi)**2
def G(phi):
    return ((1 + sqrt(3))*sin(phi) - 4*pi*sin(2*phi)*cos(2*phi))
solution1 = solve(F(pi/2,phi),phi)
solution2 = solve(G(phi),phi)
solution3 = nsolve(G(phi),0)
solution4 = nsolve(G(phi),1)
solution5 = nsolve(G(phi),2)
solution6 = nsolve(G(phi),3)
print solution1, solution2, solution3, solution4, solution5, solution6

我得到这个输出:

^{pr2}$

第一次调用solve给了我相应函数的两个解。但不是第二个。我想知道为什么?nsolve似乎可以使用初始测试值,但根据该值,它给出了不同的数值解。有没有办法用nsolve或另一个函数在一行中列出所有数值解?在


Tags: returndefpisincossymbolphisolve
1条回答
网友
1楼 · 发布于 2024-09-30 18:34:28

The first call of solve gave me two solutions of the corresponding function. But not the second one. I wonder why?

一般来说,你不能用符号的方式来求解一个方程,显然solve就是这样做的。换言之:如果solve能解出你的方程,那么你就幸运了,典型的技术应用程序没有解析解,也就是说,不能象征性地求解。在

因此,后退选项是从一个初始点开始,用数值方法求解方程。在一般情况下,不能保证nsolve会找到一个解决方案,即使存在这样的解决方案。在

Is there a way to get the list all numerical solutions with nsolve or with another function, in just one line?

一般来说,没有。不过,您可以从许多初始猜测开始nsolve,并跟踪找到的解决方案。您可能希望在感兴趣的时间间隔内均匀地分布初始猜测。这称为多次启动方法。在

相关问题 更多 >