Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework 2

GENERAL INSTRUCTIONS: Store your work as a .m file named hw2_<YOUR STUDENT    ID>.m (replace <YOUR STUDENT ID> with your own 9-digit student ID number). Upload this   file to CCLE as your homework submission. Your entire .m file should run without errors. The   script should begin with the command clear and, when it completes, all variables listed at the end of this document should be in the workspace.

Each problem part indicates that a variable is to be stored in memory. When your script is         executed in its entirety, the resulting variables should be the solutions to the corresponding       problem parts. Once you have found the answer to a problem, do not modify that variable in     subsequent lines of code; if needed, create a new version of the variable with a different name.

NOTE: Using semicolons to put multiple commands on one line of text in MATLAB (e.g.,         x=1;x=x+1;) does not count as using a “single line of code” . Any problem that doesnt ask for a single line of code may be solved with any number of lines of code.

1.   Insert the following line of code into your script:

x=2:2:2222;

Then, write a loop using the following starting code:

for i=1:1111

%put your code here

end

Inside of the loop, x(i) refers to the element of x in the i'th position, such that when the loop has finished running, every element of x has had an opportunity to be x(i).

Insert code into the loop to update x(i) on each iteration, transforming the values of x as follows:

a. If x(i) is equal to 44, set x(i) to NaN (1 point)

b. If x(i) is equal to 22, 222, or 2222, set x(i) to 3 (1 point)

c. If x(i) is less than 10, set x(i) to 828 (1 point)

d. If x(i) is a multiple of 4 (i.e., 4, 8, 12, ...) and greater than 828, set x(i) to 999 (1 point)

Use conditionals (if, else, switch). For parts (c) and (d), your code should not hard-code the values of x to be changed, such that if x’s initial value were changed to any other vector (e.g., -1000:1000), the rules listed above would apply properly. For example, the following    would not be a valid answer for (c):

if ismember(x(i),[2 4 6 8])

x(i)=828;

end

HINT: You can use the mod, round, and/or floor functions (and probably others) to check if a number is a multiple of 4.


2.   Create an 8 by 8 numeric (double) matrix of 0’s with a plus sign of 1’s in the middle,          naming the matrix plussign. Note that naming the matrix plus would make MATLAB       upset because there’s already a function named plus, so don’t do that. The matrix will look like this (2 points):

0 0 0 1 1 0 0 0

0 0 0 1 1 0 0 0

0 0 0 1 1 0 0 0

1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1

0 0 0 1 1 0 0 0

0 0 0 1 1 0 0 0

0 0 0 1 1 0 0 0

Do not create the matrix by entering values directly. Instead, start with

plussign=nan (8);

and replace elements of plussign with 0’s and/or 1’s (in sets, not one at a time unless needed) until you achieve the desired output.

3.   Use the following code to create a vector of numbers between 0 and 1:

unordered=sin(1:100)+1;

Write code that creates a vector called ordered which contains the same numbers as        unordered, but rearranged to be in increasing order from lowest to highest (3 points). You may NOT use the sort function or any similar functions. Rather, you must use the     min or max function and one or more loops to repeatedly (1) identify the minimum or  maximum value, (2) put it into a separate vector, and (3) remove it from the                  unordered vector. The variable unordered does not need to remain as-is during this      process (and, indeed, if you follow those steps 1-3, it would not). NOTE: Running                 ordered=sort(unordered) takes you directly to the finish line; while this cannot be your solution, you are welcome to use it to check your work.

4.   Use a for loop and conditionals (if or switch) to creates a Fibonacci matrix, defined as follows. The Fibonacci matrix is constructed by starting with the matrix  [1 1] and              repeatedly concatenating a square matrix whose size in each dimension, and values,         correspond to the size of the longest edge of the current matrix. Matrices are concatenated in counterclockwise order, starting by concatenating a 2 by 2 matrix of 2s to the top of  [1 1]. See below for a Fibonacci matrix with 8 as its largest number:

3 3 3 2 2 8 8 8 8 8 8 8 8

3 3 3 2 2 8 8 8 8 8 8 8 8

3 3 3 1 1 8 8 8 8 8 8 8 8

5 5 5 5 5 8 8 8 8 8 8 8 8

5 5 5 5 5 8 8 8 8 8 8 8 8

5 5 5 5 5 8 8 8 8 8 8 8 8

5 5 5 5 5 8 8 8 8 8 8 8 8

5 5 5 5 5 8 8 8 8 8 8 8 8


The color-coding above is simply a visual aid for demonstrating the structure of the matrix;    you don’t need to color-code your solution (and I don’t know how you would…) Use your      loop to create a Fibonacci matrix with 4181 as its largest value; that is, the final result should be a 6765 (the next Fibonacci number) by 4181 matrix with the number 4181 as its largest    value. Store this matrix as fib4181 (4 points).

HINT: A common question is how do I alternate which side I’m concatenating on?” That’s    what you should use the conditional for. One way to do this is to make a condition that          cycles through the same four values and determine the side of concatenation from those      values. There are many ways to do this; for example, the mod function can be helpful, or you can create an additional counter” variable.

HINT: Feel free to adapt the code to generate Fibonacci numbers from W2P3 slide 14. 4181 is the 19th Fibonacci number, but it is not necessary to calculate it directly in order to solve  this problem.

5.   EXTRA CREDIT: Mr. Mazlo, an investment banker, has embezzled a large sum of money    from Stable Shelters, an American foundation that builds housing for low-income families.    Corporate attorneys Harvey and Mike identify that the amount stolen is exactly                      $152,375,242. 18, but none of Mr. Mazlo’s bank deposits are of exactly $152,375,242.18. To help solve this problem, Harvey and Mike enlist the help of Louis, an expert in financial         crimes. Louis informs them that while no single deposit will be exactly $152,375,242. 18, it is common for embezzlers to split their stolen money into separate deposits, and thus some     combination of deposits will add up to exactly $152,375,242.18. To find this combination,      Harvey, Mike, and Louis enlist your help as a fledgling MATLAB programmer. Your task is to create a vector containing deposits from Mr. Mazlo’s deposit history which sum to                 $152,375,242. 18 . The number of such deposits is 5 or fewer. Name this vector gotcha (4   points).

Professors Note: The context for this problem comes from Season 1, Episode 8 of the USA legal drama “Suits”. Watching the episode is not required to solve the problem.

By a miracle of coincidence, the dollar amounts of Mr. Mazlo’s deposits can be generated by the following line of MATLAB code:

rng(420)

moneys=round(rand(1,100)*40000000,1)+234.97;

Your code for this problem should start with these two lines. To make sure you did it right, check that the first value in the moneys vector is $12,626,071.57. To display all digits of   numbers for this problem, it might be helpful to use

format bank

which displays numbers in their entirety with two digits after the decimal.

Solve this problem by exhaustive search: first, see if any single transaction sums to            $152,375,242. 18, then see if any two transactions sum to $152,375,242. 18, then any three transactions, etc. until you find the solution.

HINT: To get all possible transactions of a given set size (e.g., all pairs of transactions), use the MATLAB function nchoosek, where the first argument is the vector to draw from (here, 1:100 for the 100 elements in  moneys) and the second argument is the number of


elements to select from that vector (1 for single transactions, 2 for pairs, 3 for trios, etc.).       Each row of the result of nchoosek can be treated as a possible set of transaction indices   (e.g., indices used to reference the moneys vector). Thus, the most intuitive way (but not necessarily the best; see extra credit) to set up your code is with two loops: an            outer loop which loops over the number of transactions to consider (i=1:5), and an inner loop which loops over rows of the output of nchoosek. There is a more efficient way to solve this problem wherein you only use a single loop (i=1:5) and work with the       entire output of nchoosek at once; this method runs much more quickly, and I encourage     you to try it if you have time, but it is not worth extra credit.

Upon completion, your workspace should contain the following variables (bold = extra credit).  Additional variables used in computations are also fine to include and will not affect your grade.

x         plussign   ordered    fib4181   gotcha