sabbraxcaddabra
sabbraxcaddabra

Reputation: 43

Scipy solve_ivp events

I try to use events in scipy.integrate.solve_ivp function, but dont understand one thing. From example in docs(https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_ivp.html) i know that i can stop integrate when y position come below 0. It works when i want to track change of sign. But for example i want to stop intagrate when the y-position of my projectile is above 3000 m. I try to add "if condition" in hit_ground function, but it doesnt work.

def hit_ground(t, y):
if y[1] > 3000:
    return y[1]

I try to start integrate with y0 = [0, 0, 500, radians(45)], where 500 is velosity an 45 is angle

This is the error i get after i try to run code with this changes:

TypeError: '<=' not supported between instances of 'NoneType' and 'int'

Without this changes it works correctly.

Upvotes: 3

Views: 1729

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114811

An "event" is defined by the event function being 0. In your case, you want the event to occur when the height is 3000. The height in the cannon ball example is y[0] (not y[1] as in your code), so you want the event to occur when y[0] is 3000, or equivalently, when y[0] - 3000 is 0. Try this:

def hit_max(t, y):
    return y[0] - 3000

Upvotes: 4

Related Questions