HW03: Practice Programming Loops#

You have seen For Loops in class. This homework will give you more practice. Loops are a very important concept in programming.

Problem 1:#

Write a loop that calculates the factorial of an integer number n. Remember that factorial 10 \(= 10*9*8*7*6*5*4*3*2*1\). You’re loop should return the factorial of the number n passed to it. You can assume that n will be a positive integer.

Once you have written your loop inside (HA! Don’t just call math.factorial inside the function, you have to write the loop yourself meaning put in the action part of the loop the multiplication of each element), test it out by running it with starting values of 5, 10, and 20. Print out the results with the comparison to the math.factorial function.

Problem 2: While Loop#

In the lecture we used a loop to complete a random walk through a grid as is detailed in the class notes. In the lecture notes for Brownian motion, there is another example showing a random walk on a line between along 10 locations of a 1 dimensional scenario. The example also calculates probabilities of arriving at the end given a start position.

Here, write a loop titled random_walk (you can use a markdown cell for that). Specify two variables: start and end. The loop should calculate the number of steps it takes to reach the end location starting from the start location. The moves are either +1 or -1 on a one dimensional “track”. For example, if you start at 6, a random move would move you either to 5 or 7. The random walk ends if it reaches the end. The random_walk function should return the number of steps taken.

Run your loop 100 times and calculate the average and standard deviation of the number of steps it takes to reach the end location starting from the start location, given a start location of 12 and an end location of 18. Print out that average and standard deviation. (Hint: One way is to use arrays to store the number of steps it takes to reach the end location for each of the 100 random walks. Then you can use the np.mean and np.std functions to calculate the average and standard deviation.)

Make sure you use a while loop in your function and you can use the np.random.choice([-1,1]) function to randomly choose a step of -1 or 1. Also add in a catch to stop the loop if the number of steps exceeds 100,000 using break in your loop.

Problem 3: Nested Loops#

In the lecture module on Brownian motion, there is a nested loop to calculate the diffusion coefficient of a particle undergoing a random walk. The inner loop was for a given ‘walk’ with a certain number of steps. The outer loop was to loop over the total number of random walks.

In this problem, modify the code given on the course website to include a third dimension z. Instead of only being able to move in x or y each step, now the particle can move in x,y,or z. Set the number of walks to 100 (instead of the 1000 as done in the lecture notes but still keep the number of steps at 1000). For this problem, complete the following:

  1. Plot the random walk with the x position versus the z position (similar to what is done in the lecture notes but with the z position given instead of the y). It will still be a 2D plot, no need to make it 3D. Like the example, you should have the start and end position properly plotted and labeled.

  2. Plot the step number for the random walk versus the average squared distance from the origin like is given in the example. No need to do more simulations than the number of walks of 100. Also no need to give the linear fit as we haven’t covered that yet.

  3. Describe what this is doing: avesqdists = np.mean(sqdists,axis=0)

  4. Write a paragraph reflecting on what you have learned so far, why what you have learned could help you, and what you might do differently in the future based on what you have learned.

Hints:

  • Start with the 2D code from the lecture notes and modify it to include the third dimension.

  • To make a choice on which direction to move in, you can use the np.random.choice function with the options being [-1,0,1] for each of the x,y, and z directions. Then use if statements to move in the given direction.

  • You can remove any code that you don’t need.