Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lab 3 CSE 7, Spring 2017 This lab is an introduction to scripts in the MATLAB program environment. LEARNING OBJECTIVES: 1. 2. 3. 4. Call functions Use zeros() function Explore multi-dimensional matrices in MATLAB Work with subplots REMINDER: Name your file Lab3_LastName. You must complete this Lab#3 assignment individually. You may ask only the course TA and Tutors for assistance. You must complete this assignment without looking at other student’s code or copying solutions from any source. Description: This assignment consists of following a series of instructions and reporting on outcomes. Some questions may ask for the code to achieve a certain result while others will ask for the result of some code. Lab Instructions: Login and set up same environment as Lab#1 (cs7sXX ieng6 home directory, Notepad++, MATLAB). Refer to Lab#1. PART ONE: INTRO TO FUNCTIONS (AND HOW THEY DIFFER FROM SCRIPTS) 1. In this Lab, we will learn how functions work and how to use them in various ways (don’t worry; we will be learning how to write them soon too). Functions encapsulate a specific task in MATLAB – they can combine many instructions into a single line of code (like with scripts) but provide more flexibility in that the user is allowed to call them with certain input to get the desired output (NOTE: not all functions have input/output). 2. In the very first lab assignment, we used built-in MATLAB functions like sqrt() and gcd(). Let’s review how we called them in the interpreter: a. sqrt() took in a number, and returned the squared root of that input as its output: Example: >> sqrt(25) ans = 5 Example (we can save the result into a variable, say ‘x’): >> x = sqrt(25) x = 5 Example (if we provide two parameters, it causes an error) >> sqrt(25, 36) Error using sqrt Too many input arguments. b. gcd() took in two numbers, and returned the greatest common divisor of these numbers: Example: >> gcd(25,10) ans = 5 Example (we can save the result into a variable, say ‘a’): >> a = gcd(25,10) a = 5 Example (if we provide only one parameter, it causes an error): >> gcd(25) Error using gcd (line 27) Not enough input arguments. As you can see here (and already viewed in Lab#1), each function has specific requirements for its input and, if it has output, it can store the returning value(s) into a variable(s) in the workspace for future use. Question#1: Review: Are values stored into variables from left to right, or right to left? Note: These are examples of built-in MATLAB functions, however not all functions have to be “built-in” to MATLAB. Functions that are not built-in are ones created by you, or another person. Question#2: (a) What are 2 other built-in functions we have used so far this quarter? (b) Can you name a function we have used this quarter that was NOT built in? (hint: MURTLE Lab#1) 3. In MATLAB, we are also able to nest functions (use them in each other). Example: >> sqrt(gcd(100,175)) ans = 5 In the line of code above, gcd() was called first with 100 and 175 and the result (25) was used in sqrt(), leading to a final value of 5. Question#3: Give an example of another set of nested functions that gives valid output (no errors). 4. In MATLAB, a function can also have a list of outputs. An example of this would be the size() function (we will be using this function a lot this quarter), which takes in a matrix, and returns two outputs – the height and the width of the matrix (multiple outputs depending on the matrix). Example: >> matrix = [1,2,3;4,5,6;7,8,9;10,11,12] matrix = 1 4 7 10 2 5 8 11 3 6 9 12 >> size(matrix) ans = 4 3 If we wanted to save these values separately into two variables, we can define a list (with square brackets [ ]) and place the two variable names accordingly. Example: >> [rows, cols] = size(matrix) rows = 4 cols = 3 You can use >> doc size or >> help size to read more about how the size function works. Question#4: What happens if we do not use a list to store the outputs of the size() function? For example, if we do: >> output = size(matrix) what would be assigned into the variable output? Question#5: Recall the following line of code from Homework#2: >> num_of_cols = size(img, 2); You used the size() function but there were two inputs and only one output. Now that you know more about functions, compare the size() from Homework#2 with the size() you just learned (i.e. >> [rows, cols] = size(matrix)). Explain how passing different inputs into the same size() function results in different outputs. Use help size if you need clarification. ***Note: the size() function is very unique, and its uses differ based on a wide range of factors. We will be seeing the different ways size() can be used and called throughout the quarter PART TWO: PRACTICE WITH BUILT-IN FUNCTIONS 1. Now that we know how to call these functions, we will explore other built-in ones that are unfamiliar to us. To figure out what functions we can use and how to use them, go to http://www.mathworks.com/ or simply use google to get the functions that you need. This is a very important skill to learn, because someone will rarely tell you exactly what commands to type (except in this class). Instead, they will pose a problem or a question and ask you to solve it. You will practice this in the next couple questions. As always, feel free to ask a TA/tutor for help/clarification. Example: How do you calculate 5 to the power of 3 and store this value into a variable called a? Solution: >> a = power(5,3) Question#6: How do you find a list of all the prime numbers less than or equal to 60, and store the whole list as primes_60? (Hint: If you’re stuck, try >> help primes.) Question#7: How would you find the base 2 logarithm of 32768, i.e., 𝑙𝑜𝑔2 (32768)? Store this value into log_32768. Question#8: How would you find the absolute value of -755, divide that number by 43, and then round this quotient to the nearest integer? Store this value as solution. (Hint: If you use nested functions, this can be done with one line of code.) 2. Now consider a polynomial like: x2. Recall from your algebra classes, that a common question regarding polynomials is: what are its roots? In other words, for what values of x does the polynomial x2 = 0? In this case, it’s pretty easy to see the roots are x = 0. But what about the polynomial: x4 – 10x3 + 35x2 – 50x + 24? It is not so clear in this case what the roots are, but MATLAB has a built-in function, called roots, to solve this for us. In order to use roots, we must input the polynomial in a form that MATLAB will understand. We do this by creating a vector of the coefficients of the polynomial. To illustrate: ax5 + bx4 + cx3 + dx2 + ex + f is represented by the vector [a b c d e f]. Note that the coefficients must be entered for every power of x, up to the largest one. So the polynomial x2, which is the same as 1x2 + 0x + 0, is represented by [1 0 0]. Back to the polynomial above, we can use MATLAB to find the roots using the following lines: >> p = [1 -10 35 -50 24]; >> roots(p) If you run this code, you’ll see that the roots are 1, 2, 3, and 4. Question#9: What are the roots of the polynomial 2x3 + 23x2 – x – 25? PART THREE: ZEROS() FUNCTION 1. A function we will be using numerous times for images will be the zeros() function, which provides a matrix filled with zeros. Type in the interpreter: >> doc zeros or >> help zeros or look it up on Mathworks.com for a more detailed explanation of how this function works. It basically creates a matrix filled completely with zeros and the size of this matrix will be determined by the input parameters used with it. (Note: If only one parameter was used, which we will call n, the resulting matrix will be of size n × n.) Example: >> zeros(4,5) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Example: (Think three dimensional for this one, as in 3 sheets of 4x5) >> zeros(4,5,3) ans(:,:,1) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,2) = 0 0 0 0 0 0 0 0 ans(:,:,3) = 0 0 0 0 0 0 0 0 NOTE: If you have a hard time understanding what a multi-dimensional matrix actually looks like, you are not alone. Here is a visualization that may help you: Example: (Using only 1 parameter) >> zeros(4) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 As you can see, (with the exception of a single input) the number of dimensions in the resulting matrix will be the number of inputs used. 2. As mentioned briefly before, since imshow() (the function we used to display images) just requires a three-dimensional, uint8 matrix as input, try this: >> imshow(uint8(zeros(40,50,3))) NOTE: a uint8 type is used because color values are represented in the range of 0 to 255, and the uint8 type can represent exactly this range. Other types store other ranges of numbers. For example a double type represents numbers between about -1.8×10308 and +1.8×10308. This range is much larger than what we need so uint8 is used instead. Question#10: What did the above line of code show? Does it make sense? (Hint: Think about how images are represented in MATLAB and the concept of RGB color. What color is all zeros?) PART FOUR: PLOTS AND SUBPLOTS 1. We will now work with plots. Like imshow(), there is a function plot() that takes in two equal-sized vectors and displays the plot like below: >> x = [0:10]; >> y = x; >> plot(x,y); 2. Before closing this plot, if we call the title() function, we can title this plot: >> title('Positive slope'); 3. Now, we can also use subplots to display multiple plots into one figure window. a. Subplots allow users to partition the figure into parts. For example, the command >> subplot(3,2,1) Indicates that in a setting of a 3×2 grid, this particular subplot will be placed in the spot number 1 (see below) 1 2 3 4 5 6 So, after using the command subplot(3,2,1), any graph you plot, for example calling >> plot(x,y) from the earlier example would place that plot in spot number 1 in the grid. If you then continued and typed: >> subplot(3,2,2) >> plot(x,y) You would see that in the figure, another plot would be placed at position number 2 according to the 3×2 grid. 4. Make a script called subplots.m. You will be pasting the lines of code from this script into Question#11. Note that this is a 2×2 grid, rather than the 3×2 grid from the example. 5. First, create a variable x that will hold values within a range of your choice. Each value must be 0.2 greater than the previous value. (i.e. 0, 0.2, 0.4, 0.8, etc) a. The first plot will be a negative slope, y = -x. b. The second plot will be the ceiling function, y = ceil(x). Recall the function floor( ) from Homework 2, which rounds a number down to the nearest integer (e.g., floor(1.999) = 1). ceil( ) functions in a similar way but rounds a number up! c. The third plot will be the log function, y = 𝑙𝑜𝑔2 (𝑥)? d. The fourth will be a plot of your choice (maybe a trig function!). 6. Once completed, the plot should look something like this: a. Save your figure as a jpg image: Lab3_subplot.jpg. LAB #3 CHECKOFF CHECKLIST To receive credit for this lab you need to: Write your name and terminal number on the whiteboard when you are finished and ready to get checked off. Show your TA/Tutor your files are in your ieng6 cs7sXX home directory folder. Be prepared to show the TA/Tutor the subplots.m file and the Lab3_subplot.jpg image, and be able to explain what you did. Be prepared to show the TA/Tutor the Notepad++ document with all 10 questions answered. Be able to answer questions about functions, roots and zeros functions, and subplot function. Do not leave until you have seen a TA/Tutor mark your name down in autograder. It is your responsibility to make sure you get credit for each lab! HOMEWORK #3: The Homework#3 assignment is due next week in YOUR lab section at the BEGINNING of Lab. You are free to go, or you are welcome to stay and work on Homework#3 (posted on the website) portion in the remainder of your enrolled lab section. Do not leave until you have received 2 emails (Lab #3 and Homework #2) from autograder.