"Matthew " <email@example.com> wrote in message news:firstname.lastname@example.org... > I have a problem with ODEs taking variables below 0 or some limit. This > occurs with multiple things like positions, pressures and volumes. > > For example, a piston head should stop when it hits the end, say its > called xend = 1m. z(3) is the position of the piston, while dz(3) is the > change in position, which is the velocity, va. > > I have a piece of code which says that if the piston head is at xend, then > dz(3) is 0 if its going to push it further past xend. However this code > doesn't work if say z(3) is at 0.99 and then dz(3) is 0.02, in this case > it does not register that it will go beyond xend. The code is listed at > the bottom. > > I have tried making statements like the one below but they don't seem to > work, maybe i have some syntax wrong. The one below is just a simple > version of what would be required for example.
Either use an Events function or set the ODE option NonNegative using ODESET.
If you're just worried about the ODE solver returning values less than 0, the NonNegative option will do what you need. If there are events (like the piston reaching its upper limit, not the lower limit) to which you need to react use an Events function. For one example of how to use an Events function, see the example BALLODE.