MATLAB: Is there a way to add constraints (other than nonnegativity) on ode15s
odeode15s
I am using ode15s to solve a DAE system describing oil-water flow in a pipe. Some of the variables in the solution are physically unrealistic, and I was wondering whether there is a way to add specific constraints?
Thank you.
Best Answer
Only in the sense that you could add an event function to detect the value going out of range, and terminating when it does. If you wanted it to somehow "bounce off" of the boundary then you would have to signal the termination and then call ode15s again with revised conditions and tspan. See for example ballode()
For stiff problems you should always specify the Jacobian or its sparsity pattern via the options structure. Sometimes this can greatly improve performance since the solver estimates it numerically when you don't specify it. Seeodeset for more info.
Try some of the other stiff solvers since they use a slightly cruder tolerance to calculate steps (ode23s,ode23t,ode23tb). Since your ODE function contains a loop the fastest solver is likely to be the one with the fewest function evaluations. You can check that by setting'Stats','on' withodeset and comparing the stats of each solver.
If your equations include some conservation equations then you might have a system ofDifferential Algebraic Equations. If that's the case then you can continue usingode15s but might need to set some options as outlined on the linked page.
1. Choose smaller error tolerances for RelTol and/or AbsTol
2. Make a copy of the solution variables in the subroutine where you supply the time derivatives. Then check the copy for negative values and reset them to small positive values within the forthcoming calculations, e.g.
Best Answer