ECE 381: Laboratory 0

Winter 2006

MATLAB Tutorial Session

January 17


    

Pointer to MATLAB Educational Sites


Attendance is mandatory. There will otherwise be no grading.

This session is intended as a means for you to pick up the bare essentials of the Matlab programming environment and language features that you will need to carry out the assignments. Ideally, you have had prior experience with Matlab --have taken a course on it, or used it before. If you have not, do not be too concerned. MATLAB is not hard to learn provided, of course, you have some programming experience and are willing to give some extra time to it. In this session, we will go over some general features of Matlab and programming using Matlab. The focus is placed on graphing and matrix indexing, which are two areas where Matlab is particularly powerful.

 

Working with complex numbers

>> z = -3 - j*4

>> z_real = real (z)

>> z_imag = imag (z)

>> z_mag = sqrt (z_real^2 + z_imag^2)

>> z_mag = abs (z)

>> z_rad = atan2 (z_imag, z_real)

>> z_rad = angle (z)

>> z_deg = angle (z) * 180/pi

 

Vectors

>> a = [2 3]                       

>> a = [2 3];                       %does not print the input

>> a = [2 3]’                       %transpose of the vector

>> a = [2:5]                        %defines a set of values to ‘a’

>> t = 4

>> w = exp (j * (2 * pi * t))       %try with different values of t like t = .25, .5, .75

>> t = [4:0.1:5]                    %define a vector that starts with 4 and ends in 5 with steps of .1

>> w = exp (j*(2*pi*t))

 

Matrix Operations

 

>> b = [2 3 4 ; 5 6 7]              %creates a 2 x 3 matrix. ‘;’ to separate rows.

>> c = [8 7 6 ; 5 4 3]

>> d = b + c

>> d = [b + c]’

>> d = b * c                        %What happens when you try executing this

>> d = b * c’

>> d = b’ * c

>> d = b.* c                        %Element wise multiplication

>> sin (d)

 Creating an executable file

First, you will need to create the file. The easiest editor on our system is to just use the built in Matlab editor. It will allow you to do some very simple file manipulations. Matlab executable files (called M-files) must have the extension ".m". To run the executable file type

>> load <filename> in the command window.  

To add comments use ‘%’.

 

Graphing

t = [1:.125:10];

w = exp (j * (2 * pi * t));

plot (real (w), imag (w));

xlabel ('Real (w)');

ylabel ('Im (w)');

 

Loops

1. for loop
 
h = 0.1;
x = [0:h:2];                   
y = 0*x;                       %initialize y to 0
y(1) = 1;
size(x)                        %display size of x
 
for i=2:21,                    %begin with i=2 and loop till i=21
        y(i) = y(i-1) + h*(x(i-1)^2 - y(i-1)^2);
end
plot(x,y)
plot(x,y,'go')
plot(x,y,'go',x,y)
 
 

2. while loop

h = 0.001;
x = [0:h:2];
y = 0*x;
y(1) = 1;
i = 1;
size(x)
 
max(size(x))
 
while(i<max(size(x)))
        y(i+1) = y(i) + h*(x(i)-abs(y(i)));
        i = i + 1;                         
end
plot(x,y)
 
if statement
a = 4;
b = 4;
if (a<b) 
    y = -1;
else if (a>b)
    y = 2;
else 
    y = 3
end     
 
 

Try these problems from text.

 B.2 (c,d)

B.3 (c,e,f)


School of Computing and Engineering
University of Missouri - Kansas City

Last updated: January 14, 2005