MATLAB: Matlab ode solver – putting condition in function

ode solverode23s

I use ode23s solver
and the results show that variables oscillate around zero plus and minus.
I want to fix the variables to zero when they first go under zero.
ode23s solver can't use odeset 'NonNegative' option,
so we should have to put some condition in the function.
Please give me help.

Best Answer

The easiest option might be to do this after getting the solution from ode23s. For example
[t, y] = ode23s(..)
idx = find(y<0, 1);
y(idx:end) = 0
This assumes that your ODE is first-order and y is a column vector.
Related Question