QwertyKing
QwertyKing

Reputation: 64

How can I get Bisection Method to print out all roots of a polynomial up to 5 exponents

I am doing a bisection method program where you input the coefficients up to the power of 5 and I find every root of the polynomial. My code only prints out the first root of the polynomial. How do I make it keep looking for other roots? Here is my code

public void bisectionMethod(double a, double b) {
        double average;
        double yOfC;
        double [] roots;
        int size = 1;
        while (size <= 5) {
            average = (a + b) / 2;
            yOfC = calculateY(average);
            if (Math.abs(yOfC) < 0.001) {
                System.out.println(average);
                size++;
            } else if (yOfC * calculateY(a) > 0) {
                a = average;
            } else {
                b = average;
            }
        }
    }

This is my output

-0.9999990463256836
-0.9999990463256836
-0.9999990463256836
-0.9999990463256836
-0.9999990463256836

Upvotes: 0

Views: 431

Answers (1)

M.S.
M.S.

Reputation: 1091

This is more of a mathematical problem than a programming question.

Given two points in a continuous function f, you are guaranteed to find a root between points a and b if f(a)*f(b)<0 (i.e. one value is positive and the other negative). However, this only guarantees one root.

To find another root, if it exists, a different interval must be chosen. However, you will not know until you calculate the root whether this new interval yields a new root. If interested in other root-finding algorithms I would suggest you read about them here.

Upvotes: 1

Related Questions