2013F002/HW4

From CDS 130

Jump to: navigation, search

Homework 4

Due Tuesday 1 October 2013. Name:___________________________________ Section: (9am or 10:15am)

Note - you can use MATLAB to check your answers.

Contents

  1. Accessing MATLAB
  2. Valid Names
  3. Array Syntax I
  4. Array Syntax II
  5. Array Syntax III

1. Accessing MATLAB

To answer these questions, you will need access to MATLAB using any one of the options given at Introduction_To_MATLAB. You don't need to know what the commands mean - this is only a check that you are able to get MATLAB to work.

Hint: To prevent transcription errors, copy and paste these commands on to the MATLAB command line. If you are using MATLAB through the VCL and you are using a Mac, you will need to use CTRL C (command C on Mac) to copy from your browser and control v (command V on Mac) to paste onto the MATLAB command line.

  • Start MATLAB and enter rand(1) on the command line. Write down what you see after you press enter.


  • Enter sprintf('%d ','last name') on the command line except replace last name with your last name. Write down what you see after you press enter. Extra credit: What do the numbers mean?


  • Enter version on the command line. Write down what you see after you press enter.


  • Enter for on the command line, do not press return but instead press the "F1" button (or fn F1 on a Mac) to the upper left of your keyboard. Describe what you see.



Answer:

rand(1)
answer may vary
since this generates a random number,
you'll get a different number everytime you run it
sprintf('%d ','Weigel')
87 101 105 103 101 108
these numbers represent the ASCII code for your last name
87=W 101=e 105=i 103=g 101=e 108=l
version
8.1.0.604 (R2013a)
for
Help window pops up which has an explanation of what
for command does and how it's used

2. Valid Names

Which of the following are valid names for a variable? State the reason for your answer. (See the module on Assignment#General_Syntax_Rules for syntax rules.)

  • c..v
  • c_v
  • one_my
  • 1_my
  • A1^3
  • A1cubed
  • A/B*C


Answer:

c_v
one_my
A1cubed

3. Array Syntax I

See the module on Arrays and Arrays#Questions for help with answering this question. You should be able to answer this question without using MATLAB (on the Mid-term there will be similar questions and you will not have access to MATLAB).

Describe what would happen if you entered the following on the command line:

clear; 
A(1) = 1;
A = [3,4,5,6];
A(1)

Describe what would happen if you entered the following on the command line:

clear;
Q = [10.1,10.2,10.3]; 
Q(3)+Q(2)

Describe what would happen if you entered the following on the command line:

Q(1)=Q(3)+Q(2);

What are the values of the elements of Q after you enter the above set of commands?


Answer 1:

ans = 3; you'll see the first element of array A

Answer 2:

ans = 20.5; you'll see the sum of the third and second elements of array A

Answer 3:

the resulting sum, 20.5, will be assigned to the first element of array A
if you type Q(1), you will now see 20.5, instead of 10.1

4. Array Syntax II

See the module on Arrays and Arrays#Questions for help with answering this question. You should be able to answer this question without using MATLAB (on the Mid-term there will be similar questions and you will not have access to MATLAB).

A.) Describe the problem with the following sets of commands.

clear;
z = 10;
A(0) = z;

B.) Is there a problem with the following sets of commands?

clear;
z = 10;
n=2;
A(n-1) = z

C.) what is the value of A(n-1) in part B.)?


Answer A:

A(0); the index number, number inside the parenthesis, must be greater than 0

Answer B:

No

Answer C:

10. Since n=2, A(n-1) = A(2-1) = A(1); since z=10, A(1)=10

5. Array Syntax III

See the module on Arrays for help with answering this question. You should be able to answer this question without using MATLAB (on the Mid-term there will be similar questions and you will not have access to MATLAB).

Which of the following (circle each one that applies)

  • A = [1:7];
  • A = [1:2:7];
  • A = [1:7:2];
  • A = [1,2,3,4,5,6,7];
  • A = [1,3,5,7];
  • A = [1,3,[5:2:7]];
  • A = [1,7,5,3];

is the same as entering

A(1) = 1;
A(4) = 3;
A(3) = 5;
A(2) = 7;

Answer: A = [1,7,5,3]

Personal tools