<< 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 supress 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.

Contents

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.

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

#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.

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

#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.

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

#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?

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

#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;

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

#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);

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

#7

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

Explain what Matlab will do if you type in:

A=rand(3,4);

A(:)

A(4)

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

#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.)

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

#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;.

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

#10

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

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

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