Physics 3730/6720 Midterm Test (Fall 2018)

Rules


Please put the answers to all of 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) The file gax has lines with data from a galaxy survey. The third column of this file contains the recession velocity. Write a one-line Unix command that lists on your screen the lines that have the top 15 recession velocities.

1 (b) Suppose that u = [-1, 0, 1] is an eigenvector of the matrix A with eigenvalue 5. What is A-1u? That is, what is the inverse of the matrix times the eigenvector?

1 (c) Write the one-line Python3 command that prints all the values in the second column of the matrix S.

1 (d) A file myfile.txt contains the elements of a matrix. You are trying to read it using the numpy loadtxt function and store its value in an array S You are testing the code in interactive mode and get an error:
    101 cygnus:~> python3
    Python 3.5.0a2 (default, Mar 19 2015, 15:31:59)
    [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy as np
    >>> S = np.loadtxt(myfile.txt)
    Traceback (most recent call last):
    File "", line 1, in 
    NameError: name 'myfile' is not defined
    >>>
Write the corrected Python command that fixes the problem.

1 (e) You are editing a file with emacs. How can you tell just by looking at the emacs window that your changes have not been saved?

1 (f) In your Python code you need the absolute value of a floating-point number x. Give the expression that calculates that value and, if you need to import any package(s), write the statment that does that.

Problem 2. (20 pts)

2 (a) You are using the Newton-Raphson algorithm to find the solution to
  f(x) = 1 - 3*x + x2
Your current best guess for a root is x = 1. What numeric value does the Newton-Raphson algorithm give you for your next best guess? Show how you arrived at your result.

2 (b) Your Python3 code has an array s of 10 values. Write a four-line code using a for loop that calculates the sum of all the positive values in the array, leaving the result in the variable sumPos.

Problem 3. (30 pts)

You are writing a Python3 program to read two vectors from separate files and calculate three values: the norms of the two vectors and their dot product. You design your code so the actual calculation is done in a separate function. 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
import numpy as np
from numpy import linalg as LA

##############################################################
# In your answer file write the full code for the function
# that computes |v|, |w|, and their dot product.
# Your function should return all three values.
# The function should go in the space below and work with the
# calling program. 
# Please do not change the calling program.
##############################################################

# (Your code is meant to go in this space, but copy it to your
   answer file once you have completed it.)


##############################################################                                      
def main():

    # Read two vectors
    v = np.loadtxt('v.txt')
    w = np.loadtxt('w.txt')

    # Calculate the Euclidean norm of both vectors and their dot product

    normv, normw, dotvw = normDot(v,w)

    print("normv, normw, dotvw = ", normv, normw, dotvw)

##############################################################                                       
main()

Problem 4. (20 pts)

One of the goals of this course is to provide a diverse tool set so when you need to solve a problem, you can choose the best tool to do the job. In this problem, consider the following tools: (1) pencil and paper (2) awk, (3) interactive Python3 (4) Python3 program (5) Maple. Select the the one that is most efficient or most appropriate for each of the following problems and explain in a clear, complete sentence. If two methods are more or less equally efficient/appropriate, give both and explain.

(a) Solve the quadratic equation x2 - 9 = 0.

(b) Calculate the secant of 1.4 radians.

(c) Find the characteristic polynomial of a 5x5 matrix A.

(d) List the squares of the first value on each line of a text file.

(e) Calculate the determinants of a few dozen 20x20 matrices. The elements of the matrices are provided in a file.


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 2 October 2018