Computer Science 221: Software Design Lab
Fall 2005
Professor McCracken
Homework 3: Taylor Series and JUnit testing.
Section P (Tuesday and Thursday) Beginning of class Thursday, October 6.
Section G (Monday and Wednesday) Beginning of class Tuesday, October 11. A Monday schedule is followed on this day.
(Date changes are possible.)
Added 9.30.2005: Here's what you do.
Starting from the code shown below, make a class called MyTrigs, or JoelsTrigs, or whatever else you want to call, as long as it's not DansTrigs. (Daniel Ranells is excused from this requirement. J)
Your trigs class should compute the sine, cosine, hyperbolic sine, and hyperbolic cosine of an argument passed to it. Each method should take a second int argument, which is the highest power of the argument that should be used in the computation.
Then, if your class is MyTrigs, write another class called MyTrigsTest, which does JUnit testing. Sample code is shown at the bottom, for testing the hyperbolic sine.
Now write three more methods, called testDansTrigsTestSin, etc. For the sine and cosine, the range of values should be -3 to 3. For the hyperbolic sine and cosine, the range of values should be -2 to 2. In every case, experiment to find the smallest value of N that will permit the JUnit test to pass. Finally, write a fifth method that tests the sinh with an argument of 10. You will be unable to make the test pass with a tolerance of 1E-15. Find the smallest value for which it will pass. What happens? Why is high accuracy impossible here?
End of 9.30.2005 insertion.
=============================================
Prepare a class named MyTrigs that contains methods to compute the sine, cosine, hyperbolic sine, and hyperbolic cosine. Write a class named MyTrigsTest that does JUnit testing of your four functions.
We will talk more about JUnit testing, of course. Meanwhile, as shown on the Useful Links page, you can study A JUnit tutorial.
The Taylor series for the sine is:
sin x = x - x^3/3! + x^5/5! - x^7/7! + - ...
There is no exponentiation operator in Java; ^ is a common way to show it in something like this.
The Taylor series for the cosine is:
cos x = 1 - x^2/2! + x^4/4! - x^6/6! + - ...
Change all signs to plus signs to get the corresponding hyperbolic sine and cosine.
We evaluate the series by deciding how many terms to take, then rewriting in a form that takes far fewer arithmetic operations. Suppose we stop with terms through x^7. If we pre-compute x^2, calling it xSq, then the resulting expression can be rewritten as:
sin x ~= x [1 - (xSq/2x3)[1 - (xSq/4x5)[1 - (xSq/6x7)]]]
This rewriting is called Horner's method; I mistakenly called it Newton's method in class. (Newton's method is a standard way of finding a real root of a polynomial equation.)
Here is code that computes the sine, using Horner's method
to evaluate the Taylor's series for the sine, through the x^7 term:
static double dansSin(double x){
double sum = 1.0;
double xSq = x * x;
for (int n = 7; n > 1; n -=2){
sum = 1 - xSq/(n*(n-1))*sum;
}
return x * sum;
}
We'll talk about all this. The static is a temporary fix for a problem you won't have .
Change a few details in this, and it computes the cosine. You get to figure out how to do that.
Get this much running and tested with JUnit, then let's experiment with the minimum number of terms to get maximum accuracy, for a specified argument range. Basic change: add an integer argument to each function: n is the highest power in the Taylor series.
The JUnit class described above:
// DDM 9.30.2005 Starter for HW3
package hw3;
import junit.framework.*;
public class DansTrigsTest extends TestCase {
public void testDansTrigsTestSinh() {
for (double x = -2.0; x <= 2.0; x += 0.1) {
double expected = Math.sinh(x);
double computed = DansTrigs.dansSinh(x, 21);
assertEquals(computed, expected, 1E-15);
}
}
}