Physics 3730/6720 Midterm Test
submit p6720 midterm midterm.txt
Please be sure you submit your final version. Replacing files is
permitted until the end of the test.
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.
1 (b) You have written a program
called addup that adds up a list of numbers. That is, when you
type addup at the Unix command prompt and then type a bunch of
numbers on the keyboard, followed by Ctrl-d, the program prints the
sum of the numbers you typed. You have a file
called expense.txt that has two columns, the first gives the
name of an item as a single word and the second gives the price.
Write the one-line Unix command that uses Unix utilities
and addup to get the total of all the prices in the second
column of the file.
1 (c)
You have a file data that contains five columns of numbers.
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.
1 (d)
Find all the zeros of x - 2*exp(x/4)*cos(x) on the interval
[-10,10] to 4 significant figures by any quick method. Explain how
you did it (showing all of your work) as well as giving the answer.
1 (e)
Suppose you have a file called galaxies with lines with
#. You need to remove these lines and create a new file
called galaxies.fix. Write a one-line Unix command for doing
the job.
Problem 2. (20 pts)
2 (a)
In the following code fragment say what values are printed out and why.
double a = 5; double *r = &a; double &g = a; *r = 2; cout << a << "\n"; cout << g << "\n";(Be very careful about the data types and the meaning of each of the assignments.)
You are using Horner's algorithm to evaluate the polynomial and its
derivative, and you put the Horner code in a separate file, which is
not shown here. That code defines a subprogram called horner.
The subprogram prototype declaration is included below with comments
so you can see what arguments it takes. Please pay special attention
to the datatypes of the arguments.
// Code for finding roots of a polynomial
// using the Newton-Raphson method
// Physics 6720 8/18/03
// Usage
// nr
// The user is prompted for the tolerance and the starting point.
#include <iostream>
#include <cmath>
using namespace std;
#define N 100 // Maximum number of iterations
// This is the prototype declaration for the subprogram horner. It
// evaluates the polynomial at x and returns its value there as pr and its
// derivative there as qp. The degree of the polynomial is n and the
// coefficients of the polynomial are in the array a.
void horner(int n, double a[], double x, double &pr, double *qp);
int main(){
double p,pnew;
double f,dfdx;
double tol;
int i,n;
cout << "Degree of polynomial = ";
cin >> n;
double a[n+1];
cout << "Coefficients, a[0], a[1], ..., a[" << n << "]\n";
for(i = 0; i <= n; i++)
cin >> a[i];
cout << "Enter tolerance: ";
cin >> tol;
cout << "Enter starting value x: ";
cin >> pnew;
// Main loop
for(i = 0; i < N; i = i + 1){
p = pnew;
// Evaluate the function and its derivative
// IN YOUR ANSWER FILE WRITE THE ONE LINE STATEMENT
// THAT BELONGS HERE AND THAT COMPLETES THE CODE.
// THIS STATEMENT CALLS horner WITH THE APPROPRIATE ARGUMENTS.
////////////////////////////////////////////////////
/////////////////////////////////////////////////////
// The Newton-Raphson step
pnew = p - f/dfdx;
// Check for convergence and quit if done
if(abs(p-pnew) < tol){
cout << "Root is " << pnew << " to within "
<< tol << "\n";
return 0;
}
}
// We reach this point only if the iteration failed to converge
cerr << "Failed to converge after " << N << " iterations.\n";
return 1;
}
Problem 4. (20 pts)
Answer in a clear, complete sentence.
4 (a) In calculus we learn that a derivative of a continuous function f(x) at x is found by taking the limit as h->0 of
[f(x+h) - f(x)]/h.
The idea of the limit is that we get steadily closer to the derivative
as we make h smaller. Suppose you try taking this limit in a
computer program. Note that the numerator will involve a subtraction
of two numbers that are nearly equal. Will this quantity get closer
and closer to the derivative? Explain.