Lab #2 Signal Ploting and Matrix Operatons in Matlab
Lab #2 Signal Ploting and Matrix Operatons in Matlab
EXPERIMENT # 2
The result is shown in Figure 1, where it is clear that the value of N is too small.
12
Signals and Systems Lab
1.2 Grids
13
Signals and Systems Lab
1.4 Multi–plots
1.5 Hold
A call to plot clears the graphics window before plotting the current graph. This is not
convenient if we wish to add further graphics to the figure at some later stage. To stop the
window being cleared:
> plot(x,y,’w-’), hold
> plot(x,y,’gx’), hold off
“hold on” holds the current picture; “hold off” releases it (but does not clear the window, which
can be done with clf). “hold” on its own toggles the hold state.
1.6 Subplot
The graphics window may be split into an m × n array of smaller windows into which
we may plot one or more graphs. The windows are counted 1to mn row–wise, starting from the
top left. Both hold and grid work on the current subplot.
14
Signals and Systems Lab
subplot(221) (or subplot(2,2,1)) specifies that the window should be split into a 2 × 2 array and
we select the first sub-window.
1.7 Zooming
We often need to “zoom in” on some portion of a plot in order to see more detail. This is
easily achieved using the command
>> zoom
Left-Click on plot = Zoom in
Right-Click on plot = Zoom out
Selecting a specific portion of the plot using the mouse will zoom in to the selected portion of the
plot.
Once a plot has been created in the graphics window you may wish to change the
range of x and y values shown on the picture.
> clf, N =100; h =1/N; x =0:h:1;
> y =sin(3*pi*x); plot(x,y)
> axis([-0.5 1.5 -1.2 1.2]), grid
The axis command has four parameters, the first two are the minimum and maximum values of x
to use on the axis and the last two are the minimum and maximum values of y.
Note the square brackets. The result of these commands is shown in Figure 4. For more info,
check out help axis.
15
Signals and Systems Lab
We shall describe two ways in which a meaning may be attributed to the product of two
vectors. In both cases the vectors concerned must have the same length.
The first product is the standard scalar product. Suppose that u and v are two vectors of length n,
u being a row vector and v a column vector:
16
Signals and Systems Lab
The second way of forming the product of two vectors of the same length is known as the
Dot product. It is not often used in Mathematics but frequently used in Signals & Systems. It
involves vectors of the same type. If u and v are two vectors of the same type (both row vectors
or both column vectors), the mathematical definition of this product, is the vector having the
components:
The result is a vector of the same length and type as u and v. Thus, we simply multiply the
corresponding elements of two vectors. In Matlab, the product is computed with the operator .*
and, using the vectors u, v, w, z defined previously,
> u.*w
> u.*v’
Note: a) the use of pi, b) x and sin(pi*x) are both column vectors (the sin function is applied to
each element of the vector). Thus, the dot product of these is also a column vector.
17
Signals and Systems Lab
> a./a
The previous calculation required division by 0 --- notice the Inf, denoting infinity, in the
answer.
> a.*b -24, ans./c
To square each of the elements of a vector we could, for example, do u.*u. However,
a neater way is to use the .^ operator:
> u.^2
> u.*u
> u.^4
> v.^2
> u.*w.^( -2 )
Recall that powers (.^ in this case) are done first, before any other arithmetic operation.
Row and Column vectors are special cases of matrices. An m×n matrix is a rectangular array of
numbers having m rows and n columns. It is usual in a mathematical setting to include the matrix
in either round or square brackets—we shall use square ones. For example, when m = 2, n = 3
we have a 2 × 3 matrix. To enter such an matrix into Matlab we type it in row by row using the
same syntax as for vectors:
>> A =[5 7 9;1 -3 -7]
18
Signals and Systems Lab
> size(ans)
So A is 2×3 and x is 3 ×1 (a column vector). The last command size(ans) shows that the value
returned by size is itself a 1 × 2 matrix (a row vector). We can save the results for use in
subsequent calculations.
Transposing a vector changes it from a row to a column vector and vice versa. The extension of
this idea to matrices is that transposing interchanges rows with the corresponding columns: The
1st row becomes the 1st column, and so on.
> D, D’
Matlab provides a number of useful built–in matrices of any desired size. ones(m,n) gives
an m × n matrix of 1’s,
>> P =ones(2,3)
The second command illustrates how we can construct a matrix based on the size of an existing
one. Try ones(size(D))
19
Signals and Systems Lab
An n×n matrix that has the same number of rows and columns is called a square matrix.
A matrix is said to be symmetric if it is equal to its transpose (i.e. it is unchanged by
transposition):
> S =[2 -1 0; -1 2 -1; 0 -1 2],
> St =S’
> S-St
The n × n identity matrix is a matrix of zeros except for having ones along its leading
diagonal (top left to bottom right). This is called eye(n) in Matlab (since mathematically it is
usually denoted by I).
>> I =eye(3), x = [8; -4; 1], I*x
Notice that multiplying the 3 × 1 vector x by the 3 × 3 identity I has no effect (it is like
multiplying a number by 1).
A diagonal matrix is similar to the identity matrix except that its diagonal entries are not
necessarily equal to 1.
D=
300
040
002
is a 3 × 3 diagonal matrix. To construct this in Matlab, we could either type it in directly
>>D=[-300;040;002]
But this becomes impractical when the dimension is large (e.g. a 100 × 100 diagonal matrix). We
then use the diag function. We first define a vector d, say, containing the values of the diagonal
entries (in order) then diag(d) gives the required matrix.
20
Signals and Systems Lab
On the other hand, if A is any matrix, the command diag(A) extracts its diagonal entries:
> F=[0187;3-2-42;4211]
> diag(F)
> A, B, H =[A; B]
so we have added an extra column (x) to C in order to form G and have stacked A and B on top
of each other to form H.
The command spy(K) will produce a graphical display of the location of the nonzero entries in K
(it will also give a value for nz—the number of nonzero entries):
We may extract sections from a matrix in much the same way as for a vector. Each element of a
matrix is indexed according to which row and column it belongs to. The entry in the ith row and
jth column is denoted mathematically by Ai,j and, in Matlab, by A(i,j). So
> J
> J(1,1)
21
Signals and Systems Lab
> J(2,3)
> J(4,3)
> J(4,5)
Thus, : on its own refers to the entire column or row depending on whether it is the first or the
second index.
The dot product works as for vectors: corresponding elements are multiplied together—so the
matrices involved must have the same size.
> A,
BA=
579
1-3-7
B=
-125 9
05
22
Signals and Systems Lab
> A.*B
> A.*C
> A.*C’
3.9 Matrix–Matrix Products
Exercise
Draw the following:
23
Signals and Systems Lab
3. Draw graphs of the functions for x =0:0.1:10 and label y
4. Plot our graph properly.
i. y = sin(x)/x
2
ii. u = (1/(x-1) )+x
2 2
iii. v = (x +1)/(x -4)
1/3 2 1/2
iv. z = ((10-x) -1)/(4 - x )
Task 1:
Code:
clc;
clear all;
t=0:0.001:4;
x=2*sin(2*pi*t - (pi/2));
plot (t,x,'r-')
title('Sine curve')
xlabel('Time(s)')
ylabel('Y(radians)')
Plot:
Task 2:
Code:
clc;
clear all;
x=0:0.001:2;
y1=cos(x);
y2=x;
plot (x,y1,'g-',x,y2,'r-')
legend('Cos Curve','y=x line')
xlabel('x')
ylabel('y')
grid
Plot:
After zooming in we can see that the curves intersect at x=0.74, y=0.74
Task 4:
Code:
clc;
clear all;
x=0:0.1:10;
subplot(221),plot(x,sin(x)./x ,'r-'),xlabel('x'),ylabel('y')
subplot(222),plot(x,(1./((1-x).^2)+x),'g-'),xlabel('x'),ylabel('y')
subplot(223),plot(x,((x.^2)+1)./((x.^2)-4),'b-'),xlabel('x'),ylabel('y')
y4= ((10 - x).^(1./3) - 1)./(4 - x.^2).^(1./2),xlabel('x'),ylabel('y')
subplot(224),plot(x,real(y4),'b-',x,imag(y4),'r'),xlabel('x'),ylabel('y')
Figure: