Sympy nsolve feature and many solutions

I did this little python test program to see how solve and 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 

And I get this output:

 [pi/2, pi] [] 0.0 -0.713274788952698 2.27148961717279 3.14159265358979 

The first call to the solution gave me two solutions to the corresponding function. But not the second. I wonder why? nsolve seems to work with the initial test value, but depending on this value it gives different numerical solutions. Is there a way to get a list of all numerical solutions using nsolve or with another function in only one line?

+4
source share
1 answer

The first call to the solution gave me two solutions to the corresponding function. But not the second. I wonder why?

In general, you cannot solve the equation symbolically and, apparently, solve does just that. In other words: consider yourself happy, if solve can solve your equation, typical technical applications do not have analytical solutions, that is, they cannot be solved symbolically.

Thus, the drop option is a solution to the equation numerically, which starts from the starting point. In general, there is no guarantee that nsolve will find a solution, even if it exists.

Is there a way to get a list of all numerical solutions with nsolve or using another function in only one line?

In general, no. However, you can start nsolve with a few initial guesses and keep track of your solutions. You might want to distribute your initial guesses evenly over the interval of interest to us. This is called multitasking .

+6
source

Source: https://habr.com/ru/post/1445512/


All Articles