Homework41.pdf

MIE 124

HW 4

In this assignment you will be exploring a leaning tower of lire. A leaning tower of lire is an

overhanging stack of blocks. How far each block can overhang the one below it is given by

1/(2*n) where n is the block's position from the top (see picture below). So, block 1 can

overhang block 2 by ½ its length, whereas block 2 can overhang block 3 by 1/4 th , etc. Note that

the block number starts at the top of the tower, so we are building the tower from the top

downwards.

You will be calculating how many blocks a tower will need for a given block length and total

desired overhang.

For your homework, turn in two m_files, a script and a function. Call these

“hw4_script_yourinitials.m” and “hw4_function_yourinitials.m”, respectively (replace yourinitials

with your actual initials). Use proper formatting and headers in both files. The script will run the

function.

Your Function

Your function will have two scalar inputs - the desired overhang of the tower, and the block

length. The outputs of your function are the required number of blocks to reach the desired

overhang (a scalar) and the total overhang as each block gets placed (a vector). The overhang

is measured between the left edge of the bottom-most block and the left edge of the top-most

block. So, the overhang is 0 for a tower with one block. With two blocks, your tower has an

overhang of block_length*½ . After the third block the overhang increases to block_length*½ +

block_length*¼ , and so on and so forth.

Your function essentially builds the tower starting at the top block and moving down while

keeping track of the total overhang. The function will use a while loop that adds each

consecutive overhang to the running total overhang storing the latest value in the last element of

a vector. Use an integer counting variable to track the number of iterations in the while loop.

Initialize the overhang as 0, and then add a new element to it each iteration, which is the total

overhang at that particular iteration. You should continue looping until the total overhang is

equal to or greater than the desired overhang.

Your Script

Your Script will use two for loops to call your function. The first for loop will call your function for

varying desired overhangs and fixed block length. The second for loop will call your function for

fixed overhangs and varying block lengths.

Your first for-loop will have a constant block length of 1 and should call your function for desired

overhangs ranging from 1 to 4 with step size of 0.1. You should store the total number of blocks

(one of the function outputs) for each iteration of your for-loop in a vector. Create a plot with

desired overhang as the x-axis and required number of blocks as the y-axis. Label your axes

accordingly and give the figure an appropriate title. Also, make the line dashdot and colored

magenta with no point markers. In a comment in the script, discuss the plot and why you think it

looks the way it does (2-3 sentences). On a separate figure, plot the overhang vector that your

function outputs on the last loop iteration versus block number. I.e. plot the overhang vector for

a desired overhang of 4. Label and title this plot appropriately and make the line solid black. In a

comment in the script, discuss the shape of this plot.

Your second for-loop will have a constant desired overhang of 4 and should call your function

for block lengths ranging from 1 to 2 with step size of 0.05. Again, store the total number of

blocks (one of the function outputs) for each iteration of your for-loop in a vector. Create a new

plot with block length as the x-axis, and the required number of blocks as the y-axis. Label your

axis accordingly and give the figure an appropriate title. This time, make the line dashdot and

colored red with no point markers. Again, make a comment in your script discussing the plot.

For the last part of your script, you will call the function two more times and keep track of the

time it takes to run the function using the commands tic and toc. As you can imagine, as the

overhang gets larger with respect to the block length, the number of blocks required gets large

very fast. Call your function with a desired overhang of 8 and block length of 1, and then again

with an overhang of 9 and block length of 1. Record how long it takes your function to produce

an answer each time and call them time_oh_8 and time_oh_9 respectively. Also record the

number of blocks it takes and call them blocks_oh_8 and blocks_oh_9. In a comment in your

script, discuss how many blocks it takes and how long it takes on your own computer to get

answers for both lengths.

Hint: Type help tic and help toc to learn more.