ECE-S352 Lab 1 MATLAB Tutorial How to learn MATLAB? 1. MATLAB comes with good tutorial and detailed documents. a) Select MATLAB help from the MATLAB Help menu to open the help window. Follow MATLAB s Getting started and learn step by step. b) Use Index tab when you know the MATLAB command name. c) Search can help when you know related keywords and topics. 2. Search in Internet. For example, google MATLAB tutorial or MATLAB help, you will find a lot of useful links to study. Here s one possibility: http://www.math.ufl.edu/help/matlab-tutorial/index.html#sec1 In the following tutorial, all lines started with >> are matlab commands, please follow the tutorial and type them in your MATLAB command window to execute them. Some predefined variables i sqrt(-1) j sqrt(-1) pi 3.1415926535 >>y = 3+4*i y = 3.0000 + 4.0000i There are also a number of predefined functions that can be used when defining a variable. Some common functions that are used often are: abs magnitude of a number ( absolute value for real numbers) angle angle of a complex number, in radians cos cosine function, assumes argument is in radians. sin exp for example >> abs(y) ans = 5.0000 Be careful once you redefine the variable i and j, they will no longer represent sqrt(-1) >> i = 3 >> y = 2+5*i y = 17 If you finish your command line with semi column ;, then MATLAB won t echo the output in the screen. This is very useful when you have variables of large dimensions.
DON T try this to define a 100 by 100 zero matrix, >> X1 = zeros(100,100) It could mess up your MATLAB screen. Instead you should type: >> X1 = zeros(100,100); to suppress the output. Working with vectors and matrices >> a = 4:2:12 a = 4 6 8 10 12 a is a row vector with 5 evenly spaced elements, started with 4 and ended with 12. The default step is 1, so a = 2:1:4 is the same as a = 2:4. >>a = linspace(4, 12, 5) This will create the same row vector as above. >> a1 = -1:0.1:1 Increasing step is 0.1 here. >> help linspace help + <command name> shows help for the specific command. All possible input and output parameters for the command are explained. Some related commands are listed in the end. >> b = a b is transposed a, therefore a column vector. >> b = transpose(a) This equals to b = a. >>A = [1 2 3 4; 5 6 7 8; 9 10 11 12] A = 1 2 3 4 5 6 7 8 9 10 11 12 A is a 3 by 4 matrix. >> B = [1 1 1 1; 2 1 0 0; 3 1 9 0] B = 1 1 1 1 2 1 0 0 3 1 9 0 B is also 3 by 4. >>whos the whos lets you know all of the variables you have in your work space. >>c = A(2,:)
c is the 2 nd row of matrix A. c is a row vector, the colon after comma represents all columns. >> d = B(:, 3) d is the 3 rd column of matrix B, therefore d is a column vector >>E = B(2:3, 1:2) E = 2 1 3 1 E is a sub-matrix of B. Compare E with matrix B to see how they are related. >>F = B(2, 2:2:4) F = 1 0 F is created by taking B s 2 nd row and 2 nd,4 th columns. Remember 2:2:4 is [2, 4] >> M = B(2:end, 3) M = 0 9 end is very useful here. It refers to the last subscript value. Clearly B(end,end) is 0, and B(end,end-1) is 9. >>G = A+B +, -, *, / are defined for matrix operation. Make sure the sizes of 2 matrices match before carrying on the operations. * is the matrix multiplication. For example, A is a 3 by 4 matrix. D is 4 by 5, then A*D will be 3 by 5. If there is a matrix G having 2 rows and 4 columns, A*G will be illegal operation and MATLAB gives a error message. These matrix operations follow the rules in linear algebra. >> X = A.*B Note:.* is different from *..* is an element-wise operation. An entry in X is created from multiplying corresponding entries in A and B, that is: X(i,j) = A(i,j) *B(i,j).* equals to * for scalar variables. >>eye(6) %identity matrix >>zeros(3,4) % zero matrix >>ones(7) % one matrix >> H = [] % this defines a null matrix The above 4 commands create some standard matrices. Everything following the % is comment. MATLAB will just skip instead of interpreting it. Plotting >>h = 0.5;
>> x = 0:h:4; >> y = sin(2*pi*x); >> plot(x,y); This will open a figure window and plot a continuous x-y curve. >>title( an example of sin function ); Add title! >>xlabel( time ); >>ylabel( signal ); >> axis off; >> figure; This command creates a new figure window. >> plot(x,y, r* ); Plot x-y in the new figure window. What is the difference between the 2 figures? >> help plot If you want to plot several figures in one figure window, you should use subplot. >>help subplot >>close all; This command closes all figure windows. >> y1 = sin(3*pi*x +2) + 0.5; >> y2 = cos(1.5*pi*x); >>figure; >>subplot(2,1,1); This tells MATLAB you will treat current figure window as 2 by 1 sub-windows and you are going to plot in the 1 st window. >>plot(x,y, r. ); >>subplot(2,2,3); Suppose the figure has 2 by 2 sub-windows, the 3th sub-window is the left bottom one. >>plot(x,y1, b* ); >>subplot(2,2,4); >> plot(x,y2, gd ); M-files M-files are macros of MATLAB commands that are stored as ordinary files with extension m, that is filename.m. An M-file can be either a function with input and output variables or a list of commands. >>edit Open the edit window. For example, you can have a M-file called mylab1.m with the following lines: t = -3:0.1:3; x = t.^2; plot(t,x); title( x = t^2 function );
This M-file is just a list of commands. Save the file in your working directory and change current MATLAB path to your working directory, or add your working directory to the MATLAB searching path by selecting Set path in the File menu so that MATLAB knows where to find your file. >>mylab1 Now let s see an example of function with input and output. Create a file in your working directory named yplusx.m that contains the following commands: function z = yplusx(y,x) z = y + x; Save the file and in the MATLAB command window, typing following lines: >> x0 = 3; >> y0 = 12; >> h = yplusx(x0,y0) h = 15 Note: the first line in yplusx.m file has to start with keyword function. The name of the file must be the same as the name of the function in order to work properly. LOOPS (for,while) >> x = []; >> for n = 1:10 x = [x n^2]; end >> x is a 1 by 10 vector: [1 4 9 16 25..] Read and save images using functions from image processing toolbox >> I=imread('pout.tif'); >> imshow(i); >> help imread; >> help imwrite; >> I1 = double(i); Most functions in MATLAB deal with only double data type. Since the output data of imread has type of unsigned integer, usually it needs to be converted to double before further processing. >> I2 = uint8(i1); Other useful commands include: imagesc, colormap, imhist etc.
Assignments: 1. Write a matlab m-file functions which generates and plots each of the following signals: a) y ( t) = 10cos(2* pi * f * t + θ ), where 2 t 2 milliseconds, and any f and θ can be chosen. Plot t vs. y(t). Check the relationship between time intervals and frequencies. Check how the signals are shifted due to the phase, θ. b) An exponentially damped sinusoidal signal is defined by x( t) = 20sin(2* pi *1000* t pi / 3) *exp( at) where the exponential parameter a is a variable taking the following set of values: a = 500, 750, 1000. Investigate the effect of varying a on the signal x(t) for 2 t 2 milliseconds. (Hint: use.* instead of * to write the x(t) expression. ) 2. Plot following functions: a) A square wave using the function square. b) A triangular wave using the function sawtooth. c) A growing and decaying exponential using function exp. d) A cosine signal. f) A step function. g) A ramp function. h) An impulse function. Deliverables: The report is to follow the format instructions (see the lab policy document on this web site). In addition to the narrative, include text of the m-files you write as well as sample plots (described above). Make sure graphs are properly labeled. There is no electronic submission for this lab.