# MATLAB homework(Basic stuff)

Discussion in 'Programmer's Corner' started by wiz0r, Nov 24, 2008.

1. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Ok, so what I need to do is;

1) Given the functions
x1(t)= tu(t)-tu(t-1) and x2(t)=10e^-4tu(t), do the following:
1. Plot x1(t) and x2(t) using MATLAB.
2. Use MATLAB to find and plot x(t)=x1(t)* x2(t), where * denotes convolution.
3. Find x(t)=x1(t)* x2(t) by hand using Laplace transforms.
4. Plot the result of part 3 in MATLAB and compare it to that found in part 2.​

First of all, I've never used MATLAB. So, I'm looking for help. It would be nice if you could recommend me a good MATLAB tutorial/manual. Or give me some hints to get me started. Or even better, do it for me?! Hehe, anyway, right now I have MATLAB 6.5 installed in my computer. So yeah, I don't even know where to start. So, please, help me get started!

PS: I got no problem on doing part 3 and 4(as long as I get to know how to do part 1 and 2).

So, yeah, any help is greatly appreciated.​

Thank you,
Edwin​

2. ### blazedaces Active Member

Jul 24, 2008
130
0
For a matlab tutorial, search "matlab tutorial" on google.

Here's the big secret to using matlab: "help >> matlab help"

Search everything you need or look it up online. Matlab has such good documentation both with its product and online that you should never have to ask anyone how to do anything.

Good luck,
-blazed

3. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Sounds good, I'll get onto it ASAP. (I didn't know about "help"). And ask questions later, if I got any, anyway.

Thanks,
Edwin

4. ### steveb Senior Member

Jul 3, 2008
2,432
469
You may notice this in the help, but just in case; there is a Matlab function called CONV which will numerically convolve two signals.

5. ### Dave Retired Moderator

Nov 17, 2003
6,960
170
wiz0r,

Firstly, I complied a list of web-based Matlab tutorials from the Internet a while back: http://forum.allaboutcircuits.com/blog.php?b=56

Let me know if there are any broken links. Thanks.

Secondly, your problem. Your two signals x1(t) and x2(t) are defined with respect to t, therefore you first need to define a time vector, t:

Code ( (Unknown Language)):
1. t = 0:0.1:10;
This will create a time vector from 0 to 10 (seconds) at 0.1 (second) intervals - the word "second" is in brackets because I have arbitrarily chosen the time unit to be seconds - it can be any time unit, but it must be consistent.

You now need to define x1(t) and x2(t). For this you need to specify u(t) - you haven't stated what u(t) is in your OP.

Once you have done this you can plot x1(t) and x2(t) with respect to t by:

Code ( (Unknown Language)):
1. plot(t,x1)
2. plot(t,x2)
Lastly, steveb is correct to say that you can use the inbuilt Matlab function conv to calculate x1(t)*x2(t):

Code ( (Unknown Language)):
1. y = conv(x1,x2);
Dave

6. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Oh wow, thanks a lot for your help, Dave. Thing is I was really busy in the last few days studying for my last partial tests. Now that I'm done with them I'll start working on my assigment. Again, thanks a lot everyone, and I'll be sure to keep everything updated as soon as I get to work on the assigment.

Regards,
Edwin

7. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Ok, as to initializing u(t);

In my book it appears that u(t) = 0 if t<0, and u(t) = 1 if t>0.

and that; u(t-1) = 1 if 1<t<infinite, and u(t-1) = 0 if t<1.

So, could I do the following to define u(t)?

Code ( (Unknown Language)):
1.
2. if t<0
3.     u(t) = 0;
4.     if t>0
5.         u(t) = 1;
6.     end;
7. end;
8.
9.
Uhm, obviously that didn't work. I wonder, why? ;<

How else could I define u(t) if it changes depending on time? u(t-1)?

Last edited: Nov 27, 2008
8. ### blazedaces Active Member

Jul 24, 2008
130
0
In what context did you define u(t) in this way? That code alone obviously wouldn't work, because 0 wouldn't be compared to every single value stored in t...

Change your code so it goes through every value of t, perhaps by use a for loop for example?

-blazed

Edit: not to mention that u(t) is not the correct notation for a function in matlab...

9. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Okay, what about if I define two functions, one would be u(t) and the other u(t-1). So, umm I would have another two files named, u, and v. v being u(t-1). So I would call them on my original program. the two function would look like;

u(t) =

Code ( (Unknown Language)):
1. function u = u(t)
2. if t > 0
3.     u = 1;
4. else
5.     u = 0;
6. end
v(t) = u(t-1)

Code ( (Unknown Language)):
1. function v = v(t)
2. if t > 1
3.     v = 1;
4. else
5.     v = 0;
6. end
my original program would look like;

Code ( (Unknown Language)):
1. t = 0:0.1:10;
2. x1 = t*u - t*v;
3. x2 = 10*exp(-4*t)*u;
4. plot(t,x1)
5. plot(t,x2)
6. y = conv(x1,x2);
7. plot(t,y)
8. return;
I really thought that would work, but bleh, it doesn't. Can anyone help me telling me what's wrong?

Edwin

10. ### blazedaces Active Member

Jul 24, 2008
130
0
When you do if t > 0, it only will compare a SINGLE value of t, like the first one perhaps. It WILL NOT compare EVERY value of t.

On another note, shouldn't you be calling u(t) and v(t), not just u and v in your x1 and x2 equations?

-blazed

11. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Oh, how stupid of me. I get it now, thank you blazedaces. I'll try to fix it, but before I do it, a little ago I was doing a little research and I found this;

http://www.seas.upenn.edu/~ese216/handouts/Chpt13LaplaceTransformsMATLAB.pdf

It tells me that u(t) is defined in matlab as function called heaviside(t). That would make things a lot easier for me. But, for some reason it says that heaviside is an undefined function. Do you think that happens because I have a relatively old matlab(6.5)?

12. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Okay, I got some things down.

First of all I found and downloaded the heaviside function(u(t)). So, I think I got everything ready now. I just got to verify if what I did was correct. That's all.

The heavisde function;

Code ( (Unknown Language)):
1. [FONT=Verdana]function y=Heaviside(x)[/FONT]
2. [FONT=Verdana]y=(x>=0);[/FONT]
Why that should work, because;

So, yeah. Now my code would look like;

Code ( (Unknown Language)):
1. syms t
2. t = 0:0.1:2;
3. x1 = t.*Heaviside(t) - t.*Heaviside(t-1);
4. x2 = 10*exp(-4*t).*Heaviside(t);
5. plot(t,x1)
6. plot(t,x2)
7. y = conv(x1,x2);
8. plot(t,[x1; x2]')
9. return;
Also, I'll attach a graph of the conv, and the two functions. Can anyone check if everything is alright?

Thank you very much,
Edwin

File size:
26.7 KB
Views:
35
File size:
27.5 KB
Views:
34
• ###### x2.JPG
File size:
26.3 KB
Views:
30
Last edited: Nov 28, 2008
13. ### blazedaces Active Member

Jul 24, 2008
130
0
First of all, heaviside is defined in matlab to be:

heaviside(x) has the value 0 for x < 0, 1 for x > 0, and 0.5 for x = 0. heaviside(sym(0)) = 1/2. (SOURCE)

So in my opinion originally that would not be a good u(t). But your function is...

Yeah, I looked up that code when you first mentioned it. But I wanted you to figure things out on your own, not to mention that code-wise it would be doing the same thing this way or your way.

Anyways... first of all, you didn't plot the convolution (y)... so I can't tell if that's correct at the moment.

But something is bugging me. Why is the x1 plot going up and then going back down at 0.9 and not at 1? Theoretically it should rise until 1 and at t = 1 go to 0...

-blazed

14. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Well, first of all I just want to say that your help is really appreciated, blazed. Second, about x1, I don't know. I look onto it and see what I can find.

About conv, you are right, I'm just plotting the two graphs together. What happens is that when I try to plot(t,y) I get an error; "Vectors must be the same lengths." So, I did some googling and I found that fix that I put in my code, but I suppose the only thing I really did was plot the two graphs together, and did not really conv the two functions. I'll do a little more research when I get back home, and try to fix everything.

Again, any help is appreciated.

Edwin

15. ### blazedaces Active Member

Jul 24, 2008
130
0
You're most welcome good sir.

I think I know the problem, and it's very simple. When you take the convolution of two signals, very often the result is not the same length as the original two signals. Take a look at this java-based convolution demo. Try just using the first two example signals, which is two square pulses. If you notice, the original two have a certain length, and their result, a triangle, has a different length.

So the fact of the matter is, the convolution of these two signals doesn't go from 0 to 2 in t. That's why when you try to plot them, the length of t and y are not the same, therefore matlab produces an error.

You need to create a new t, call it tforconv = 0:0.1:length(y). Understand?

As for figuring out why x1 plots the way it does, try plot(t, Heaviside(t)) so we can see for sure that Heaviside is working as it should... though I can't imagine it isn't. If that doesn't work try plot(t, Heaviside(t)-Heaviside(t-1)).

Good luck,
-blazed

16. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
Yes, the conv worked like a charm! I set the tforconv = 0:0.1:4; and then plotted just fine as you can see in my attachment!

As for heaviside, as you can see in the other two graphs that you told me to plot, for some reason it never touches 0, mm, wonder why, is that supposed to happen? I'll attach them both so you can see them(btw, I plotted them from t = -5:0.1:5; ), and I'll be doing a little research to see how u(t) is really supposed to behave. Again, thank you for all your help, blazed.

Edwin

File size:
25.6 KB
Views:
33
File size:
24.8 KB
Views:
31
File size:
25.1 KB
Views:
26
17. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
I think I found a way around it to fix the problem. I made t be t = 0:.001:2; and look how x1 looks like now(attachment). Much better right? Though, I'm not really sure why, but well, it worked. I also attached a new conv graph, it looks better now, but uhh.. the range is almost 2000, and not 25ish like before, why?

Edwin

File size:
27.4 KB
Views:
34
File size:
28.3 KB
Views:
30
18. ### blazedaces Active Member

Jul 24, 2008
130
0
I have an answer for all of it.. .it's really quite simple:

First of all, I feel like an idiot. Of course we're working with discrete time here, not continuous time, so of course since time was in intervals of 0.1 that at 0.9 it would be 1, and then at 1 it would be 0. AND of course if you decrease the width of each interval the signal would be closer to the real thing...

As for why it gets so much higher... like 2000 ... is because it's increasing exponentially so of course the more points it has to increase before it decreases again, the higher it can reach. You could solve for the answer by doing the convolution yourself and find out exactly how high it should go if you want (and at what point in time it will reach it)...

Either way, it looks solid to me.

-blazed

19. ### wiz0r Thread Starter Active Member

May 2, 2008
64
0
That makes a lot of sense. Woot, thank you for everything, blazed! I'm glad I'm done for now. I'll see you around.

Regards,
~Edwin

20. ### rey0111 New Member

Dec 2, 2008
1
0
which is the final code?