[Solved] COMP1012 Lab9

$25

File Name: COMP1012_Lab9.zip
File Size: 122.46 KB

SKU: [Solved] COMP1012 Lab9 Category: Tag:
5/5 - (1 vote)

Material covered

  • Numpy

Notes

You must save all your code as a script. The program should be written using vectorization.

Exercises

Exercise 1: Random walk

Fair use

This problem is adapted from Exercise 37 of Langtangens A Primer on Scientific Programming with Python.

Simulating gas particles in a closed space is something you can do with a Monte Carlo type simulation simulate a period of time (using a loop) and move particles around

(change values for (x,y) coordinates) for each unit of time. This kind of simulation is called a random walk.

Download the file randomwalk.py. This implements a Monte Carlo simulation for a random 2-dimensional walk. It uses numpy arrays, but it does not use them idiomatically.

Your task for this exercise is to change this program to use numpy arrays idiomatically. That is, the block

for i in range(np):

direction = random.randint(1, 4) if direction == NORTH: ypositions[i] += 1 elif direction == SOUTH: ypositions[i] -= 1 elif direction == EAST: xpositions[i] += 1 elif direction == WEST: xpositions[i] -= 1 should be rewritten to use numpy arrays of random integers.

Challenge: multidimensional arrays

Rewrite the code so that (x,y) coordinates are represented with a 2-dimensional numpy array. Heres how you can start:

coords = np.zeros(shape=(np, 2)) # xpositions = numpy.zeros(np)

# ypositions = numpy.zeros(np)

Remember that you can index entire columns of a 2-dimensional array by using the notation [:,n] to get the nth column.

Super challenge: no loops

Rewrite the code so that it has no loops. Youll need to think in 3 dimensions to accomplish this.

Answer some question first:

  1. Can you simulate time without loops?
  2. Each unit of time depends on the unit of time before Can different matrices in a 3-dimensional numpy array depend on each other?

If you can answer yes to both of these questions, then proceed!

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] COMP1012 Lab9
$25