# Matlab coding help

I need to graph the following functions on MATLAB:

$$x(t)=\frac{3sin(2t)}{2}\\ y(t)=\frac{-4e^{-1/2t} sin(\frac{\sqrt{15}}{2}t)}{√15}+2e^{-1t/2}cos⁡(\frac{\sqrt{15t}}{2})\\ h(t)=x(t)+y(t)$$

I have the data points for this functions. I need help making a smooth curve on MATLAB.

I try this code and I get and error:

>> x=@(t)(3/2)*sin(2*t);
>> y=@(t)(1/sqrt(15))*exp(-0.5*t)*sin(sqrt(15)*0.5*t)+2*exp(-0.5*t)*cos(sqrt(15)*0.5*t);
>> g=@(t)x(t)+y(t);
>> t=0:0.1:20;
>> plot(t,x(t),y(t),g(t))

-
you mean plot? did you try the help function ? – Dominic Michaelis Feb 19 '13 at 23:07
@DramaFreak: something went really wrong with your formatting and you might want to correct. – Amzoti Feb 19 '13 at 23:07
Ok, you need to do that. So what have you tried, and how has it failed? Do you own a copy of MATLAB? Does it have any documentation? Have you read it? – Chris Eagle Feb 19 '13 at 23:08
One of the symbols you copied in doesn't render (at least not on my computer) - the one right after the sin/cos's. Could you try again, or simply describe it? – gnometorule Feb 19 '13 at 23:08
Your caps lock key is broken. Please replace it. – mrf Feb 19 '13 at 23:09

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 [a*b c*d].

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 plot(t,h(t)).

Why do I like anonymous functions so much?

1. You can specify your functions up-front, without having to specify your domain first.
2. You don't have to worry about row vs. column vectors. Whatever you put in as the argument is what you'll get out.
3. 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,h) or 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?
-
When I try doing it in your method, I get an error that states "Conversion to double from function_handle is not possible." – Username Unknown Feb 20 '13 at 2:07
@DramaFreak Post your code in an edit to your post. – Arkamis Feb 20 '13 at 5:11
@DramaFreak I'm not sure what issue you're having, but if you are doing it right you should have no issues. – Arkamis Feb 20 '13 at 16:51
I get out of work at nine. I will post it then – Username Unknown Feb 20 '13 at 22:54
@dramafreak the error in your code is because you define t before your anonymous functions. Define t after. – Arkamis Feb 21 '13 at 5:59

Several ways of doing this. Easiest would be to define a vector $t$ from say, $[0,\pi]$ (e.g. t = linspace(0,pi,100) for 100 uniformly spaced points between $0$ and $\pi$).

Then create vectors x and y separately. For e.g. x = 1.5*sin(2*t). Use functions sqrt, exp, sin and cos to create the vector y. Don't forget to use .* when multiplying two vectors together component-wise (e.g. exp(-0.5*t).*sin(0.5*sqrt(15)*t)).

Then, plot it with:

plot(t,x+y)

-