Physics 3730/6720 Midterm Test (Fall 2017)

Rules


Please put the answers to the following questions in a single text file midterm.txt. Identify each answer by beginning the line with the number and letter 1 (a), 1 (b), etc. of the question.


Problem 1. (30 pts)

1 (a) Write a Unix command that lists the contents of your home directory, no matter where your current directory happens to be, displaying the file size and date.

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 x2 - 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 x2 - 5 x + 1 = 0
Your 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.

1 (i) You are using the bisection method to find a zero of a continuous function f(x). You have determined that f(a) < 0 and f(b) > 0. You find that f((a+b)/2) < 0. What is the next step? Answer in pseudocode.

1 (j) If [3, 1, 6] is an eigenvector of A with eigenvalue 9, is [6, 2, 12] also an eigenvector? If yes, what is its eigenvalue?

Problem 2. (20 pts)

2 (a) You have imported numpy as np and are reading a file data that contains five columns of numbers separated by spaces. You want to put the third column in a vector called x and the fifth column in a vector called y. Write two or three lines of Python code to do that. (Be careful with the indexing.)

2 (b) You have a file data that contains five columns of numbers separate by spaces. Write the one-line gnuplot command that makes a two-dimensional plot taking the third column as the horizontal axis and the fifth column as the vertical axis. The plot should have lines connecting the points.

Problem 3. (30 pts)

You are writing a program to compute the difference of two vectors. The main program is shown below, but the function is missing. You are asked to complete it by writing the missing function. Put ONLY the code for the function in your answer file.

#! /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()


Problem 4. (20 pts)

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?

4 (b) Someone has written a code for Horner's algorithm, but, where it should have range(n) for a polynomial of degree n, the code has range(n-1). (See below.) If the subroutine is given n = 2 and a = [3,-2,1], what polynomial is actually evaluated? Write it as a function of x in your answer file.
######################################################################                            
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



Please be sure to save the final version of your answer file and then submit it. See instructions at the top.


This page is maintained by:
Carleton DeTar detar@physics.utah.edu
Last modified 3 October 2017