I have to integrate an energy along a path. I know the energy at the "beginning" of the path (energy[0]), and I can determine the energy change (gain and loss) along it (energy'[x]). But the values the energy[x] can have are restricted, since, along the path, the energy is always equal or above a certain value (minimumEnergy[x]). This minimum energy limits changes in an abrut manner, and only influence the values downward the path.
If this would be done in a Euler step manner, it would be easy: starting from the beginning of the path, at each new step energy[x]=Max[energy[x-step]+step*energy'[x-step], minimumEnergy[x]].
How can I put this restrictions on NDSolve?
I've tried to artificially change the energy'[x] equation so that it compensates the difference to have the minimumEnergy[x], but this did not went well. Another problem that I have is that energy'[x] is based on the value of energy[x], and the equation has no real value for energy[x] < minimumEnergy[x].
I've tried to figure out how to implement it with StepMonitor, but came to no conclusion.
The system is slightly complex to be placed here, and so I hope the above explanations are good enough for the understanding of my problem.
The following code will not run. I just placed it here for a better understanding. I already implemented J.M. suggestion on the EventLocator.
ans = energy/. Flatten[NDSolve[
{
energy[outletChannelEnd] == minimumEnergy[flowrate] + bottomFunction[0],
energy'[x] == headLoss[flowrate, height4Head[flowrate, energy[x] - bottomFunction[x]]]
},
energy,
{x, 0, 241},
Method -> {
"EventLocator",
"Event" -> (energy[x] - (minimumEnergy[flowrate] + bottomFunction[x])),
"EventAction" :> (energy[x] = minimumEnergy[flowrate] + bottomFunction[x])
}
]]
The result is as follows, where the blue line is the minimum energy, and the purple is the calculated one:

EDIT:
It correctly detects there is an event on the x=73 (I have asked for a print on the event actions), but it doesn't correct the energy'[x]


Method -> "EventLocator"... – J. M.♦ Feb 8 '12 at 18:11