File Exchange

image thumbnail

BLEND: utility for smoothly blending functions or creating piecewise functions

version (2.3 KB) by Sky Sartorius
Returns a function that smoothly blends, with specified location and distance, between two functions


Updated 21 Apr 2016

View License

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);
plot(x, foo(x))

Cite As

Sky Sartorius (2019). BLEND: utility for smoothly blending functions or creating piecewise functions (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)


Constant f1 and/or f2; better vectorized; input checking; examples and picture.

descr. typo

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux