我在python中做了这个小测试程序,以查看 solve 和 nsolve 的工作方式.
I did this little test program in python to see how solve and nsolve work.
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我得到以下输出:
[pi/2, pi] [] 0.0 -0.713274788952698 2.27148961717279 3.14159265358979solve的第一次调用给了我对应函数的两个解.但不是第二个.我想知道为什么? nsolve 似乎可以用于初始测试值,但是根据该值,它可以提供不同的数值解.有没有办法在一行中用 nsolve 或另一个函数来获取所有数值解的列表?
The first call of solve gave me two solutions of the corresponding function. But not the second one. I wonder why? nsolve seems to work with an initial test value, but depending on that value, it gives different numerical solutions. Is there a way to get the list all numerical solutions with nsolve or with another function, in just one line?
推荐答案
第一个调用solve给了我相应功能的两个解决方案.但不是第二个.我想知道为什么吗?
The first call of solve gave me two solutions of the corresponding function. But not the second one. I wonder why?
通常,您不能象征性地求解 ,显然, solve 确实可以做到这一点.换句话说:如果 solve 能解决您的方程式,那么典型的技术应用程序就没有解析解决方案,也就是说,无法用符号表示解决方案.
In general, you cannot solve an equation symbolically and apparently solve does exactly that. In other words: Consider yourself lucky if solve can solve your equation, the typical technical applications don't have analytic solutions, that is, cannot be solved symbolically.
因此,后备选项是数值求解方程,该方程从起始点开始.在一般情况下,即使存在,也不保证 nsolve 会找到解决方案.
So the fall-back option is to solve the equation numerically, which starts from an initial point. In the general case, there is no guarantee that nsolve will find a solution even if exists one.
是否有一种方法可以在一行中使用nsolve或另一个函数来获取所有数值解的列表?
Is there a way to get the list all numerical solutions with nsolve or with another function, in just one line?
通常,不会.不过,您可以从许多初步猜测开始 nsolve ,并跟踪找到的解决方案.您可能希望在感兴趣的时间间隔内均匀分布初始猜测.这称为多次启动方法.
In general, no. Nevertheless, you can start nsolve from a number of initial guesses and keep track of the solutions found. You might want to distribute your initial guesses uniformly in the interval of interest. This is called multi-start method.
更多推荐
Sympy nsolve函数和多种解决方案
发布评论