EENG226 Lab1 PDF
EENG226 Lab1 PDF
LAB EXPERIMENT 1
INTRODUCTION TO MATLAB
1. Objective
ans = -13.2388
Note the semicolon after each variable assignment. If you omit the semicolon, then
MATLAB echoes back on the screen the variable value.
4. Arithmetic Operations
>> X=[1,3,4]
>> Y=[4,5,6]
>> X+Y
ans= 5 8 10
1
For the vectors X and Y the operator + adds the elements of the vectors, one by
one, assuming that the two vectors have the same dimension. In the above example, both
vectors had the dimension 1 × 3, i.e., one row with three columns. An error will occur if
you try to add a 1 × 3 vector to a 3 × 1 vector. The same applies for matrices.
To compute the dot product of two vectors, you can use the multiplication
operator *. For the above example, it is:
>> X*Y’
ans = 43
Note the single quote after Y. The single quote denotes the transpose of a matrix
or a vector. To compute an element by element multiplication of two vectors (or two
arrays), you can use the .* operator:
>> X.*Y
ans = 4 15 24
That is, X.*Y means [1×4, 3×5, 4×6] = [4 15 24]. The ‘.*’ operator is used very
often (and is highly recommended) because it is executed much faster compared to the
code that uses for loops.
5. Complex Numbers
>> z=3 + 4i
>> conj(z) % computes the conjugate of z
>> angle(z) % computes the phase of z
>> real(z) % computes the real part of z
>> imag(z) % computes the imaginary part of z
>> abs(z) % computes the magnitude of z
You can also define the imaginary number with any other variables you like. Try
the following:
>> img=sqrt(-1)
>> z=3+4*img
>> exp(pi*img)
6. Array Indexing
In MATLAB, all arrays (vectors) are indexed starting with 1, i.e., y(1) is the first
element of the array y. Note that the arrays are indexed using parenthesis (.) and not
square brackets [.] as in C/C++. To create an array having as elements the integers 1
through 6, just enter:
2
>> x=[1,2,3,4,5,6]
>> x=1:6
>> x=1:2:6
Ans = 1 3 5
>> x(3:7)
>> length(x) % gives the size of the array or vector
>> x(2:2:length(x))
7. Allocating Memory
You can allocate memory for one-dimensional arrays (vectors) using the zeros
command. The following command allocates memory for a 100-dimensional array:
>> Y=zeros(100,1);
>> Y(30)
ans = 0
Similarly, you can allocate memory for two-dimensional arrays (matrices). The
command
>> Y=zeros(4,5)
defines a 4 X 5 matrix. Similar to the zeros command, you can use the command ones to
define a vector containing all ones,
>> Y=ones(1,5)
ans= 1 1 1 1 1
3
8. Special Characters and Functions
Symbol Meaning
pi π(3.14...)
sqrt indicates square root e.g., sqrt(4)=2
ˆ indicates power(e.g., 3ˆ2=9)
abs Absolute value | .| e.g., abs(-3)=3
NaN Not-a-number, obtained when comparing mathematically undefined operations,
such as 0/0
Inf Represents +∞
; Indicates the end of a row in a matrix. It is also used to suppress
printing on the screen (echo off)
% Denotes a comment. Anything to the right of % is ignored by the
MATLAB interpreter and is considered as comments
’ Denotes transpose of a vector or matrix. It’s also used to define strings,
e.g.,str1=’DSP’;
>> x=1:10;
>> length(x)
ans = 10
The function find returns the indices of the vector X that are non-zero. For example,
I= find(X>100), finds all the indices of X when X is greater than 100. So for the
above
>> find(x> 4)
ans = 5 6 7 8 9 10
9. Plotting
You can plot arrays using MATLAB’s function plot. The function plot(.) is
used to generate line plots. The function stem(.) is used to generate “picket-fence” type
of plots.
>> x=1:20;
4
>> plot(x)
>> stem(x)
More generally, plot(X,Y) plots vector Y versus vector X. Various line types,
plot symbols and colors may be obtained using plot(X,Y,S) where S is a character
string indicating the color of the line, and the type of line (e.g., dashed, solid, dotted,
etc.). Examples for the string S include:
You can insert x-labels, y-labels and title to the plots, using the functions
xlabel(.), ylabel(.) and title(.) respectively. To plot two or more graphs on the
same figure, use the command subplot. For instance, to show the above two plots in the
same figure, type:
The (m,n,p) argument in the subplot command indicates that the figure will be
split in m rows and n columns. The ‘p’ argument takes the values 1, 2 . . . m×n. In the
example above, m = 2, n = 1,and, p = 1 for the top figure and p = 2 for the bottom figure.
**** To get more help on plotting, type: help plot or help subplot.
Assignment: