OldGuy
OldGuy

Reputation: 53

Using python 2.7.10 fsolve to find the root of a single variable nonlinear equation given a constant

Sorry, I'm new

I would like find the root of the equation f(x) = exp(x)*erfc(sqrt(x))- (1/(1+y))
where y is a constant passed to the equation.

I have written the following in Python 2.7.10

    1    from scipy.optimize import fsolve
    2    import math
    3    import numpy as np
    4    from numpy import *
    5    from scipy import special
    6    import scipy.optimize
    7
    8    def equation(x,y):
    9       return (np.exp(x) * special.erfc(np.sqrt(x)) - (1/(1+y)))
    10
    11   hd = input ("Input hd ")
    12   guess = (.48) * (hd**(1.71))
    13
    14   print ( "guess soln = ", guess)
    15
    16   soln =  fsolve(equation(x, y=hd), guess)
    17
    18   print ("hd = ")
    19   print ( "Solution = ", soln )

When executed, at the fsolve call at line 16 Python returns: NameError: name x is not defined

If I try line 16 as

    16    soln = fsolve(equation(y=hd), guess)

Python returns: TypeError: equation takes exactly 2 arguments (1 given)

So, I obviously understand neither the proper syntax for passing a constant to a function nor the syntax for getting fsolve to find the root of a single equation given a constant. I would be most grateful is someone would be kind enough to show me the proper way to do this.

Upvotes: 0

Views: 1771

Answers (1)

Julien
Julien

Reputation: 15071

use this instead:

soln =  fsolve(equation, guess, hd)

PS: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.fsolve.html tells you everything you need to know...

Upvotes: 1

Related Questions