# Computer Laboratory

1

BIOE 1000 Computer Laboratory Exercise 2 (CLE2)

Friday, September 14, 2018

Due F September 21, 2018 (15 pts)

Type your name, assignment & lecture time in a comment statement at the top of all m-files,

i.e. % Name, CLE 2, lecture time

The MATLAB Onramp tutorial provided you with an interactive introduction to the MATLAB

commands that we will be using this semester in the course. I hope that you have taken not just

the time to complete the Onramp tutorial, but have also made an effort to understand the

command syntax and usage. Please refer to the Onramp tutorial information or the MATLAB

help command whenever you have questions about command syntax and usage.

In this computer lab exercise, we will (1) review some frequently used, essential commands; (2)

work through a couple of short MATLAB program examples; and (3) write a MATLAB script to

generate and plot data. As you type each MATLAB command, think about what the command

means and try to predict the output that is echoed back to your screen before hitting enter.

Part 1: Vectors, matrices, and the colon operator

1. Why store data in vectors? 2. Exercise:

a) In the command window, type a % followed by your name, CLE2, and your lecture time.

b) Define two matrices, a and b, that are 3×3. Choose any numbers that you want for

values of the elements.

i. Suppose you want to multiply each element in a by the corresponding element

in b. This mathematical operation is called element-wise multiplication. In

MATLAB, you would write at the command prompt: a.*b This command

can also be written as: times(a,b)

ii. Now try this without the ‘dot’ after the a. What happened? This operation is

called the matrix product of the matrices.

c) Define the following matrices: d=1:6; e=10:15; f=[1 4 9 16 25 36].

Perform the following operations and describe the output. 3*d

d.^2

f./d

d.*e

f.^(1/2)

sqrt(f)

1./sin(e)

1./(2*e+1)

(5) Turn in: Highlight the command window commands/output for this exercise. Print selection

and turn in this printout with your annotations for the output from part c.

Part 2: The plot command

1. Read the excerpt on the plot command from “Understanding Matlab” on pages 3-5 below. 2. Example 1

2

a. Type the commands for Example 1 (p. 4) into an m-file, save the m-file (not to the computer or desktop), and execute the m-file in the command window.

b. Using the formulas, calculate and write out on paper the first 5 values that should be

stored in the t and d vectors.

c. Add commands to your m-file to print the first five values in each vector to see if you are right. If your numbers do not agree, go back and figure out why and correct

your calculations or your m-file.

d. Publish the Example 1 m-file to a pdf document. 3. Example 2

a. Type the commands for Example 2 (p. 4) in an m-file, save the m-file, and execute the m-file in the command window.

b. Using the formulas, manually calculate the 5th value that should be found in each of the three vectors: x, z, and y.

c. Add commands to the m-file to print only the 5th value from each vector – are you right?

d. Publish the Example 2 m-file to a pdf document. 4. Example 3

a. Complete Example 3 (p. 5). b. Save the m-file and publish the Example 3 m-file to a pdf document.

(5) Turn in: Please submit the published m-files for Examples 1-3 and the page with the hand

calculations from Parts 2 and 3.

Part 3: MATLAB program for generating and plotting vector data

Following the steps outlined below, create a new program m-file in MATLAB. Save the m-file

and run it in the MATLAB command window. Remember to put your name, assignment, and

lecture time in a comment statement at the top of the m-file. When your program is running as

expected, publish your program to a pdf document.

Write a MATLAB m-file that generates data and plots a sine wave by doing the following tasks

in order:

1. Generate x-axis (independent) data. Use the colon command to set up a row vector X that

has 25 elements ranging linearly in value from 0 to . This task should not require a

calculator. Note: If on your first try to define X, you don’t get it right, study your output

and try again. You aren’t going to break anything by experimenting with commands.

2. Generate y-axis (dependent function) data. Using vector X, produce a row vector Y that

has the shape of the first half-cycle of a sine wave (what is the period of a sine wave?). It

will have values ranging from 0 to 1. Using vector Y, produce a scaled row vector Z that

has the same shape as vector Y, but has values ranging from 0 to 5.

3. Make a plot. Plot vectors Y and Z versus vector X. What are appropriate labels for the

axes? Label the axes. Title the plot with your name.

(5) Turn in: Please submit the published m-file for Part 3.

3