For NDSolve
, an InterpolatingFunction
might be sufficient. Here are smooth versions of UnitStep
and UnitBox
, with a smoothing "radius" of epsilon
.
us[epsilon_] :=
Interpolation[{{{-epsilon}, 0, 0}, {{epsilon}, 1, 0}},
"ExtrapolationHandler" -> {If[# < -epsilon, 0, 1] &, "WarningMessage" -> False}];
ub[epsilon_] :=
Interpolation[
{{{-1/2 - epsilon}, 0, 0}, {{-1/2 + epsilon}, 1, 0},
{{1/2 - epsilon}, 1, 0}, {{1/2 + epsilon}, 0, 0}},
"ExtrapolationHandler" -> {0 &, "WarningMessage" -> False}]
Example
sol = NDSolve[{y''[x] + ub[0.1][(x - 6)/6.5] y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 15}];
Plot[y[x] /. First[sol], {x, 0, 15}]
Response to updated question
You could construct an explicit interpolation for your particular use case instead of using the generic functions us
and ub
. For example, for the updated question, one could replace the ± 1/2
by ± 8.5
in ub
, or by arbitrary cutoffs a
, b
, if a general function is needed, as follows.
smoothbox[a_, b_, epsilon_] /; a < b && epsilon > 0 := Interpolation[{
{{a - epsilon}, 0, 0}, {{a + epsilon}, 1, 0},
{{b - epsilon}, 1, 0}, {{b + epsilon}, 0, 0}},
"ExtrapolationHandler" -> {0 &, "WarningMessage" -> False}]
GraphicsRow@{
Plot[smoothbox[-8.5, 8.5, 2.5][x], {x, -15, 15}],
Plot[smoothbox[-8.5, 8.5, 2.5][x], {x, 5, 12}]}
Tanh
? I think the proper parametrization (how steep you make them) needs some information about your problem... $\endgroup$x = -8.5
andx = 8.3
? $\endgroup$