在数值计算中我们时常会遇到分段函数(Segment function or piecewise function)。
如何比较优雅地表示分段函数呢?这里分享一下用heaviside function来表达的方法。
它是个不连续函数,其“微分”是狄拉克δ函数。它是一个几乎必然是零的随机变数的累积分布函数。
事实上, x = 0的值在函数应用上并不重要,可以任意取。
这个函数由奥利弗·赫维赛德提出。
MATLAB中,heaviside function表达为
1 2 3 4 5 |
heaviside(x) Description: heaviside(x) returns the value 0 for x 0, and 1/2 for x = 0. |
写成公式即为:
那么假设我们有下面这个分段函数:
那么可以将公式写作:
对上述第一部分做非运算,第二部分做且运算
那么我们可以将写作:
假设,那么我们可以尝试作图验证:
代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
%parameters a=1;b=24;c=12;alpha=-5;beta=6; % variable x = (-10:0.01:10); % segment function f = a + (x.^2-a-b).*heaviside(x-alpha)-... (x.^2-b).*heaviside(x-alpha).*heaviside(x-beta)+... c.*heaviside(x-beta); % plot figure; plot(x,f,'LineWidth',2); xlabel('x','FontSize',18);ylabel('f(x)','FontSize',18); xlim([-10,10]); title('Segment function','FontSize',18); |
得到结果为:
没毛病!heaviside function在控制论以及信号处理中经常会用到,所以用它来表达分段函数还是很有用哒。丢掉你的if...else...吧!
StackExchange:How to rewrite a piecewise function in terms of the Heaviside function
Cmd Markdown 公式指导手册
CMD Markdowns可在线编辑。
List of LaTeX mathematical symbols
Katex渲染问题,公式换行\ 不管用 #224
You must be logged in to post a comment.
Be the first to comment.