How to write piecewise function using anonymous function in Matlab? (closed) Ask Question. The best solution is probably to use an .m file to write a function with an IF to deal with this.. Matlab function input problem, can only see 1 root but supposed to see 2.

Mathematical description. Suppose is equal to on the interval and on the interval and you want to define it as a MATLAB function. Use the unit step function or Heaviside function to define the piecewise function. This is the function in Section 6.3 of Boyce and DiPrima. It is defined by and .Then is 1 where and, so on the interval, and and it is 0 outside the interval.

This is not essential to what you're trying to do if you already have the piecewise function defined for a single period in another way. Once you have your function f defined for one period, taking the periodic extension is as simple as considering f(mod(x,365)) (in Matlab notation).