Message Boards Message Boards

0
|
3606 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Get Non-negative ODE solution?

Is there any Mathematica option equivalent to “Nonnegative” in “odeset” in Matlab?

Here is the documentation about the function I’m talking about:

https://www.mathworks.com/help/matlab/math/nonnegative-ode-solution.html

I think it’s a very useful function and it’d be nice to have something similar in Mathematica.

Thanks in advance!

2 Replies

You may try with WhenEvent. For example, this gives a solution which becomes negative:

NDSolveValue[{y'[x] == Sqrt[Abs[y[x]]], y[1] == 1}, y, {x, -5, 3}]

but we can pick the nonnegative solution with this:

NDSolveValue[{y'[x] == Sqrt[Abs[y[x]]], y[1] == 1,
  WhenEvent[y[x] < 0, y[x] -> 0]},
 y, {x, -5, 1}]
POSTED BY: Gianluca Gorni

Thanks for your answer!

Could this technique be applied to a vector variable?

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract