3

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.

1 Answers1

3

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
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • Thank you for your help, it realy work! p.s. I use the 4 equation system for x, y pos, velosity and angle(y pos has index 1), but generally it doesnt matter. Thank you for your help again!!! – sabbraxcaddabra May 30 '21 at 18:37