Physics 3730/6720 Midterm Test Session 1

Rules


Please put the answers to the following questions in a text file midterm1.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) While you are in your home directory, you run ls with the following result
 Makefile 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 create the required subdirectories and put your files there.

   mkdir asst01 asst03
   mv *.txt asst01
   mv M* *.cc asst03
There are many variations.

1 (b) Your C++ code consists of three files called main.cc, sub1.cc, and sub2.cc. You want to compile the code to produce an executable code called solve. Write a two-line Makefile so the command make solve compiles the code.

   solve: main.cc sub1.cc sub2.cc
      g++ main.cc sub1.cc sub2.cc -o solve


1 (c) A file called favorites contains a table of numbers with two numbers on each line. Write the one-line Unix command to print the lines containing the lowest ten values that occur in column 2.

    sort -k2 -n favorites | head -n 10


1 (d) Write the one-line command that prints all the lines in the file quadratic.cc that contain the string "risky" somewhere in the line.

   grep risky quadratic.cc


Problem 2. (20 pts)

2 (a) A student wants to create a plot in Postscript format in a file called plot.ps. So she writes a gnuplot command file with the three lines
   plot 'mydata'
   set terminal postscript
   set output 'plot.ps'
But after running gnuplot with these commands, and getting no error messages, she notices that the file plot.ps is empty. She checks the file mydata and verifies that it has the correct numbers. She then figures out how to fix the command file so it creates the plot correctly. What does she have to do? Put your corrected command file in your answer file.

The "set" commands should come before the "plot" commands.

   set terminal postscript
   set output 'plot.ps'
   plot 'mydata'


2 (b) Evaluate the Bessel function J2(Pi) to 15 digit accuracy (Pi is pi). Show how you did it. For example, if you used Maple, give the Maple command(s).

   evalf(BesselJ(2,Pi),15);
You can also get 15 digits with "Digits = 15;".

Problem 3. (30 pts)

You have written the following program:
#include <iostream>
#include <cmath>
using namespace std;

void func(const double x, double *y, int &status)
{
  if(x == 0){
    *y = 0;
    status = 1;
  }
  else {
    *y = 5/x + cos(x);
    status = 0;
  }
}

int main(){
  
  double p = 4;
  double y;
  int status;

  cin >> p;

  func(p, &y, status);

  if(status == 0)
    cout << y;
  else
    cout << "error";
}

It evaluates a function. It also checks for a singular condition at x = 0 and gives an error message in that case.

You decide to rewrite the code and change the way you get the output value y. Instead of passing y back through the second argument of func, you make it be the return value of func. This requires changes in the code for func and in the code for main.

Make these changes and paste the entire revised code in your answer file.

#include <iostream>
#include <cmath>
using namespace std;

double func(const double x, int &status)
{
  if(x == 0){
    status = 1;
    return 0.;
  }
  else {
    status = 0;
    return 5/x + cos(x);
  }
}

int main(){

  double p = 4;
  double y;
  int status;

  cin >> p;

  y = func(p, status);

  if(status == 0)
    cout << y;
  else
    cout << "error";
}

Problem 4. (20 pts)

4 (a) When we evaluate the expression
    -b + sqrt(b*b - 4*a*c)
for what conditions on the parameters a, b, and c is a loss of precision certain to occur due to roundoff?

If b > 0 and b*b >> |4*a*c| then we will have trouble.

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

What is the principal advantage of the Newton Raphson method?
What is the principal advantage of the bisection method?

Answer both questions in clear, complete sentences!

The NR method is faster -- it is a second order method.

The bisection method is reliable -- it always finds a root if it is bracketed properly to begin with.


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


This page is maintained by:
Carleton DeTar Mail Form
Last modified 4 October 2008