Lab 1 Part One: Introduction to Matlab Solution

$24.99 $18.99

In this lab, you will work through a series of exercises to get you started in Matlab (“Matrix Laboratory”). You will learn how to use Matlab variables, perform basic operations, and write your own script files. Note: Pre-labs should be done individually, and turned in at the beginning of the lab. Each student will be…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 votes)

In this lab, you will work through a series of exercises to get you started in Matlab (“Matrix Laboratory”). You will learn how to use Matlab variables, perform basic operations, and write your own script files.

Note:

  • Pre-labs should be done individually, and turned in at the beginning of the lab. Each student will be checked off by the TA during the lab section.

  • All in-lab exercises and the lab report should be completed in groups of 2-3.

  • No lab report this week – lab report will be with part 2 next week

What Is Expected From You In Lab 1 Part One

Completion of the Prelab exercise: show to the TA at beginning of lab (2 points) Completion of 2 in-lab check offs with TA (2 points)

PRE-LAB

Read the GettingStarted tutorial and familiarize yourself with the Matlab user interface. This material will be needed to answer the prelab questions and in the lab.

  1. Set up the Remote Desktop Connection. Create a new folder by right-clicking in CURRENT FOLDER window and selecting New Folder. Call this folder Lab 1. Double-click the folder Lab 1 and then press “Select Folder”. You will see that your “Current Folder” now shows you are in the Lab 1 directory that you specified. Create a script template for future use: Write your name, lab section, and the date in your EDITOR window, save the file as template.m. Make a screenshot showing the Matlab interface with the specified folder and script template. Exit Matlab and then restart it. Make sure you can find this file again, both through Matlab and using a folder browser outside of Matlab.

  1. While we will be working on paper with continuous-time signals, we need to use discrete-time representations of those signals in lab.

    1. Give an equation defining sampling time TS in terms of sampling frequency FS

    2. How many time samples are in a vector created by the Matlab command t=0:0.5:2 ?

IN-LAB

For the following exercises, refer to the GettingStarted tutorial, specifically Sections Matlab Variables and Basic Operations for Exercise 1 and Section Creating a Signal that is a Function of Time for Exercise 2. Ask your TA if something isn’t clear.

  1. Lab Exercise #1: M-files

We will write code using M-files which have the extension *.m. M-files can be used as scripts that contain a sequence of commands that are executed exactly as if they were manually typed into the Matlab COMMAND window.

    1. Open up a new script by doing the following: Go to Home tab -> New Script

  1. Save your script: Go to Editor tab -> Save. Then, save file as Ex1.m

  1. On the Matlab script window on the upper center window, first copy or type in the following lines of code and comments (which are done in Matlab using the % symbol):

    • FILE: Ex1.m

    • NAME: [FILL IN NAME HERE]

    • DESCRIPTION: Matlab Variables and Basic Operations

    • Clear all variables and close all windows

clear all; close all;

    • PART A

    • PART B

    • PART C

    • PART D

  1. Under PART A:

      1. Write the line of code to create a 3 x 1 column vector called y1 with values 4, 6, and 2. Suppress the output using a semicolon at the end of the line of code.

      2. Write the line of code to a 3 x 3 matrix called z, where the first row has all ones, the second row has values 3, 6, and 9, and the third row has all zeros. Again, suppress the output by using a semicolon at the end of the line of code.

      3. Test your current script by pressing <F5> or by going to Editor -> Run. You should see your two variables y1 and z on your WORKSPACE window.

  1. Under PART B:

      1. Write the line of code to extract the value 6 in vector y1, and store that value in variable c. Display the output so we can see the value of c on the COMMAND window. Therefore, omit the semicolon at the end of the line of code.

      2. Using the colon operator, write the line of code to extract the last two elements in vector y1 and store them in variable c1. Display the output by omitting the semicolon.

      1. Test your script so far by pressing <F5> or by going to Editor -> Run. You should see the variables y1, z, c, and c1 on your WORKPSACE window. You should also see the values of c and c1 displayed on the COMMAND window.

  1. Under PART C:

      1. Write the line of code to access the value 9 in matrix z, and store that value in variable d. Display the output on the COMMAND window.

      2. Using the colon operator, change the values of matrix z so that the last two columns in the 3rd row are -1. Display the output on the COMMAND window.

      3. Test your script so far by pressing <F5> or by going to Editor -> Run. You should see the variables y1, z, c, c1, and d on your workspace window. You should also see the values of c, c1, d, and z displayed on the COMMAND window.

  1. Under PART D:

Developed by Eldridge Alcantara (Spr 2015); modified by Trang Tran & Mari Ostendorf (Aut 2016, 2017) Page 2 of 3Write the line of code to get the number of columns in vector y1, and store that value in variable e. For this part, display the output on the COMMAND window.

  1. Test your script so far by pressing <F5> or by going to Editor -> Run.

CHECKOFF #1: Demonstrate your script for Ex1.m with your Lab TA.

  1. Lab Exercise #2: Defining signals

    1. Open up a new script and save it as Ex2.m

    2. Copy or type in the following for Ex2.m:

      • FILE: Ex2.m

      • NAME: [FILL IN NAME HERE]

      • DESCRIPTION: More Matlab Variables and Basic Operations

      • Clear all variables and close all windows

clear all;

close all;

    • PART A

    • PART B

    • PART C

    • PART D

  1. For PART A: Write the lines of code to define the following two signals

x(t) = 0.5t and y(t) = t2. Both signals will be defined over the range 0 ≤ t ≤ 3 with a sampling frequency of 2.

  1. For PART B: Write the line of code to define a new signal z(t), where z(t) = x(t) – 2y(t).

  1. For PART C: Write the line of code to access z(t) at t = 2, and store the result in variable w1. Display the output on the COMMAND window.

  1. For PART D: Write the lines of code to extract values of z(t) from 0 ≤ t ≤ 1.5, and store the values in variable w2. Display the output on the COMMAND window.

  1. Run and test your script.

CHECKOFF #2: Demonstrate your script for Ex2.m with your Lab TA.

Developed by Eldridge Alcantara (Spr 2015); modified by Trang Tran & Mari Ostendorf (Aut 2016, 2017) Page 3 of 3

Lab 1 Part One: Introduction to Matlab Solution
$24.99 $18.99