One of the nicest ways to handle explicit functions in MATLAB is with anonymous functions. Using the syntax
x = @(t)(...), you can replace the ... with function code, and then simply call
x(t) whenever you want your function evaluated.
So, what you can do for your functions is
x = @(t)3/2*sin(2*t);
y = @(t)(-4/sqrt(15)*exp(-1/2*t).*sin(sqrt(15)/2*t)+2*exp(-1/2*t)).*cos(sqrt(15)/2*t);
h = @(t)x(t)+y(t);
Note that in the definition of
y, I used the
.* operator. This is an element-wise vector multiplication operation, so
[a b].*[c d] returns
Then, you can specify your t-vector in any number of ways, say by using
t = 0:.01:20, and then you can simply call
Why do I like anonymous functions so much?
- You can specify your functions up-front, without having to specify your domain first.
- You don't have to worry about row vs. column vectors. Whatever you put in as the argument is what you'll get out.
- They greatly simplify your code and code structure. Calling
plot(t,h(t)) is unambiguous that $h$ is a function of $t$. By contrast,
plot(t,x+y) doesn't give any indication as to the characteristics of the plot. Is it a function? A phase plane? A distribution?