5

私はPythonでこの小さなテストプログラムを実行して、どのようsolveに機能するかを確認しnsolveました。

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.14159265358979

solve の最初の呼び出しで、対応する関数の 2 つの解が得られました。しかし、2番目のものではありません。なぜだろう?nsolve初期テスト値で機能するようですが、その値に応じて、異なる数値解が得られます。nsolveたった1行で、または別の関数を使用して、または別の関数を使用して、すべての数値解のリストを取得する方法はありますか?

4

1 に答える 1

6

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

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.

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.

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

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.

于 2012-11-12T14:31:28.810 に答える