Physics 3730/6720 Midterm Test (Spring 2017)

Rules


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

ANSWERS ARE GIVEN IN midterm1.txt

Problem 1. (30 pts)

1 (a) Write the one-line Unix command that prints all the lines in the file nr.cc that contain the string "pnew" somewhere in the line.

1 (b) While you are in your home directory, you run ls with the following result

 Makefile asst01/ asst03/ exper.txt  galaxies.txt  practice.cc  roots.cc unix.txt 
You decide to organize your files into subdirectories so the ones with names ending in txt go in a directory called asst01 and the others go in a directory called asst03. Write the Unix commands to put your files there. Use appropriate wild cards for the file names.

1 (c) A program consists of two source code files, namely, a main program redrocks.cc and a header file Utes.h. Write the lines for a Makefile for compiling the code, so that the name of the executable is goUtes. Design it so that any time you change either of the source files or the Makefile itself, the command make goUtes recompiles and rebuilds goUtes. Put the lines in your answer file.

1 (d) In a Maple exercise you were asked to find the third lowest nonnegative root of the Bessel function BesselJ(1,x). The Maple command fsolve(BesselJ(1,x),x) gave the answer 0, which is the first root, so not the answer you wanted. Write the Maple command that does give the correct answer.

1 (e) A text file payroll contains three columns of numbers. Write the Unix command that produces a list of only the numbers in the second column, sorted in ascending numerical order.

1 (f) You are finding the solution to the equation x = exp(-x) using the fixed-point method. Your starting guess is 1. What is the next approximation, according to the algorithm? Put your answer in the answer file and show how you obtained it.

Problem 2. (20 pts)

2 (a) Suppose you have defined a class for representing two-dimensional vectors in polar coordinates,
   class polar {
      public:
        double radius;
        double theta;
        polar(double r, double t){radius = r; theta = t;}
   }
Then, in your main program, you declare
   polar w(5., 1.);
In your answer file, write a line of code that prints the polar coordinates of w.

2 (b) In your code you would like to calculate r = a/s, but s might be zero. Write three lines of code that set r to zero if s is zero, but otherwise set r equal to the desired value. Put your three lines in the answer file.

Problem 3. (30 pts)

You are writing a program to compute the squared norm of a vector. The main program is shown below, but the subprogram is missing. For this question you are asked to complete it by writing in your answer file the missing subprogram.

// Code for computing the squared norm of one vectors
// (The sum of the squares of the vector components

// Physics 6720 2/23/17

#include <iostream>
using namespace std;

// This procedure computes the squared norm
// The return value is the squared norm.

////////////////////////////////////////////////////////////
// IN YOUR ANSWER FILE WRITE THE FULL CODE FOR THE SUBPROGRAM
// THAT COMPUTES THE SQUARED NORM OF A VECTOR AND RETURNS 
// ITS VALuE TO THE CALLING PROGRAM.  THE CODE IS INTENDED
// TO GO IN THE SPACE BELOW.  PLEASE DO NOT ChANGE ThE
// CALLING PROGRAM.  YOUR SUBPROGRAM NEEDS TO WORK WITH
// THE CALLING STATEMENT GIVEN.
///////////////////////////////////////////////////////////



////////////////////////////////////////////////////

int main(){
  double sqnorm;
  int i,n;

  cout << "Number of elements in the vector = ";
  cin >> n;

  double a[n];

  cout << "Vector elements, a[0], a[1], ...\n";
  for(i = 0; i < n; i++)
    cin >> a[i];

  // Subprogram norm2 computes the squared norm.

  sqnorm = norm2(a, n);

  // Then we print its square root
  cout << "The norm is " << sqrt(sqnorm) << "\n";
}

Problem 4. (20 pts)

4 (a) If you have to evaluate a polynomial of degree 100, why would it be advantageous to use Horner's algorithm? Please put a one-sentence answer in your answer file and say how many multiplies and adds it takes.

4 (b) When you are looking for the zeros of a function, why does the Newton Raphson method take fewer iterations than the bisection method? Please put a one-sentence answer in your answer file. Hint: What information about the function do the two methods use?


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 17 February 2017