@tomleslie
Please look to my answer to JohnS, maybe it will enlighten you.
I just have two events.
The first one one [[x(t)=0, v(t)<0], [x(t)=0, v(t)=0]] plays the same role than the event described in the help pages for the bouncing ball test case. Excepted I do not reverse the velocity here, but just force it to be 0 (think not to a boucing ball but o an egg crashing on the floor).
The events are correctly specified: see help pages, example 1 :
dsn := dsolve({diff(y(t),t,t)+y(t)=0,y(0)=0,D(y)(0)=1}, numeric, events=[[[y(t),diff(y(t),t)>0],halt], [[y(t),diff(y(t),t)<0],halt]]);
You write "If I replace your 'events' option with the simpler events=[[x(t)=0, v(t)=0], [x(t)-CMAX, halt]] ..." ... I will not solve correctly the problem (my reply to JohnS).
Why ? Because the event [x(t)=0, v(t)=0] :
- will preclude any initial movement of the mass as it is trivially verified by the initial condition
- ... and even if you use some trick to "pass" this pathological case, the equation dv(t)/dt = f will make the velocity v(t) to evolve and then x(t) to become negative
You can also look also to this more complete mw file
ErrorWithDsolve-2.mw
The acceleration is a see-saw function.
The first pattern ranges from 0 to 4 with maximum amplitude 1.
The second pattern ranges from 4 to 8 with maximum amplitude 2
Given c and k, depending on the value of CMAX :
- x(t) = CMAX before t=2: the solution stops because of the event [x(t)-CMAX, halt]
- x(t=2) < CMAX : the mass moves backward up to its wall position x=0 and stays still until the acceleration acc(t) (t>4) is large enough for the piston to move forward again
Then :
- either it reaches CMAX before the acceleration reverses and forces it to return to its wall position
- either it reaches CMAX and the solution stops because of the event [x(t)-CMAX, halt]