我用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
或另一个函数在一行中列出所有数值解?在
一般来说,你不能用符号的方式来求解一个方程,显然
solve
就是这样做的。换言之:如果solve
能解出你的方程,那么你就幸运了,典型的技术应用程序没有解析解,也就是说,不能象征性地求解。在因此,后退选项是从一个初始点开始,用数值方法求解方程。在一般情况下,不能保证
nsolve
会找到一个解决方案,即使存在这样的解决方案。在一般来说,没有。不过,您可以从许多初始猜测开始
nsolve
,并跟踪找到的解决方案。您可能希望在感兴趣的时间间隔内均匀地分布初始猜测。这称为多次启动方法。在相关问题 更多 >
编程相关推荐