submit p6720 midterm midterm.txtPlease be sure you submit your final version. Replacing the file is permitted until the end of the test.
1 (b)
A file called favorites contains a table of numbers with two
numbers on each line. Write the one-line Unix command to display the
lines containing the lowest ten values that occur in column 2.
1 (c)
Write the one-line Python numpy code for multiplying two
matrices A and B and storing the product in a matrix
called C. Assume that numpy has been loaded with import
numpy as np.
1 (d)
Use Maple to find the first nonzero positive root of the function
x^{2} - sin(x) to four significant figures. Copy the
Maple command(s) to your answer file including your answer.
1 (e)
Write the Python command that prompts the user with "Enter three
numbers, separated by commas", reads the values that are entered, and
stores them in the variables x, y, and z.
1 (f)
Use Python in interactive mode to calculate the cosine of 70 degrees.
Copy the lines of your Python session (including the answer) to the
answer file.
1 (g)
Write the Python command to generate a vector (list) called u
containing six components, all equal to 0.
1 (h)
You are using the Newton-Raphson algorithm to find the solution to
f(x) = 3 x^{2} - 5 x + 1 = 0Your current best guess for a root is x = 0. What does the Newton-Raphson algorithm give you for your next best guess? In your answer file, write the formula (in terms of x) that you used to get the result and then the result.
#! /usr/bin/python3 import sys ############################################################## # In your answer file write the full code for the function # that computes the difference of two vectors. The code is intended # to go in the space below. Please do not change the # calling program. Your function needs to work with # the calling statement given below. # Your function should check that the lengths of the vectors # are the same. If they are not, the code should return # the one-component vector [0] and a status integer that signals failure. # If they are the same length, the code should return the difference vector # and a status integer that signals success. ############################################################## # (Your code is intended to go in this space) ############################################################## def main(): a = eval(input("Enter the first vector in the format a1, a2, a3, ..., an ")) b = eval(input("Enter the second vector in the format b1, b2, b3, ..., bn ")) # Call the function to compute the difference a - b d, status = diff(a,b) if status == 0: print("The result is", d) else: print("The vectors must have the same length. Quitting") sys.exit(status) ############################################################## main()
4 (a) You used the Python linalg package to calculate Ainv, the inverse of a 3x3 matrix A, and when you multiply Ainv times A you get
array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 4.16333634e-17, 1.00000000e+00, 2.77555756e-17], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])Explain in a two or three complete, clear sentences why some of the off-diagonal elements are not equal to zero, and why they have the size you see?
###################################################################### def poly(n, a, x): """Evaluate a polynomial""" p = a[n] for i in range(n-1): p = a[n-1-i] + x*p return p