Using the solve_ivp() to solve the equations of motions

I have written a class that solves a physical system. My solve-method should not return anything. My code for the method is

    def solve(self, y0, T, dt, angle='rad'):
        time_points = np.linspace(0, T, int(T/dt)+1)
        if angle == 'deg':
            y0 = y0[0] * pi / 180, y0[1] * pi / 180
        elif angle != 'rad':
            raise ValueError(
                f"Invalid argument {angle} for 'angles' in 'solve'," +
                " must be 'deg' or 'rad'.")
        sol = solve_ivp(self, (0, T), y0, t_eval=time_points)
        self._t, y = sol.t, sol.y
        self._theta, self._omega = y[0], y[1]

when runing the code with the instance

if __name__ == "__main__":
    pendulum = Pendulum(L=2.7)
    pendulum.solve((1, 1), 50, 0.5)

    plt.plot(pendulum.t, pendulum.y[0])
    plt.show()

I get the following error-message

File “pendulum.py”, line 70, in y return – self.L * cos(self.theta) TypeError: only size-1 arrays can be converted to Python scalars

my code for y is:

    @property
    def y(self):
    """
    Translating angles into cartesian coordinates
    y is an array of vertical positions

    """
    return - self.L * cos(self.theta)

Answer

You say that your solve method should not return anything yet you use result to store the return value. The error is correct What you need to do is change this

    def solve(self, y0, T, dt, angle='rad'):
        time_points = np.linspace(0, T, int(T/dt)+1)
        if angle == 'deg':
            y0 = y0[0] * pi / 180, y0[1] * pi / 180
        elif angle != 'rad':
            raise ValueError(
                f"Invalid argument {angle} for 'angles' in 'solve'," +
                " must be 'deg' or 'rad'.")
        sol = solve_ivp(self, (0, T), y0, t_eval=time_points)
        self.t, y = sol.t, sol.y  # Notice self.t not self._t
        self._theta, self._omega = y[0], y[1]

And

if __name__ == "__main__":
    pendulum = Pendulum(L=2.7)
    pendulum.solve((1, 1), 50, 0.5) #Does not return anything

    plt.plot(pendulum.t, pendulum.u[0])  # We attached the result to instance , we didn't returned it
    plt.show()