Find the root of a cubic function
Asked Answered
K

1

1

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?

Kristynkrock answered 28/1, 2015 at 8:21 Comment(1)
Since the plot is different than expected, i d suggest you check if your math is correct. Can you check a few results of fsolve(func, y) and see if they have the expected value?Jeremiahjeremias
R
7

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!

Retriever answered 28/1, 2015 at 14:44 Comment(1)
@epx, don't forget to accept the answer then. It is far more useful for both Jon as well as for you (you both get reputation points and the question will be marked as "answered", thereby indicating to the broader community it has been solved). Maybe go through some of your other questions too: most of them seem answered.Andie

© 2022 - 2024 — McMap. All rights reserved.