Question: How to numerically solve an ODE when the unknown is to be bounded

I want to simulate the behaviour of a mass-spring system under an oscillating acceleration, but I also want that the mass remains between two rigid walls (no bouncing has to be considered).
Here is the notional example I use in the attached file (here x(t) will have to verify x(t) >= 0 and x(t) < 2).

s := t -> 10*cos(t)-x(t):

sys := { 
  diff(v(t), t)=s(t), 
  diff(x(t), t)=v(t), 
  v(0)=0, 
  x(0)=0
}:

When the mass reaches one wall it gets stucked at it until the acceleration allows it to take off from this wall. It can then move to the opposite wall and possibly get stucked at it waiting for appropriate conditions to fly again.
Here are two images to help you understand what happens:

  1. the first one represents the "unbounded" solution of the system above
  2. the second is the "bounded" solution
     

To obtain the right picture I construct the solution piecewise (the method, based on events and the use of discrete variables, is described in the attached file).
For some reasons I would like to build the global solution "in one shot" and no longer have to assemble it from elementary pieces (the blue, red and gray chunks on the right figure).
I've tried to do this through events but  I have failed so far (an ettempt is presented in the attached file).

From_wall_to_wall.mw

Could you show me how to use "events" to handle this problem?

Thanks in advance

Please Wait...