The code should be self-explaning..

function y = f(x) y1 = x.* (x<0); y2 = x.^2 .* (x>=0) .* (x<2); y3 = 4 .* (x>=2); y = y1 + y2 + y3;

Note: The copyright belongs to the blog author and the blog. For the license, please see the linked original source blog.