Michael Brannon
Michael Brannon

Reputation: 3

Calculate Relative Error Using de Jager formula and While Loops

Just to begin, I have this working using for loops. I CANNOT use for loops however. It has to be these nested while loops.

Since my program works fine using a for-loop, there must be an issue with my nested while logic. Every time the code below runs, I am just always coming back with 0 for the closestEstimate (my intialized closestEstimate starting point). This does not happen and I get a proper fraction of percent estimate with for loop. So there is an issue with my nested while loop logic somewhere.

The instructions are below:

"Now consider the de Jager formula waxbyczd, where each of a, b, c, and d is one of the 17 numbers {-5, -4, -3, -2, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}. The "charming theory" asserts that the de Jager formula with your four personal numbers can be used to approximate μ within a fraction of 1% relative error. For example, suppose you choose to approximate the mean distance from the earth to the moon in miles: μ = 238,900. And suppose you are an OSU sports fan, so your personal numbers are the number of wins in OSU's last national championship season (14; also the record for wins in a year by any college team), the seating capacity of Ohio Stadium (102,329), the year of Jesse Owens' four gold medals in Berlin (1936), and your jersey number when you played high school field hockey (13). Then the value of 14-5102329119361/2134 is about 239,103, which is within about 0.08% of μ.

Your job is to create a Java program that asks the user what constant μ should be approximated, and then asks in turn for each of the four personal numbers w, x, y, and z. The program should then calculate and report the values of the exponents a, b, c, and d that bring the de Jager formula as close as possible to μ, as well as the value of the formula waxbyczd and the relative error of the approximation to the nearest hundredth of one percent."

Any help on where I am going wrong with the code below would be much appreciated. I can post the working for-loop version as well if it would help. The only code not included are the static method calls that gather the numbers below but since they aren't a problem on the for-loop version, I didn't include them here since it makes the code much longer and less readable. Let me know if having it would help though.

    public static void main(String[] args) {
        SimpleReader in = new SimpleReader1L();
        SimpleWriter out = new SimpleWriter1L();

        //represents our universal physical or mathematical constant. any positive real number
        out.println(
                "First, let's get the double value for our mathematical constant:");
        double mu = getPositiveDouble(in, out);

        //represents our four personal numbers (not equal to 1) for da jager formula
        out.println(
                "Now we need four numbers with personal significance to you "
                        + "(doubles not equal to 1.0):");
        double w = getPositiveDoubleNotOne(in, out);
        double x = getPositiveDoubleNotOne(in, out);
        double y = getPositiveDoubleNotOne(in, out);
        double z = getPositiveDoubleNotOne(in, out);

//these are the 17 numbers as exponents for second part of da jager formula
double[] charmingTheory = { -5, -4, -3, -2, -1, -1 / 2, -1 / 3, -1 / 4,
        0, 1 / 4, 1 / 3, 1 / 2, 1, 2, 3, 4, 5 };

//initialize counters for our four loops
int i = 0;
int j = 0;
int k = 0;
int l = 0;

//starting estimate
double closestEstimate = 0;

while (i < charmingTheory.length) {
    double num1 = Math.pow(w, charmingTheory[i]);
    out.println(num1);
    i++;
    while (j < charmingTheory.length) {
        double num2 = Math.pow(x, charmingTheory[j]);
        j++;
        while (k < charmingTheory.length) {
            double num3 = Math.pow(y, charmingTheory[k]);
            k++;
            while (l < charmingTheory.length) {
                double num4 = Math.pow(z, charmingTheory[l]);
                double estimate = num1 * num2 * num3 * num4;
                if (Math.abs(mu - estimate) < Math
                        .abs(mu - closestEstimate)) {
                    closestEstimate = estimate;
                }
                l++;
            }
        }
    }
}
out.println("Closest Estimate is:");
out.println(closestEstimate);

Upvotes: 0

Views: 3042

Answers (1)

RaffleBuffle
RaffleBuffle

Reputation: 5455

You aren't resetting the values of the j,k,l loop counters at the start of each while loop. They therefore only run once, the first time through the outer i loop. You need to update the start of each while loop to look like this:

j=0;
while (j < charmingTheory.length)
   //....
   k=0;
   while (k < charmingTheory.length)
     //....
     l=0;
     while (l < charmingTheory.length)

I suspect the reason this was working with for loops is that you were doing this:

for(int j=0; j < charmingTheory.length; j++)

Upvotes: 0

Related Questions