Combine subs and double for Numeric Evaluations

The subs command can be combined with double to evaluate a symbolic expression numerically. Suppose you have the following expressions

syms t
M = (1 - t^2)*exp(-1/2*t^2);
P = (1 - t^2)*sech(t);

and want to see how M and P differ graphically.

One approach is to type the following commands, but the resulting plot does not readily help you identify the curves.

ezplot(M)
hold on
ezplot(P)
hold off

Instead, combine subs, double, and plot to produce a multicolored graph that indicates the difference between M and P.

T = -6:0.05:6;
MT = double(subs(M, t, T));
PT = double(subs(P, t, T));
plot(T, MT, 'b', T, PT, 'r-.')
title(' ')
legend('M','P')
xlabel('t')
grid

Was this topic helpful?