MATLAB assignment
ECE 3300 MATLAB Assignment 1 This assignment (1) gives an overview of MATLAB’s basic commands, (2) demonstrates how to represent and use simple discrete-time signals in MATLAB, (3) introduces sampling as a way to represent continuous-time signals, (4) shows how to perform signal transformations and com- binations, (5) demonstrates how to produce a well-formatted signal plot, (6) illustrates how to determine difference/derivative signals and summation/integral signals. Getting Started Go to the Editor tab, choose New and then Script. This starts a new place to work. Then go to Editor, Save to save your work as filename.m, where filename is whatever filename you choose. During this process you can choose a directory to save it in. To run your code at any time, use Editor, Run. The first line of your file is required to be your name in a comment statement, something like % Bellwhether Fumblemuffin if you are unfortunate enough to have that name. Comments always begin with a percent sign (%). The second line of your file is required to be a commented line containing your student ID num- ber, like this: % C12345678 except that you replace the number with your actual ID. Failure to do this will result in a score of zero on the entire assignment. The third line of your file is required to be the name of the MATLAB assignment, also as a comment; in this case, you should have % MATLAB 1. You are strongly encouraged to have your fourth line be clear; clc; close all; because these commands clear all variables, clean your screen, and close any figures, respectively. The reason this line is recommended is that when you rerun your code while making changes, you can then be sure that there are no strange results arising from your previous activities. The semicolons in this command suppress printing of output. You are strongly encouraged to end every line (except for comments) with a semicolon. (When you want to print output you should use special commands discussed later.) The fifth line of your file should be the commented statement % 1, meaning that the code that follows is to be an answer to the first part of the assignment. When you get to code that works on the second part of the assignment, add the line % 2 and so on. These lines are required. Getting Help MATLAB has extensive help files online. Perhaps the easiest way to get help on a particular command is to enter matlab command in a search engine, where command is the name of the command you want to learn more about. Web pages that begin with www.mathworks.com are official MATLAB pages. Getting Finished When you are satisfied that your code has accomplished all that is required for the assignment, go to the Publish tab. Under Edit Publishing Options, set the output format to pdf. Choose your output folder, and then click Publish. The result will include your code and outputs. You must submit your work using Publish in this way or you will receive a zero on the assignment. Note that extra long lines will scroll off the page using Publish. To avoid this, break a long line with the command ... (three periods) and continue it on the next line. 1 Variables and Constants The command a=1 sets the variable a equal to one. The command b=3-a sets the variable b equal to 3-a, which equals 2. Predefined constants in MATLAB include pi (approximately 3.1416) and e (approximately 2.7183). Also predefined are i and j (both √ −1). Lists The command b=[1 2 3 4] produces a list of the numbers 1 through 4 with the value stored in the variable b. (The command b=[1,2,3,4] does the same thing.) Lists can be made of lists; for example, c=[b 5] (or c=[b,5]) is the same as c=[1 2 3 4 5], and d=[b b] is the same as d=[1 2 3 4 1 2 3 4]. The ith element in a list can be accessed by the use of parentheses, and the final element in a list can be referred to via the command end. For the example of d above, d(7) equals 3 and d(end) equals 4, and the command d(7)=8 modifies d to produce d=[1 2 3 4 1 2 8 4]. Multiple elements in a list can be obtained or changed by putting lists inside the parentheses. For example, d(6,8) equals [2 4], and setting d(6,8)=[0 0] modifes d (the version of d that was already modified) to produce d=[1 2 3 4 1 0 8 0]. Ranges The colon (“:”) allows one to easily make a list of equally spaced values. Thus an alternate way to obtain b=[1 2 3 4] is to use b=1:4. To make the separation between adjacent numbers in the list a value other than one, we use the format n:m:k, where n is the starting number, m is the separation between adjacent numbers, and k is the ending number. For example, 1:2:7 is the same as [1 3 5 7]. Ranges can be used absolutely anywhere lists are used. For example, if d=[1 2 3 4 1 2 3 4], d(1:2:7) is equal to [1 3 1 3]. Note that separation between adjacent numbers m can be negative; 5:-1:1 is equal to [5 4 3 2 1]. It follows that d(end:-1:1) reverses the order of the elements in list d. Arithmetic Operators In MATLAB, + is used to add two numbers, - is used to subtract one number from another, * is used to multiply two numbers, and / is used to divide one number by another. Each of these operators can be used between lists and single values. For example, if a=[1 2 3 4], a+2 equals [3 4 5 6], a-2 equals [-1 0 1 2], a*2 equals [2 4 6 8], and a/2 equals [0.5 1 1.5 2]. Putting a dot (period) in front of these commands allows them to perform operations element by element across two lists. For example, if a=[1 2 3 4] and b=[4 3 2 1], a.+b equals [5 5 5 5], a.-b equals [-3 -1 1 3], a.*b equals [4 6 6 4], and a./b equals [0.25 0.667 1.5 4]. To raise a number to a power, ^ should be used. The dot form should be used between two lists as well as between a list and number because MATLAB reserves the power operator in a special way for square matrices. Thus, if a=[1 2 3 4] and b=[4 3 2 1], a.^2 equals [2 4 8 16] and and a.^b equals [1 8 9 4]. Whenever two lists are used with any of these commands, the lists must be the same length. 2 Built-in Functions exp(x), log(x), log10(x), and sqrt(x) compute the exponential ex, the natural logarithm ln(x), the base-10 logarithm log10(x), and the square root √ x, respectively. The trigonometric functions sin(x), cos(x), and tan(x) do exactly what one expects (in radians), as do their inverse functions asin(x), acos(x), and atan(x). Each of these commands can be used on lists; they apply the operation to every element in the list. There is also a four-quadrant version of the inverse tangent, atan2(y,x), that computes the inverse tangent of yx but has inputs y and x entered separately so that it can determine the correct quadrant. Functions Designed Specifically for Lists length(c) produces the length of a list c. max(c), min(c), prod(c), and sum(c) produce the maxinum value, the minimum value, the product of the values, and the sum of the values of list c, respectively. sort(c) sorts the values in increasing order. For example, if d=[1 2 3 4 1 2 3 4], length(d) equals 8, max(d) equals 4, min(d) equals 1, prod(d) equals 576, sum(d) equals 20, and sort(d) produces the list [1 1 2 2 3 3 4 4]. find(c) produces a list of the indices of the nonzero values of c. For example, if d=[1 0 0 4 -3 0 0 1], find(d) gives [1 4 5 8]. Relational Operators In MATLAB, two numbers can be compared via an equality or inequality. If the statement is true, the output is a one; if the statement is false, the output is a zero. The relational operators are < (less="" than),=""> (greater than), <= (less="" than="" or="" equal="" to),="">= (greater than or equal to), == (equal to), and ∼= (not equal to). For example, 2<3 equals="" one,="" 2="">=3 equals zero, and 2∼=3 equals one. These commands can also be used between a list and a number as well as between two equal- length lists. For example, if a=[1 2 3 4] and b=[4 3 2 1], then a<2 equals="" [1="" 0="" 0="" 0]="" and="">2>
=2)=[0 1 1 0], because 2 and 3 are the values of a that satisfy a<=3 and="" a="">=2. Also, (a>3)|(a<2)=[1 0="" 0="" 1]="" because="" 1="" and="" 4="" are="" the="" values="" of="" a="" that="" satisfy="" a="">3 OR a<2. and="" ∼(a="=3)=[1" 1="" 0="" 1]="" because="" 1,="" 2,="" and="" 4="" are="" the="" values="" of="" a="" such="" that="" it="" is="" not="" true="" that="" a="=3." subsets="" of="" lists="" suppose="" a="[5" 4="" 3="" 2="" 1]="" and="" we="" wish="" to="" produce="" the="" subset="" of="" the="" list="" in="" which="" a="" is="" strictly="" greater="" than="" 3.="" note="" that="" a="">3 produces [1 1 0 0 0]. It follows that find(a>3) produces the indices where (a>3) is nonzero; that is, find(a>3)=[1 2]. Because a(1,2)=[5 4], it follows that a(find(a>3))=[5 4]. However, there is an even simpler way to find this subset: it turns out that getting rid of the find command and simply writing a(a>3) produces [5 4] as well. It helps to think of this command as saying “the values of a such that a is greater than three.” The reason this works has to do with MATLAB’s definition of logical variables which we will not discuss in this course. However, this simpler approach should always be used rather than the approach using find because it is easier to read and runs more quickly. 3 Creating a Discrete-Time Signal A discrete-time signal can be implemented in MATLAB as two lists: (1) a list of time values, and (2) a list of the values of the signal at those time values. These lists must be the same length and must be coordinated; that is, the ith signal value must go with the ith time value. Ordinarily, the list of time2.>2)=[1>=3>3>=>