Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

python - Find the root of a cubic function

Here is the thing. I am trying to use fsolve function in Python to find the root of a cubic function. This cubic function has a parameter, deltaW. What I do is change this parameter deltaW from -50 to 50, and find the root of the cubic function at the same time. Below is my script:

from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import numpy as np
import pylab

g = 5.61
gamma = 6.45
kappa = 6.45
J = 6.45
rs = 1.0                            #There are just parameters
m = 5.0*10**(-11)
wm = 2*3.14*23.4

X = []
X1 = []

def func(x):                                #Define the cubic function I  need to solve

        A = 1j*g**2*(kappa + 1j*deltaW)*x*x/(m*wm**2)
        B = J**2 + (1j*deltaW - gamma)*(1j*deltaW + kappa)
        C = A + B
        D = abs(C)*x - J*np.sqrt(2*kappa)*rs
        return D

for deltaW in np.linspace(-50, 50, 1000):
    x0 = fsolve(func, 0.0001)
    X.append(x0)

deltaW = np.linspace(-50, 50, 1000)
plt.plot(deltaW, X)    
plt.show()

When I run this script, I get these two messages:

 RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last five Jacobian evaluations.
  warnings.warn(msg, RuntimeWarning)

/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py:152: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)

I am sorry I do not have enough reputation to put the plot of this script here. My question is why do I get this message and why do my plot look so weird in the left part.

Is it because of my code is wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As in almost all cases of finding roots, a good initial guess is imperative. Sometimes the best initial guess is, in fact, known to be wrong. That is the case here. The behavior of your script, which shows unexpected 'spikes' in the answer, can be looked at more deeply by both plotting up the function, and plotting up the found roots around those spikes (hey, you've got a Python console - this is really easy).
What you find is that the solution returned by the solver is jumping around, even though the function really doesn't look that different. The problem is that your initial guess of 0.0001 lies close to a tiny minimum of the function, and the solver can't figure out how to get out of there. Setting the initial guess to 1.0 (way far away, but on a nice, easy descending portion of the function that will head directly to the root), results instead in: enter image description here

So, three things: 1. solvers need loving care and attention - they are rarely automagic.

  1. Sometimes the 'right' initial guess can be well away from what you know is the right answer, but in such a way that the solver has an easy time of it.

  2. the interactive Python console lets you look quickly at what is going on. Use the power of it!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...