3

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}$$

1 Answers1

4
T = 1.0;
f = @(x,t) (0 <= t & t <= T/2)*20*exp(-4*x.^2) + 0.0;
x = linspace(0,1,100);
plot(x, f(x,T/4), x, f(x,T))
legend('t=T/4','t=T')
cfdlab
  • 3,028
  • 13
  • 19