0
votes

I am working on a little program to calculate and draw a parabola. But I'm stuck at a little part in the program where I need to calculate the valuetable.

The quadratic function is the following one: y = a * (x - alpha)² + Beta.

So if I fill in the following values for this function: y = 2 * (x - 1)² + (-12). I need to become this value table:

x | -4.0 | -3.0 | -2.0 | -1.0 | 0.0 | 1.0 | 2.0 | 3.0 | 4.0 |

y | 38.0 | 20.0 | 6.0 | -4.0 |-10.0|-12.0|-10.0|-4.0 | 6.0 |

I managed to get the first row right, but the second row (the one that calculates te values), is totally wrong.

    public double[][] berekenWaardentabel(int input[], int AANTALKOLOMMEN, double startWaarde) {
    double[][] waarden = new double[RIJEN][AANTALKOLOMMEN];

    System.out.println(waarden.length);
    System.out.println(startWaarde + "\n");

    for(int i = 0; i < RIJEN; i++) {
        for(int j = 0; j < AANTALKOLOMMEN; j++) {
            waarden[0][j] = startWaarde++; //value of first row (start from -4.0, counts upwards) 
            waarden[1][j] = input[0] * (Math.pow((startWaarde - input[1]), 2)) + input[2];
            System.out.print(waarden[i][j] + "   ");
        }
        System.out.println();
    }
    return waarden;
}

This is the output I get:

-4.0 -3.0 -2.0 -1.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0

86.0 | 116.0 | 150.0 | 188.0 | 230.0 | 276.0 | 326.0 | 380.0 | 438.0 | 500.0 | 566.0

Anyone an idea how to solve this problem? Thanks!

2
Try debugging your code. You should be able to see step-by-step what's happening with the content of your variables. - Michaël Benjamin Saerens
startWaarde should be reinitialized to -4 in your 1st loop, otherwise you fill the second row starting with 8 instead of -4. - StephaneM
I've managed to find the answer to calculate right, but I still have one little problem. The solution of each calculation isn't in the correct order yet. I get ROW1: -4.0 | -3.0 | -2.0 | -1.0 | 0.0 | 1.0 | 2.0 | 3.0 | 4.0 | 5.0 | 6.0. ROW2: 20.0 | 6.0 | -4.0 | -10.0 | -12.0 | -10.0 | -4.0 | 6.0 | 20.0 | 38.0 | 60.0. While -4.0 equals 38, and -3.0 is 20. So they need to move 1 to be in the right position. - Lorenzo
You increment startwaarde here: waarden[0][j] = startWaarde++;, you should do it on the next line. - StephaneM
Okay it works properly now! Thanks! - Lorenzo

2 Answers

1
votes

You have to mistakes :

you want to fill two lines of your array (the 0-th (x) and the first(y)). Then you store the value of x in the first row and compute y(x+1) in the second row.

you need to fill your array with :

for (int j = 0; j < AANTALKOLOMMEN; j++) {
    waarden[0][j] = startWaarde; // value of first row (start from -4.0, counts upwards)
    waarden[1][j] = input[0] * (Math.pow((startWaarde - input[1]), 2)) + input[2];
    startWaarde++;
}

Then you can display your values :

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < AANTALKOLOMMEN; j++) {
        System.out.print(waarden[i][j] + "   ");
    }
    System.out.println();
}
0
votes

First error is this:

        waarden[0][j] = startWaarde++; //value of first row (start from -4.0, counts upwards) 
        waarden[1][j] = input[0] * (Math.pow((startWaarde - input[1]), 2)) + input[2];

You put the value of startWaarde in waarden[0][j]. And then you increment it by one.

Then you calculate the value of the function for the updated startWaarde.

So your waarden[0][j] is, perhaps, 4.0, but you calculate the value waarden[1][j] for 5.0 rather than 4.0.

Your other problem is that you are looping with this line:

for(int i = 0; i < RIJEN; i++) {

I don't know what the value of RIJEN is. But your waarden array is only supposed to have two rows. Once you go through the loop again and again, startWaarde keeps growing and growing. Because you print the second row only after this loop iterates the second time, you see the wrong values.

You shouldn't have this loop at all.