How Do You Convolve and Plot Functions in MATLAB?

  • Thread starter Thread starter wiz0r
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To convolve and plot functions in MATLAB, define the unit step function u(t) as a vector rather than a function, allowing for element-wise operations. The user attempted to define u(t) and v(t) as separate functions but encountered issues with their implementation in the convolution process. Instead, create a time vector and evaluate u and v at each point to generate corresponding function values. Utilize MATLAB's built-in help commands, particularly "help conv," for guidance on convolution syntax and examples. Properly defining the functions as vectors will enable successful convolution and plotting of the results.
wiz0r
Messages
56
Reaction score
0
Ok, so what I need to do is;


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.

Okay, what I had trouble was defing u(t). Since, according to my book it says that u(t) = 0 if t<0, and u(t) = 1 if t>0. Also that; u(t-1) = 1 if 1<t<infinite, and u(t-1) = 0 if t<1. So,
what I tried to do was to define the two functions, one would be u(t) and the other u(t-1). So, I had two functions(m.files) named, u, and v. v being u(t-1). So, I would use them on my original program. the two function would look like;

u(t) =

Code:
function u = u(t)

if t > 0
    u = 1;
else
    u = 0;
end

v(t) = u(t-1)

Code:
function v = v(t)
if t > 1
    v = 1;
else
    v = 0;
end

my original program would look like;

Code:
t = 0:0.1:10;
x1 = t*u - t*v;
x2 = 10*exp(-4*t)*u;
plot(t,x1)
plot(t,x2)
y = conv(x1,x2);
plot(t,y)
return;

I really thought that would work, but bleh, it doesn't. Can anyone help me telling me what's wrong?

PS: I have no experience programming with MATLAB. I only know very basic C. So, yeah, please bare with me.

Edwin
 
Physics news on Phys.org
You can't define a function as you've done (well, it can be used to return values, but all it'll do is return values, you can't do operations on these, or on symbolic functions in general). What you need to do is define a vector (a.k.a. 1-D array) with x (or, in this case, t) values, and then evaluate the function at each of these points, producing a second vector with your function values.

If you do this for u and v, you'll then be able to convolve them.

As you're just starting out, here's probably the most helpful MATLAB command: help

help conv would bring up the documentation (including the expected inputs, and examples of use) for the conv function.

Also, if you've got nothing in the way of MATLAB orientation / intro from your class, here's the MATLAB documentation website / online help (link goes to the "Getting Started in MATLAB" document):
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/bqr_2pl.html

If you know absolutely nothing about MATLAB, the http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f2-8955.html" chapter will intro the basics of MATLAB entry.

Good luck!
 
Last edited by a moderator:

Similar threads

Replies
6
Views
5K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
6
Views
2K
Replies
7
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Back
Top