gusucode.com > symbolic工具箱matlab源码程序 > symbolic/heaviside.m
function Y = heaviside(X) %HEAVISIDE Step function. % HEAVISIDE(X) is 0 for X < 0 and 1 for X > 0. % The value HEAVISIDE(0) is 0.5 by default. It % can be changed to any value v by the call % sympref('HeavisideAtOrigin', v). % % HEAVISIDE(X) is not a function in the strict sense. % See also DIRAC. % Copyright 1993-2016 The MathWorks, Inc. persistent hAtOrigin; if isa(X,'char') && strcmp(X, 'Clear') % sympref calls heaviside with the argument % 'Clear' when 'HeavisideAtOrigin' is changed hAtOrigin = zeros(0); return; else % support only sym, double, and single if ~(isa(X, 'double') || isa(X, 'single') || isa(X, 'sym')) error(message('symbolic:heaviside:WrongInput')); end end Y = zeros(size(X),'like',X); Y(X > 0) = 1; if any(X(:)==0) if isempty(hAtOrigin) try hAtOrigin = cast(sympref('HeavisideAtOrigin'),'like',X); catch error(message('symbolic:sympref:SymbolicHeavisideAtOrigin')); end end Y(X==0) = hAtOrigin; end Y(isnan(X) | imag(X) ~= 0 ) = NaN;