Comp 110 Midterm Exam  Spring 2009:  Due at Noon Thursday, March 12.  Send Your Notebook by E-mail to:  rng@rice.edu.

All work must be entirely your own.  You are not allowed to work with a partner or to consult on these problems with anyone except the instructor or the labbie.

This exam is closed book and closed notes.  You are not permitted to look at your work on previous modules or previous homeworks while taking this exam, but you are encouraged to make extensive use of the Documentation Center.  

Caution:  Although we expect you to make extensive use of the Documentation Center to assist you with these problems, when you are asked to explain concepts or notation, you should not copy your answers verbatim from the Documentation Center.  Present the answers in your own words.  Avoid jargon and special notation;  use standard English and classical Mathematical notation.  When you are asked to present examples, provide your own examples;  do not copy examples from the Documentation Center.

All proofs and computations must be done using Mathematica;  no other proofs or computations will be accepted.  

Please format your solutions appropriately.  Use text format, not input format, when you are typing text.  Write coherent sentences and paragraphs;  part of your grade will depend on how clearly you present your ideas.

There is a 3.5 hour time limit for this exam.  If you get stuck on a problem, go on to the next problem and come back later to the problem that is giving you trouble.  Do not waste time.

Debugging  (10 Points)

1.  Explain why it is good programming practice to clear the name of a new function (above the definition of the new function but in the same cell), even before you define the new function.

2.  Explain why it is not sufficient simply to delete the old function definition when you are redefining a function to remove a bug.

3.  Debug the following program for computing the sum of the squares of the numbers in a list.
Give examples to show that your debugged program works for any list of numbers.

sumSquares[mylist]=Module[{sum_=0},
Do[sum=(([[i]])^2),{i,n}];
sum];

4.  In Problem 3, which variables are local and which variables are global?

Functions and Expressions  (10 points)

1.  Explain in your own words the difference in Mathematica between a function and an expression.

2.  Give 3 examples to illustrate the difference between a function and an expression.

3.  Give 3 examples of procedures in Mathematica that will work only if you pass functions, but will not work correctly if you pass expressions.  (Hint:  Use the Document Center.)

Prime Numbers  (16 Points)

1.  Let π[n] denote the number of prime numbers less than or equal to n.  Explain the difference between the function π[n] and the Mathematica function Prime[n].  Give examples to illustrate the difference.

2.  Use the Documentation Center to find a built in Mathematica function that computes π[n].  What is the name of this built in Mathematica function?

3.   Plot the ratio (p/(π[p])) between the p and the number of primes less than or equal to p, for all integers p between 2 and n.  Consider both small (<100) and large (>10,000) values of n.  Join the points on your plot with straight lines.  What do you observe?

4.  Plot the logarithm of p for all integers p between 2 and n.  Consider both small and large values of n.  Join the points on your plot with straight lines.

5.  Plot the functions in parts 3 and 4 in the same graph. What do you observe?

6.  The built in Mathematica function LogIntegral computes the integral of (1/(Log[t])) between 0 and x.  Using the built in function LogIntegral, plot the ratio (p/((∫_0^p) 1/Log[t]  dt)) between p and the integral of (1/(Log[t])) from 0 and p, for all integers p between 2 and n.  Consider both small and large values of n.  Join the points on your plot with straight lines.  What do you observe?

7.  Plot the functions in parts 3,4,6 in the same graph.  Consider both small and large values of n.  What do you observe?

Calculus  (16 Points)

1.  The derivative of a function f(x) at x=t is, by definition, the slope of the tangent to the curve y=f(x) at the parameter x=t.  To compute this slope, we approximate the slope of the tangent at x=t by the slope of the chords to the curve from the point of interest to nearby points and then take the limit of these slopes as the nearby points approach the given point.

Problem:  Using the definition of the derivative in terms of a limit, write a Mathematica function diff that takes as input a function f and a parameter t and returns the derivative of the function f evaluated at the parameter t.  In your definition of diff, you are NOT permitted to use the Mathematica functions Derivative, or D, or (∂_x), nor can you use the notation f '.

2.  Using the function diff you defined in part 1, compute the following:
	a.  the derivative of sin(x) with respect to x.
	b.  the derivative of the function (x^2) at x=1.
	 c.  the derivative of ln(x) at x=0.
	 d.  the derivative of (e^x) at x=t.
In each case verify that your answer is correct by using the built in Mathematica function for differentiation.

3.  The definite integral from x=a to x=b of a function f(x) is, by definition, the area between the x-axis and the curve y=f(x) for a≤x≤b.  To compute this integral, we approximate the area under the curve by a collection of rectangles, and then take the limit of the sum of the area of these rectangles as the number of rectangles approaches infinity.

Problem:  Write a function integral that takes as input a function f and computes the limit of the sum of the area of the rectangles that approximate the area between the x-axis and the curve y=f(x) for 0≤x≤1.  In your definition of integral, you are NOT permitted to use the Mathematica function Integrate or the symbol ∫.  You must compute the limit of a sum.  (Hint:  Look up Sum in the Documentation Center.)

4.  Using the function integral you defined in part 3, compute the following:
	a.  the area between the x-axis and the curve y=1 for 0<x<1.
	 b.  the area between the x-axis and the curve y=(x^3) for 0<x<1.
	 c.  the area between the x-axis and the curve y=(e^x) for 0<x<1.
	 d.  the area between the x-axis and the curve y=sin(π x) for 0<x<1.
In each case verify that your answer is correct by using the built in Mathematica function for integration.

Geometry:  Law of Cosines  (16 Points)

The Law of Cosines states that in any triangle ΔABC:

(c^(2 ))= (a^2)+ (b^2)– 2abCos(C)

Use Mathematica to derive the Law of Cosines.

(Hint:  Use the standard formula for the cosine of angle C:  Cos(C)=((b-x)/a), and apply the Pythagorean Theorem to obtain two additional equations involving the height h and the length x (see diagram).  Then invoke Mathematica to eliminate the variables h and x from your equations -- look up the function Eliminate in the Documentation Center -- and simplify your answer.)

Vector Algebra  (16 Points)

In the following problems, a,b,c denote arbitrary vectors in 3-dimensions, × denotes cross product, and · denotes dot product.  Using Mathematica, either prove or provide a counterexample for each of the following four identities.

Note that numerical examples are fine as counterexamples.  But a numerical example is NOT a proof.  NO CREDIT will be given if you present a numerical example in place of a proof.

(Hint:  How do you represent vectors in Mathematica?)

1.  a·(b×c) = (a×b)·c

2.  a×(b×c) = (a·c)b – (a·b)c

3.  a×(b×c) =  (a×b)×c

4.  a×(b×c) +  b×(c×a) + c×(a×b) = the zero vector

Debugging  (16 Points)

The curve depicted below looks like the symbol for infinity.

The parametric equations of this curve are:  x = sin(θ),  y = sin(aθ), where 0≤θ≤2π and a=2.  Below is a Mathematica program to generate this curve, but the code has a few bugs.

loop[[b]]=manipulate[Plot[(sin(θ),sin(aθ)),{x,0,2π,π/10},{Aspectratio->true,Plotstyle->thickness[[.0085]],Plotrange->automatic,Imagesize=25}],{a,Table[k b,{i,1,5,1/2}]}];

loop[[2]];

1.  Debug this code so that the program correctly generates the infinity symbol.

2.  What influence does the parameter a have on the curve?  Explain the difference between the curves generated for even and odd values of a.

3.  What happens if you replace 2 by (Sqrt[2])?