NDSolve with Piecewise solving BVP gives warning bvdisc
It's a backslide introduced since v9:
and another issue of "DiscontinuityProcessing"
so we at least have 2 workarounds.
One is to turn off the "DiscontinuityProcessing"
:
eq1 = {y'[x] == Piecewise[{{z[x], x < 5}, {y[x], 8 > x > 5}, {2 z[x], x > 8}}]};
eq2 = {z'[x] == Piecewise[{{2 z[x] + 1, x < 5}, {2 y[x] + 1, x > 5}}]};
eqs = {eq1, eq2, y[0] == 1, z[10] == 0};
sol1 = NDSolve[eqs, {y, z}, {x, 0, 10}, Method -> {"DiscontinuityProcessing" -> False}]
But as pointed out by Michael E2 in his answer, this is dangerous.
Another workaround is to transform the Piecewise
into UnitStep
, with the help of the undocumented Simplify`PWToUnitStep
:
eq1 = {y'[x] == Piecewise[{{z[x], x < 5}, {y[x], 8 > x > 5}, {2 z[x], x > 8}}]};
eq2 = {z'[x] == Piecewise[{{2 z[x] + 1, x < 5}, {2 y[x] + 1, x > 5}}]};
eqs = {eq1, eq2, y[0] == 1, z[10] == 0};
sol1 = NDSolve[eqs // Simplify`PWToUnitStep, {y, z}, {x, 0, 10}]
Interesting: Changing the conditions in the Piecewise
function to simple one-sided inequalities allows NDSolve
to work.
eq1 = {y'[x] == Piecewise[{{z[x], x < 5}, {2 z[x], x > 8}, {y[x], x > 5}}]};
eq2 = {z'[x] == Piecewise[{{2 z[x] + 1, x < 5}, {2 y[x] + 1, x > 5}}]};
eqs = {eq1, eq2, y[0] == 1, z[10] == 0};
sol1 = NDSolve[eqs, {y, z}, {x, 0, 10},
Method -> {"Shooting",
"StartingInitialConditions" -> {y[0] == 1, z[0] == 1}}]
You still get a NDSolve::berr
message (scaled boundary error), but that it not surprising in a discontinuous BVP.