<< Return to class page

# Math 151 Lab 01 - 01/17/2018:

DUE: Monday, January 22, 2018 (before class starts)

INSTRUCTIONS: Save this Matlab script with the filename LastName_Lab01.m (example: Lewis_Lab01.m). Complete each question (either in words or with Matlab code). When you are ready to have your assignment graded, choose the PUBLISH command from the file menu, and submit the resulting file to CANVAS.

Any text that you wish me to read needs to be written as a comment (like the questions themselves). Any typing that you would like Matlab to evaluate should not be commented out. If the output of a given code snippet is too lengthy, please suppress the output using a semicolon so I don't have to wade through pages of matrix entries. I will be able to re-run your code on my own if I need to see the output.

## Example

What is the Matlab command to initialize the variable 'x' with the value 9.8?

------------------------------------------------------------------------

```% Type your answer here:
x=9.8
```
```x =

9.8000

```

## This command clears all existing variables

```clear all
```

## #1

What is the Matlab command to create the array x which holds the integers: 2, 5, 8, 11, . . . 89? Write it as Matlab code which will be evaluated when published, not as a comment.

------------------------------------------------------------------------

```x=2:3:89
```
```x =

Columns 1 through 13

2     5     8    11    14    17    20    23    26    29    32    35    38

Columns 14 through 26

41    44    47    50    53    56    59    62    65    68    71    74    77

Columns 27 through 30

80    83    86    89

```

## #2

(Referring to the array from problem 1 above) What would the Matlab command be that zeros out the even-numbered indices (That is, x(2)=0,x(4)=0,x(6)=0,...)? Write it as Matlab code which will be evaluated when published, not as a comment.

------------------------------------------------------------------------

```x(2:2:end)=0
```
```x =

Columns 1 through 13

2     0     8     0    14     0    20     0    26     0    32     0    38

Columns 14 through 26

0    44     0    50     0    56     0    62     0    68     0    74     0

Columns 27 through 30

80     0    86     0

```

## #3

What is the difference in Matlab between typing: x=[1 2 3] and x=[1,2,3] and x=[1;2;3]? What happens if you type a semicolon at the end of the commands (i.e., x=[1 2 3];)? Explain this in words, don't just evaluate that code for me.

------------------------------------------------------------------------

x=[1 2 3] and x=[1,2,3] both produce a row vector with the entries 1, 2, and 3.

x=[1;2;3] produces a column vector with the entries 1, 2, and 3.

Adding a semicolon at the end of an expression suppresses the output, but the expression is evaluated.

## #4

(Referring to question 3) (a) For each of those, what happens if you type x.^2 ?

(b) What happens if you forget the period (e.g., x^2 )? Don't tell me it returns an error. I know that. What was it trying to do, and failing at?

------------------------------------------------------------------------

(a) Using the dot means the power operation will be done componentwise. So each entry of x will be raised to the second power.

(b) Leaving off the period will attempt to matrix multiply x by itself. This will return an error because x is not a square matrix.

## #5

Describe in words what the following commands do.

(a) x=4;2;8;

(b) x=4:2:8;

(c) a=pi:pi:7*pi;

------------------------------------------------------------------------

(a) Assigns the value 4 to the variable x, suppresses the output, and suppresses outputting 2 and 8.

(b) Creates a vector [4,6,8], assigns it to the variable x, and suppresses the output.

(c) Creates a vector [pi,2*pi,3*pi,...,7*pi], assigns it to the variable a, and suppresses the output.

## #6

Describe the output for each of the following Matlab commands.

(aa) rand('seed',0.123456); This ensures that everyone's random numbers will be the same. Please type this before part (a).

(a) A=rand(3,4);

(b) A([1,2],3)=zeros(2,1);

(c) B=sin(A);

(d) C=B+6;

(e) D=2*B';

(f) E=A./2; Is the ./ necessary?

(g) F=sum(A.*A);

------------------------------------------------------------------------

(aa) Seeds the random number generator to return a consistent "random" number

(a) Creates a 3x4 random matrix and assigns it to the variable A

(b) Replaces entries A(1,3) and A(2,3) with zeros.

(c) Computes the sine of each entry in A and assigns the matrix of these values to the variable B

(d) Computes the componentwise addition of entries of B and 6, then stores the result in the variable C

(e) Conjugate transposes the matrix stored in B, then multiplies the result componentwise by 2 and stores the result in D

(f) Divides every entry in A by 2 and stores the result in E. The dot is not necessary because the constant 2 causes MATLAB to assume the operation should be done componentwise.

(g) Does componentwise multiplication of entries in A by themselves (squaring) and then sums the columns of the result and stores this in F.

## #7

Explain what Matlab will do if you type in:

A=rand(3,4);

A(:)

A(4)

------------------------------------------------------------------------

The first line will create a random 3x4 matrix and assign it to A. The second line will vectorize A, or create a column vector out of the ordered columns of A. Finally, the third line will output the entry in the 4th index position of A.

## #8

What is the Matlab command to perform the following:

(a) Given an array x, add 5 to each of its values (do not assign the result to a variable).

(b) Given an array A, remove its first column and assign the result to a new array B. (Note that A should remain the same.)

------------------------------------------------------------------------

(a) x+5

(b) B = A(:,2:end)

## #9

What is the difference between putting a percentage sign before a line of code, and using a semicolon at the end of a line of code? For example, compare % x = 5 to x = 5;.

------------------------------------------------------------------------

The percentage sign tells MATLAB to ignore the line entirely. A semicolon tells MATLAB to run the line, but suppress the output so that the user doesn't see it on the screen.

Looking at the difference between % x = 5 and x = 5;, we can type x after each command to see that x is not defined in the first case and is defined to have the value 5 in the second case.

## #10

What does the Matlab command exp(4) return? You may need to consult the help to look it up!

------------------------------------------------------------------------

The value of the exponential function with input 4.

```exp(4)
```
```ans =

54.5982

```