# Physics 3730/6720 Midterm Test (Spring 2018)

#### Rules

• The test is open book and notes. You may use the internet to consult references. However, you may not seek or receive assistance from anyone other than the instructor or TA.

• There is one answer file, namely midterm.txt. Please submit it just as you have been submitting assignments. Use
```    submit p6720 midterm midterm.txt
```
Please be sure you submit your final version. Replacing the file is permitted until the end of the test.

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) Write a Unix command that moves all files ending in the characters .txt to a subdirectory (under the current directory) called examples.

1 (b) A file called positions contains a list of position vectors, one per line. Each line has two numbers, namely, the x and y components of a vector. Write the one-line Unix command that creates a new file called lengths that contains the lengths of the vectors (the square root of the sum of the squares of the components), one length per line.

1 (c) You have loaded numpy as np and its linalg package as LA. Write the one-line command that normalizes the vector x to unit length and stores the result in the vector y.

1 (d) Use Maple or Matlab to find the lowest positive solution to x = cot(x) to four significant figures. Copy the Maple or Matlab command(s) to your answer file including your answer. (You may do the rounding by hand.)

1 (e) You want to read the string of characters vibranium from standard input and store it in a variable metal. Your input statement reads

```  metal = eval(input("Enter the name of the metal "))
```
What should you type on the keyboard when prompted by your command?

1 (f) You are trying to use Python in interactive mode to calculate the the tangent of 40 degrees. You 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
>>> tan(40)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'tan' is not defined
>>>
```
There are multiple errors here. Write the Python commands that give the correct result.

1 (g) In the Python for loop
```  for i in range(n+1):
```
with n = 6, what are the first and last values of i?

1 (h) Suppose a Python function f(x,y) has the definition
```  def f(x,y):
return x**2, x*y
```
and it is called with
```  a, b = f(7,5)
```
What are the resulting values of a and b?

1 (i) Write four lines of Python code that sets x to 5 when b is zero, but otherwise sets x to 2. (x and b are integer variables).

1 (j) If u1 is an eigenvector of A with eigenvalue 5 and u2 is an eigenvector of A with eigenvalue 2, what is A (2*u1 - u2)?

#### Problem 2. (20 pts)

2 (a) You are using the fixed-point method to find the solution to x = sin(2*x). You start the sequence with the guess x = 1. Write the next two values in the sequence. (Answer to four significant figures.)

2 (b) You have a file data that contains, on each line, the polar coordinates r and theta (in radians) separated by a space. You want to convert them to Cartesian coordinates x and y and plot them in a scatter plot with the x range between -1 and 1 and the y range between -2 and 2. Write the one-line gnuplot command that creates the plot on your screen.

#### Problem 3. (30 pts)

You are writing a program to solve the linear system Ax = b, where A is a square matrix, b is a column vector, and x is the solution vector. You decide to check the solution by computing the norm (Euclidean) of the vector A x - b. The main program is shown below, but the function for checking the solution 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 numpy as np
from numpy import linalg as LA

##############################################################
# In your answer file write the full code for the function
# that computes |A x - b|, the norm of the difference between the
# left side and right side of the equation.
# Your function should return the value of the norm.
# The function should go in the space below and work with the
# calling program.
# Please do not change the calling program or the import statements.
##############################################################

# (Your code is intended to go in this space)

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

# Read the matrix and vector from files

# Solve the linear system
x = LA.solve(A,b)

# Call the function to compute |Ax - b|
d = check(A,x,b)

print("The solution to Ax = b is", x)
print("The norm |Ax-b| is", d)

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

```

#### Problem 4. (20 pts)

4 (a) In calculus we learn that a derivative of a continuous, differentiable function f(x) at x is found by taking the limit as h->0 of

```    [f(x+h) - f(x)]/h.
```
The mathematical idea of the limit is that the ratio gets steadily closer to the derivative as h is made smaller. Suppose we try taking the limit numerically, using this formula in a computer program. Will the numerical quantity get closer and closer to the derivative? Explain in a clear, complete sentence.

4 (b) You need to find the root of a continuous, differentiable function f(x), and you are deciding between the Newton-Raphson method and the bisection method.

In what way is the Newton-Raphson method superior to the bisection method?
In what way is the bisection method superior to the Newton-Raphson method?

Answer both questions in clear, complete sentences.
Please be sure to save the final version of your answer file and then submit it. See instructions at the top.