BLEND uses hyperbolic tangent (tanh) to smoothly blend two functions or smoothly combine a piecewise function into a single function handle.
F = blend(f1,f2,LOC,DIST) returns function handle foo which, when evaluated, will return a smooth blend between functions provided in handles f1 and f2.
The blend is centered around LOC, and most of the smooth blend happens in the region LOC +/- DIST such that F will have value ca. 0.88*f1(x) + 0.12*f2 at LOC - DIST and 0.12*f1(x) + 0.88*f2(x) at LOC + DIST.
For a pure piecewise function, set the blend distance DIST to zero. F will evaluate to NaN at x = LOC. To avoid evaluating to NaN at x = LOC, use eps instead of zero. Default DIST = 0.
Example 1: Make a single function handle for a piecewise function (no smoothing).
foo = blend(@sin, 1, pi/2);
ezplot(foo, [0 pi])
Example 2: Make a smooth function from a discontinuous piecewise function and use vectorization to illustrate the effect of different blending distances.
f1 = @(x) -x;
f2 = @(x) x;
[dist, x] = meshgrid([0 .01 .1 .5 1], -2:.001:4);
foo = blend(f1, f2, 1, dist);
Sky Sartorius (2019). BLEND: utility for smoothly blending functions or creating piecewise functions (https://www.mathworks.com/matlabcentral/fileexchange/56530-blend-utility-for-smoothly-blending-functions-or-creating-piecewise-functions), MATLAB Central File Exchange. Retrieved .
Constant f1 and/or f2; better vectorized; input checking; examples and picture.