
190 Accelerating MATLAB
®
Performance
However, it would be more convenient to use a one-liner anonymous function.
Unfortunately, MATLAB does not enable dening anonymous functions that use condi-
tionals. Instead, we could use the equivalent functional form:
548
odeFcn = @(t,y) [y(2); -1/m * (k*y(1) + b*y(2) - F*(t > t0))];
Not only does this form enable JIT’ization and avoids the function-call overhead, it is
also arguably simpler and easier to maintain and debug, as well as lending itself rather
easily for vectorization.*
However, as Steve Lord correctly pointed out,
549
multiplication by logical values, as done
here for masking,
†
may ca