I am trying to solve a cubic equation in Python. However I am getting only one root of the equation. Please find the code snippet below.

import numpy as np

from scipy import optimize as op

def my_func(p):

    k = 0.17
    d = 3e-6 
    A = 1.6e-9 
    epsilon = 8.85e-12 
    Vspi = np.sqrt((8*k*np.power(d,3))/(27*epsilon*A)) 

    Vdc = 0.2 * Vspi

    xeq = p
    F = (k*np.power(xeq,3))-(2*k*d*np.power(xeq,2))+(2*k*np.power(d,2)*xeq)-(epsilon*A*np.power(Vdc,2))
    return F

zguess = 0.5e-6

z = op.fsolve(my_func,zguess)


What is the mistake ? I am supposed to obtain three roots.

Regards, Raghu


1 Answer 1


Since you use a numerical solver starting from an initial guess, the algorithm will give you the first solution found. You should run the algorithm three times starting from good guesses for the root in order to find all three roots.

Alternatively, since you are looking for roots of polynomials, why not use directly numpy.roots? This should give you all roots. See this link for details.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.