Search code examples
pythonodedifferential-equationsvalueerror

ODES: ValueError: setting an array element with a sequence


I am trying to define the following system of ODES in python: the following system of ODES in python

this way:

def rhs(t, P):
    dP = np.zeros_like(P)
    dP[0] = np.sqrt((1 - 3 / P) * (2 + 4 / P**2))
    dP[1] = 1 / math.pow(P,2)
    return dP

However, I am getting an error:

ValueError: setting an array element with a sequence.

I am not sure what is the problem... Would be grateful for any help with this!


Solution

  • Obviously, your state P has 2 components. Thus also the expressions you compute from P will have two components. Then you try to assign these tuples to a single cell in the array dP, which is not possible and results in that error message.

    You might want to replace P with its first element P[0] in these expressions. Or use

    def rhs(t, u):
        P,phi = u
        dP = ...
        dphi = ...
        return [dP, dphi]