<< Return to class page

Math 151 Lab 04 - 01/29/2018:

DUE: Wednesday, 31, 2018 (by 4:00pm before class starts)

INSTRUCTIONS: Save this Matlab script with the filename LastName_Lab04.m (example: Lewis_Lab04.m). Complete each question using MATLAB code. When you are ready to have your assignment graded, use the PUBLISH command to generate a pdf file, 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.

Contents

#1

WRITE CODE BELOW:

There is a famous algorithm called the "bubble sort" algorithm, which we're going to implement here. It will sort a vector until the entries are all in ascending order.

Step 1: Set x=[1,8,3,6,2,9,7,5,4]

Step 2: Use nested loops (i.e., two for loops) to sort x. The inner loop isn't bad: loop over elements of x from 1 to 8, and if x(j) > x(j+1), then swap them. You may need an extra variable to store one of the values while you swap them.

Step 3: Since you have suppressed the output within the loops, verify that your code sorted x so you have x=[1,2,3,4,5,6,7,8,9] (this means print x to the screen).

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

x=[1,8,3,6,2,9,7,5,4];
for i = 1:length(x)
    for j = 1:(length(x)-1)
        if (x(j)>x(j+1))
            tmp = x(j);
            x(j) = x(j+1);
            x(j+1) = tmp;
        end
    end
end
x
x =

     1     2     3     4     5     6     7     8     9

#2

WRITE CODE BELOW:

Write a function called "collatz" below where you take an input n, and if n is odd, return 3*n+1. If n is even, return n/2. (You need to compute mod(n,2) to test for even/odd. If mod(n,2)==1, n is odd, otherwise n is even.)

Note: You cannot use the keyword function when you publish. To avoid errors, paste your function code below as comments.

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

function ret = collatz(n)
if (mod(n,2)==0)
   ret = n/2;
else
   ret = 3*n+1;
end

#3

WRITE CODE BELOW:

Please make sure your collatz function from the previous problem is correct first.

Step 1: Set up a list of 100 blank counters (i.e., c=zeros(1,100)) and a list of x values x=1:100.

Step 2: Use a while loop inside a for loop to reassign x(i) to the value collatz(x(i)), and every time it reassigns x(i), add 1 to counter c(i). (Hint: Your while loop should run as long as x(i) is not equal to 1, and your for loop should loop over all entries of x). What you are checking is how many times you have to apply the collatz function in order for x to reach 1 given different initial conditions.

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

c=zeros(1,100);
x=1:100;
for i=1:length(x)
    while(x(i)~=1)
        x(i)=collatz(x(i));
        c(i)=c(i)+1;
    end
end
c
c =

  Columns 1 through 13

     0     1     7     2     5     8    16     3    19     6    14     9     9

  Columns 14 through 26

    17    17     4    12    20    20     7     7    15    15    10    23    10

  Columns 27 through 39

   111    18    18    18   106     5    26    13    13    21    21    21    34

  Columns 40 through 52

     8   109     8    29    16    16    16   104    11    24    24    24    11

  Columns 53 through 65

    11   112   112    19    32    19    32    19    19   107   107     6    27

  Columns 66 through 78

    27    27    14    14    14   102    22   115    22    14    22    22    35

  Columns 79 through 91

    35     9    22   110   110     9     9    30    30    17    30    17    92

  Columns 92 through 100

    17    17   105   105    12   118    25    25    25

#4

WRITE CODE BELOW:

Plot your list of counters from #3 as a scatter plot of x (the integers from 1 to 100) vs. c. Instead of using the markers Matlab gave you, please plot the points as red diamonds when you use the scatter or plot commands. Label your vertical and horizontal axes and give the plot a title (These can be whatever you wish. I just want to see that you know how to set them.)

WRITE A COMMENT BELOW:

Does there seem to be any pattern to the number of tries and the x-value or does it seem to be completely random?

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

See https://en.wikipedia.org/wiki/Collatz_conjecture for more information.

scatter(1:100,c,'rd')
xlabel('Integers')
ylabel('Collatz Numbers')
title('Plot of Collatz Numbers')