# Anonymous function in MATLAB

I want to write the following anonymous function in MATLAB.

I have a piecewise function. I want to turn this into a single expression. How can this be accomplished in MATLAB ?

$$f(x,t) = \begin{cases} 20e^{-4x^2} & \text{if 0 \leq t \leq T/2,} \\ 0 & \text{otherwise.} \end{cases}$$

T = 1.0;

• To enable vectorization with respect to t, you'll need f = @(x,t) (0 <= t & t <= T/2)*20*exp(-4*x.^2) + 0.0;, that is, logical and without short-circuiting (& instead of &&). – cos_theta Nov 10 '20 at 13:24