Piecewise defined function builder

版本 1.0.0.0 (2.6 KB) 作者: Toby Driscoll
Constructs a callable function that applies different definitions based on supplied conditions.
3.2K 次下载
更新时间 2007/8/21

查看许可证

F = PIECEWISE(COND1,DEFN1,...CONDn,DEFNn,DEFAULT) returns a callable
function F that applies different definitions according to supplied
conditions. For a given X, F(X) will test COND1 and apply DEFN1 if true,
etc. If all conditions fail, then DEFAULT is applied. For any particular
input, the first condition to match is the only one applied.

Each condition should be either a:
* function handle evaluating to logical values, or
* vector [a b] representing membership in the half-open interval [a,b).
Each definition should be either a:
* function handle, or
* scalar value.
The DEFAULT definition is optional; if omitted, it will be set to NaN.

All function definitions can accept multiple input variables, but they
all must accept the same number as in the call to F. They also should
all be vectorized, returning results the same size and shape as their
inputs. Complex inputs will work if the definitions are set up
accordingly; in that case, intervals will be tested using only Re parts.

The special syntax F() displays all the conditions and definitions for F.

Examples:
f = piecewise([-1 1],@(x) 1+cos(pi*x),0); % a "cosine bell"
ezplot(f,[-2 2])
g = piecewise(@(x) sin(x)<0.5,@sin,@(x) 1-sin(x));
ezplot(g,[-2*pi 2*pi])
h = piecewise(@(x,y) (x<0)|(y<0),@(x,y) sin(x-y)); % defined on L
ezsurf(h,[-1 1])
chi = piecewise(@(x,y,z) x.^2+y.^2+z.^2<1,1,0); % characteristic func
[ triplequad(chi,-1,1,-1,1,-1,1), 4/3*pi ]
ans =
4.1888 4.1888

引用格式

Toby Driscoll (2024). Piecewise defined function builder (https://www.mathworks.com/matlabcentral/fileexchange/15851-piecewise-defined-function-builder), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2007a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Spectrum and Signal Analysis 的更多信息
致谢

参考作品: Piecewise functions

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0

Improved documentation on overlapping definitions and on complex inputs.