Reference no: EM132394077
CDS 230
Assignment
1 Question
Create a vector of 10,000,000 random elements using a function from numpy.random. Compute the average of this vector using the old-fashioned, slow, for loop method. Show the code and the answer.
2 Question
Compute the average using a numpy function. Show the code and the result.
3 Question
The time.time() function will return the current time in terms of seconds. It is possible to determine the computational time of a process by capturing the time before and after the execution of the process. Write a Python script to compute the for the previous two problems. Do not get a new vector, and do not include the creation of the random vector in your timing.
Compute the ratio of the time to compute the average using Problem 1 to the time it took to compute the answer using the method in Problem 2. Show your codes, the times for each method, and the ratio..
4 Question
Use the numpy random seed value of 23423. Create three random vectors, each of length 5. These are named a, b, and c. Compute d = a + b × c. Show your code and all four vectors.
5 Question
Create a 5 × 5 matrix of random values. Use the seed of 453. Using the random slicing technique, write a Python command that retrieves values for the following locations in the matrix (1,3), (2,2), (3,1).
6 Question
Recall the equation used for an object falling under the influence of gravity,
y2 = y1 + v1t + 1/2 gt2.
Start with a height of 600 m. Your task is to compute the height of the object for ten different experiments.
The experiments differ in the amount of time the object drops: t→ = (1 sec, 2 sec, 3 sec, 4 sec, 5 sec, 6 sec, 7 sec, 8 sec, 9 sec, 10 sec.
To compute the y2 values for all experiments, use,
y→ 2 = y→ 1 +1/2gt→ 2.
Once the data variables are assigned, this computation can be done in one line of Python. Show your code and the results.
7 Question
Recall the chaos equation:
xn+1 = rxn(1 - xn).
Python does not offer tools to solve this equation efficiently because it is a recursive equation. The answer to xn is needed before xn+1 can be computed. However, it is possible to use NumPy tools to efficiently calculate the results for several trials using different values of r.
Create a vector rvec which has the values (2, 2.3, 2.8, 3, 3.3,, 4.0). Create a matrix mat which is 50 × 6.
In a single command set the values of the top row of the matrix to 0.1. (This was the initial starting value of x.) Each row in this matrix will be one iteration of the equation and each column is one trial. Create a for loop that spans the number of iterations (49). Inside the for loop there should be a single line that calculates the values for all trials in a single row.
The first values in the first column correspond to the experiment when r = 2.0. The x value in this trial stabilizes at 0.5. Thus, the last four values in the first column should be 0.5. In a single Python command, print out the last for rows for all columns. These values should match the results from the lecture.
8 Question
Consider the case in which the goal is to find a vector ~h such that,
h→ · xi→= 1.
The xi→ represents a vector from a set of vectors. The equations states that the dot product of the vector h→ with any of the x→ vectors is 1.0. The task of this assignment is to find h→ for a given set of x→ vectors.
The solution starts with creating a matrix X in which each of the columns is one of the x→ vectors. The code for generating this matrix is:
1 np . random . seed ( 235 )
2 X = np . random . ranf ((10 ,3) )
The equation to find h→ is
h = X (XT X)-1c→.
The XT is the transpose, and the superscript -1 is the inverse. The c→ = (1, 1, 1). The length of c→ is the same as the number of vectors used to make X.
Write a Python script to compute h→. Show your codes and results. Confirm your results by compute h→ · xi→ where xi is a column from X.
There are three columns in X, so you should confirm the h→ for each column.