Search code examples
pythonfunctiontypeerrornewtons-method

scipy.optimize.newton gives TypeError: can only concatenate tuple (not "int") to tuple


My whole program is right (I've checked at various stages). The highlighted line in this module, however, returns the error:

TypeError: can only concatenate tuple (not "int") to tuple

I don't know why this is happening. funcPsat returns float value. I would appreciate any useful advice!

import scipy.optimize.newton as newton

def Psat(self, T):
    pop= self.getPborder(T)
    boolean=int(pop[0])
    P1=pop[1]
    P2=pop[2]
    if boolean:
        Pmin = min([P1, P2])
        Pmax = max([P1, P2])
        if Pmin > 0.0: 
            Pguess = 0.5*(Pmin+Pmax) 
        else:
            Pguess=0.5*Pmax
        solution = newton(self.funcPsat, Pguess, args=(T))   #error in this line
        return solution
    else:
        return None

Solution

  • I think the problem is that, per the documentation

    args: tuple, optional

    Extra arguments to be used in the function call.

    the args argument should be a tuple.

    Just putting parentheses won't do it; the syntax for tuples is the comma. For example:

    >>> T = 0
    >>> type((T))
    <type 'int'>
    >>> type((T,))
    <type 'tuple'>
    

    Try:

    solution = newton(self.funcPsat, Pguess, args=(T,))
                                                  # ^ note comma