# Math 151 Lab 02 - 01/22/2017:

DUE: Wednesday, January 24, 2017 (by 4:00pm before class starts)

## INSTRUCTIONS:

Save this Matlab script with the filename LastName_Lab02.m (example: Lewis_Lab02.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.

Please make sure to leave code uncommented unless I ask for a comment. You do not need to label your steps for each problem, but I should be able to see you do each step I asked for.

Place your work below the dashed lines.

## #1

WRITE CODE BELOW:

Step 1: Create a vector x of length 10.

Step 2: Set the value of x at each index to be equal to the index value using a loop. (i.e. write a loop so x(1)=1, x(2)=2, ...)

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

```x = zeros(1,10);
for i = [1:10]
x(i) = i;
end
```

## #2

WRITE CODE BELOW:

Step 1: Create a vector x of the numbers 1 through 100.

Step 2: Find the sum of all numbers from 1 to 100.

Step 3: Compute 100*(100+1)/2

Note: This formula works for any integer in the place of 100.

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

```tot = 0;
%note this should use the sum function
%the for loop is just for instructional purposes
for x = [1:100]
tot = tot+x;
end
tot
100*(100+1)/2
```
```tot =

5050

ans =

5050

```

## #3

WRITE CODE BELOW:

Step 1: Set x=[3,9,5,10,1,0,7,9] and c=0.

Step 2: Use a for loop that iterates over x and, if an entry of x is strictly larger than 5, adds 1 to the counter c. Then print the counter to the screen.

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

```x = [3,9,5,10,1,0,7,9];
c = 0;
for i = [1:length(x)]
if x(i)>5
c = c+1;
end
end
c
```
```c =

4

```

## #4

WRITE CODE BELOW:

Step 1: Set x to the vector [1/1, 1/2, 1/3, 1/4, 1/5, ..., 1/50] using a for loop.

Step 2: Compute the sum of these numbers.

Step 3: Repeat steps 1 and 2 but use x as [1/1, 1/2, 1/3, ..., 1/100] and also [1/1, 1/2, 1/3, ..., 1/1000].

WRITE COMMENT BELOW:

Do each of those sums look like they're closing in on a specific value? If so, please guess a value (this will be graded loosely).

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

The values do not seem to converge. Indeed, they will not because this is a finite part of the harmonic series.

```x = zeros(1,50);
for i = [1:length(x)]
x(i) = 1/i;
end
sum(x)

x = zeros(1,100);
for i = [1:length(x)]
x(i) = 1/i;
end
sum(x)

x = zeros(1,1000);
for i = [1:length(x)]
x(i) = 1/i;
end
sum(x)
```
```ans =

4.4992

ans =

5.1874

ans =

7.4855

```

## #5

The fibonacci sequence is the sequence 1, 1, 2, 3, 5, 8, 13, 21, ... where you can find the next value by 2 = 1 + 1, 3 = 2 + 1, 5 = 3 + 2,...

WRITE CODE BELOW:

Step 1: Initialize a vector x so that it has length 100 and x(1) and x(2) are 1.

Step 2: Use a for loop to fill in the remaining entries in x so that it matches the fibonacci sequence.

Step 3: Create a new vector called y of length 99 and fill it so that it looks like [x(2)/x(1), x(3)/x(2), x(4)/x(3), ..., x(100)/x(99)].

WRITE COMMENT BELOW:

As you read through the values of y, they should be closing in on a specific value. What is this value?

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

The golden ratio: approximately 1.618

```x = zeros(1,100);
x(1) = 1;
x(2) = 1;
for i = [3:length(x)]
x(i) = x(i-1)+x(i-2);
end
x

y = zeros(1,99);
for i = [1:99]
y(i) = x(i+1)/x(i);
end
y
```
```x =

1.0e+20 *

Columns 1 through 7

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 8 through 14

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 15 through 21

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 22 through 28

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 29 through 35

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 36 through 42

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 43 through 49

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 50 through 56

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 57 through 63

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 64 through 70

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

Columns 71 through 77

0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0001

Columns 78 through 84

0.0001    0.0001    0.0002    0.0004    0.0006    0.0010    0.0016

Columns 85 through 91

0.0026    0.0042    0.0068    0.0110    0.0178    0.0288    0.0466

Columns 92 through 98

0.0754    0.1220    0.1974    0.3194    0.5168    0.8362    1.3530

Columns 99 through 100

2.1892    3.5422

y =

Columns 1 through 7

1.0000    2.0000    1.5000    1.6667    1.6000    1.6250    1.6154

Columns 8 through 14

1.6190    1.6176    1.6182    1.6180    1.6181    1.6180    1.6180

Columns 15 through 21

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 22 through 28

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 29 through 35

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 36 through 42

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 43 through 49

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 50 through 56

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 57 through 63

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 64 through 70

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 71 through 77

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 78 through 84

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 85 through 91

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Columns 92 through 98

1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

Column 99

1.6180

```