Programming and Computer Science in Java:
Homework #8:  Doubles, Methods, and Calculus
David Kosbie, 2002-2003

Due Date:  Mon, Mar 17, 2003

For each of the following questions, anything which your programs compute must be computed within a new method, and not directly inside your main method.

In class on Friday, we covered in detail how to approximate the derivative and the integral of a function f(x).  Your homework is to duplicate these programs.

Question 1:  Approximating a Derivative.  Write a Java program which reads in an integer, a, and computes (as a double) the derivative of a function, f(x), at the point x=a -- that is, f'(a).  The function f(x) should be built-in to your program, as in:
public static double f(double x) { return x * x; } // f(x) = x^2

Very briefly, how do you do this?  Because we covered this in detail in class, we will provide very brief notes here.  First, you write a function, approxSlope:
public static double approxSlope(double a, double h)
which takes two parameters -- a and h -- and returns the slope of the secant line through the points (a,f(a)) and (a+h,f(a+h)).  Note that for very small h, this value approximates the derivative f'(a), and for infinitely small h (if there were such a thing), it would in fact be that derivative.

So now you can write your derivative function:
public static double derivative(double a)
which takes one parameter, a, and returns the derivative of f at x=a, that is, f'(a).  It does so by starting with a small value for h, say 0.01, and repeatedly calling approxSlope(a,h), each time halving the value of h, until the difference between successive calls is smaller than some rather small epsilon, say 0.000000001.  Now, due to roundoff error in doubles, there is a chance this process will never terminate.  Thus, you should also terminate the loop if it has run more than some maximum number of iterations.

Try your program out for different functions f(x), and verify your program's operation both by hand (for easily differentiable functions) and, as needed, by calculator.

Question 2:  Approximating an Integral.   This problem is very similar to the preceding problem, curiously enough.  Write a Java program which reads in two integers, a and b, where a <= b, and computes (as a double) the integral of a function, f(x), from the point x=a to the point x=b.  The function f(x) should be built-in to your program, as in:
public static double f(double x) { return x * x; } // f(x) = x^2

Very briefly, how do you do this?  Because we covered this in detail in class, we will provide very brief notes here.  First, you write a function, approxArea:
public static double approxArea(double a, double b, int rectCount)
which takes three parameters -- a, b, and rectCount -- and returns the area under the curve f(x) from x=a to x=b as approximated by rectCount rectangles.  First, note the obvious:  each rectangle has a fixed-width of (b-a)/rectCount.  Now, for each rectangle, use function's value at the left x as the height, so the first rectangle's height will just be f(a) and its area will be f(a)*width, where width = (b-a)/rectCount.  Note that for a large rectCount, this value approximates the integral of f(x) from x=a to x=b, and for infinitely large values of rectCount (if there were such a thing), it would in fact be that integral.

So now you can write your derivative function:
public static double integral(double a, double b)
which takes two parameters, a and b, and returns the integral of f(x) from x=a to x=b.  It does so by starting with a moderate value for rectCount, say 1000, and repeatedly calling approxArea(a,b,rectCount), each time doubling the value of rectCount, until the difference between successive calls is smaller than some rather small epsilon, say 0.000000001.  Now, due to roundoff error in doubles, there is a chance this process will never terminate.  Thus, you should also terminate the loop if it has run more than some maximum number of iterations.

Try your program out for different functions f(x), and verify your program's operation both by hand (for easily integrable functions) and, as needed, by calculator.

Good luck!

DK