










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The matlab computing environment and its capabilities for numeric computation and visualization. It covers the basics of working with complex numbers, plotting complex-valued functions, and performing vector and matrix operations in matlab. Examples and exercises to familiarize the user with matlab syntax and programming concepts. It is intended to prepare the reader for further labs and assignments in a course that utilizes matlab extensively. Topics such as evaluating complex expressions, plotting complex-valued functions, accessing and manipulating vectors and matrices, and creating custom matlab functions. It aims to remove some of the initial hurdles in learning matlab and enable the reader to effectively use it for signal processing, communication, and control tasks.
Typology: Cheat Sheet
1 / 18
This page cannot be seen from the preview
Don't miss anything!











In this lab, you will be introduced to MATLAB by using it to evaluate expressions containing complex numbers. Using MATLAB, you can easily express these complex numbers in either rectangular or polar form. MATLAB’s plotting capabilities will be introduced and used extensively. You will also use M-files and create some simple signal processing functions that are used in later laboratory assignments.
By the end of this laboratory assignment, you should be able to:
Review Topics:
As you work through the problems and experiments in this course, you will be using MATLAB - a powerful computing environment for numeric computation and visualization - quite frequently. MATLAB is designed for ease of use and behaves as a high-level programming language that is tailored for signal processing, communication, and control tasks. It is used by professionals in industry and academia worldwide in research, development, and design. An open source program having very similar capabilities and commands is also available called Octave. MATLAB, short for MATrix LABoratory, works on matrices of numbers. We focus mostly on one-dimensional matrices called vectors that contain signal samples, or on multiple-dimensional matrices containing several signals or the parameters of a system. For example, a vector could contain just a list of values from a mathematical function that
you wish to plot. We will first focus on familiarizing you with the matrix notation in MATLAB, and get you used to working with vectors and matrices in arithmetic operations. This introduction has three parts: reading, reading while doing, and doing guided by knowing expected results. Since the main difficulties in learning MATLAB are in learning the syntax and (in some cases) learning to program, we hope that this format will remove some of the trauma. Solutions to the questions in the “Quiz” section should be handed in as part of your Lab 1 report. This introduction is not intended to present you with everything you need to know about MATLAB; it is merely to bring you to a point where you can do the following labs in the course. Use the on-line help, and references for additional information. You should follow this text with MATLAB running, and work through the examples and questions. MATLAB is available on many other platforms. MATLAB runs under Microsoft Windows, Linux X Windows, or Mac OS. All versions of MATLAB are compatible in file storage format and M-file format, so data stored on one system can be transferred to another without loss. Each MATLAB session has at least two windows: a text window, where commands are typed and data is displayed, and a graphics window, where graphics appear. Octave has a virtually identical user interface to MATLAB. If you plan to do work at home on labs and assignments and do not have a license of MATLAB, it is recommended that you repeat some or all of the activities below using Octave on your home computer.
The following sections highlight some useful MATLAB commands by working through some example problems. You should work along with the text.
Find the MATLAB icon on the desktop and double click. Two windows will open. MATLAB has on-line help for all functions and a set of demos; we recommend that you try the demos so you can see some of MATLAB’s computing power and some of the functions available to you. When the text window opens, a prompt appears:
Other variables can be set in the same way:
z = 3+4*j z= 3.0000 + 4.0000i So, to set z to the solution of part b: z = (1-j)^ z= 0.0000 - 32.0000i
Plots in MATLAB are generated using the plot function. plot (x, y) generates a plot where the values of the vector x indicate points along the horizontal axis corresponding to the values in the vector y that are to be plotted on the vertical axis. Vectors x and y must have the same number of elements. Since complex values have two components corresponding to a+jb, MATLAB provides the real and imag functions to separate the real and imaginary parts of an imaginary number:
z = 3+4*j; zr = real(z); zi = imag(z); real and imag break z into real and imaginary parts in the variables zr and zi, respectively. Note that typing a semicolon at the end of the command line prevents MATLAB from echoing the result back to your screen. This will be important when you create large matrices and vectors. In this example, the value of z was not printed as it was above. To plot a complex number, we can either plot the real parts vs. the imaginary parts or let MATLAB do it for us. Here, we supply zr and zi to the plot function: plot (zr,zi,’x’) and MATLAB generates a plot:
2 2. 2 2. 4 2. 6 2. 8 3 3. 2 3. 4 3. 6 3. 8 4 3
plot(z,’x’) since the MATLAB default for plotting complex numbers is to plot the real parts on the horizontal axis and the imaginary parts on the vertical axis. Multiple sets of parameters can be given to plot; each pair of arguments is taken as x and y data pairs. If you wish to have several plots shown at once on different sets of axes, use subplot - see the on-line help - or open new figures by typing figure and creating a whole new plotting window.
with a 2x3 matrix is undefined. Matrix multiplication requires that the number of columns in the first matrix be the same as the number of rows in the second matrix. For example, multiplication of a 2x5 matrix A with a 5x3 matrix B results in a 2x3 matrix C=AB, whereas the multiplication BA is undefined. However, the multiplication D=B’A is defined, where ’ denotes the transpose operation in MATLAB.
Let’s generate values for the complex function f ( t )= 3 ej^3 π t for t ranging from 0 to 1 in 0.001 increments. The first step is to create a time variable; note the use of the : operator with a noninteger step size.
t = 0:0.001:1; Here the semicolon at the end is especially important, unless you really want to see and wait for all 1000 values to be echoed back on your screen. Next, construct a vector containing values of this function for each time value in t: f = 3exp(j3pit); It should be pointed out that transcendental functions (e.g. sin, cos, exp) in MATLAB work on a point-by-point basis; in the above command, the function exp computes a vector where each element is the exponential of its corresponding element in j3pi*t (1001 total elements).
The data in vectors can be viewed and displayed in several different ways: it can be plotted, printed to the screen, printed on paper, and saved electronically. It is not, however, always desirable to access the entire vector at once when displaying the information in it. To access single elements or ranges of a vector, an index element or list that identifies which elements are of interest is needed. Elements in MATLAB vectors are identified by the vector name and an integer number or index, much in the same way that DT signals are indexed by integer values. However, in MATLAB, only positive integer indices are used. Thus the first element in a row or column vector f is denoted by f (1), the second element by f(2), and so forth. To access specific elements in a vector, you need to use the name of the variable and the integer index numbers of the elements vector you wish to access. Range statements can be used for indices to access the indexed elements much in the same way that range statements are used to define vectors comprising values in a specified range. For example,
f(25);
f(3:10); f(1:2:50); The first line accesses the 25th element of f. The second accesses elements 3 through 10, inclusive; and the third statement returns the odd-numbered elements between 1 and 50. Elements in matrices require use of two-dimensional indices for identification and access. For example, f (3,2) returns the element in the third row, second column; ranges can also be used for any index. For example, f (1:3, 4:8) defines a matrix that is equivalent to a section of the matrix f containing the first, second, and third rows, and the fourth through eighth columns. If a : is used by itself, it refers to the entire range of that index. For example, a 3 x 5 matrix could have its fifth column referenced by f ( : , 5), which means “all rows, 5th column only,” as well as f (1:3, 5), which means rows 1 to 3, 5th column only. The index number can be another variable as well. This is useful for creating programming loops that execute the same operations on the elements of a matrix. From other computer programming experiences, you should be familiar with the idea of creating a loop to repeat the same task for different values. Here’s an example of how to do this using MATLAB. Suppose we want to generate an output vector where each element is the sum of the current element and the element from 10 back in an input vector. The task to be repeated is the sum of two elements; we need to repeat this for each element in the vector past 10. The elements of the vector x define the input ramp function to be integrated, y will hold the result, and k is the loop index: x = 3*(0:.1:5) + 2; y=zeros(size(x)); for k=11:length(x)
y(k)=x(k-10) + x(k); end
When a loop structure is entered in MATLAB, the body of the loop does not have a prompt printed; the command line acts the same as with a prompt. Note that the loop will not be executed until the end command is entered and followed by a carriage return. You will wait forever for the loop results if you leave off end (indicating to MATLAB that you have done all you wanted to in the loop). As you can see, the variable k is set to range from 11 to the length of x; this allows k to index all elements in x. k is set to increment by integers. Loops generally are not desirable, since they take a very long time to run. If possible, rewrite your operations in terms of vector additions and multiplications instead of
laboratory session.
2 3
2 q = 3 cos xy + 2 y for x in the range 0 to
x = 0:0.01:5; y = .05x+2.01; p =3sin(x.x)+2cos(y.y.y); q=3cos(x.y) + 2y.y; z = p + q; plot (p); plot (q); plot(z);
A polynomial function has roots at - 2, 2, - 2+3 j , - 2 - 3 j. Determine the polynomial, plot the four roots in the complex plane, and plot the polynomial function for the range x ∈(-5,5) in steps of 0.01. You may wish to use help to look up the functions poly, roots, residue and polyval. Answer: The polynomial is 4 9 16 52 4 3 2 x + x + x − x − Figure 1.4.5 Roots of Problem 3 Figure 1.4.6 Plot of Problem 3
The complex function f(t) has the form: f ( t ) = 3 e − j^2 π^ t +^ π/^4. Plot the real and imaginary parts as a function of time from 0 to 3 seconds in 0.01-second increments. Also plot the magnitude and phase of f as a function of time. You may wish to look up the functions subplot, title, and plot to see how to generate more than one plot in the graphics window at the same time. You will also need abs and angle. These commands are very useful in signals and systems.
Answer: Re( f(t)) : Im( f(t) ): Figure 1.4.7 Real and Imaginary plots Magnitude of f(t) : Phase of f(t) ): Figure 1.4.8 Magnitude and Phase plot
This section will focus on the creation of MATLAB M-files. The solution to a problem is the M-file itself. We have provided you with the output of our M-file (without echoing the generation commands to the screen). This is done by using the command echo off at the beginning of the M-file.
function to indicate to MATLAB that it is a function that may require inputs.
Generate a file that calculates a sine wave of 5 Hz for 3 seconds using 0.001-second increments and plots the sine wave versus time with all axes labeled. Display the length of the time sample and the length of the sine wave calculated. Answer:
sine Length of time vector 3001
Length of sine vector 3001
Generate a function called squarer that returns the vector with each element squared. Use this function with the sine wave from the previous problem and display the sum of the elements in both the sine wave vector and the vector composing square of the sine wave. Answer: the file prob5.m
prob Sum of sine
Note that the sum of a sine squared should be 0.5 if we interpret the sum as an approximation to an integral. The function returns 1500 because there are 1000 samples per period and three periods. 1500 / 3000 is 0.5. You must be cautious when interpreting results because you are dealing with discrete elements in the vector instead of a true continuous function. Note also that if the sum of the sine function accurately approximates the integral, it should be 0. It is, instead, a very small number. This error is due to rounding errors in the least significant place of every sine value calculated and is an artifact of numerical computing. If you expect a sum to be equal to zero, don’t be surprised if it is very small instead of zero. A brief word about help: MATLAB’s help can be useful, but it can also be confusing. If you type help by itself, you will see a list of directories and a description of their contents. help
Submit solutions to the following problems with your lab write-up.
Compute ( )( ) ( 1 7 )( 3 2 )
j j j j
and express your answer in both rectangular and polar coordinates.
An interesting plot is generated by the complex function r = 1 −cos θ, where r is the radius of a complex number z expressed in polar coordinates and θ is the angle. Sweep θ from 0 to 2 π and plot the real and imaginary parts of z = r exp(jθ) as x and y coordinates, respectively.
In communications, a signal is called a power signal if it has a zero time average