A code for finding one root of fifth degree polyno

2019-09-02 05:40发布

问题:

I'm trying to write a code that asks from the user to give 5 coefficients for a 5th-degree polynomial, and it also asks to give a range (two values) that the programs checks if there is a solution in it or not (I'm asked to find only one), and the solution must be an integer, while the coefficients can be floats.

I'm thinking of writing a code that runs over every integer in the range and substitute it in a description of a polynomial than I define, and check if its equal to zero, but I got stuck at deciding how to make the loops.

And another thing, if there are more than one root in the interval that the user enters, then we must print the minimal value of the roots (but I have no direction how to do that either).

I will show you what I wrote so far, and any kind of help would be appreciated:

#include <stdio.h>
#define zero 0.00001

int main()
{
    double a, b, c , d , e , f , y ,  beginning_of_range, end_of_range;

    int x;

    printf("please enter the coefficients of the polynomial:\n");

    scanf("%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e);

    printf("please enter two values to indicate the beginning and end of range:\n");

    scanf("%lf%lf", &beginning_of_range, &end_of_range);

    while (beginning_of_range > end_of_range)
    {
        printf("ERROR: the range you have entered isn't valid, please try again:");

        scanf("%lf%lf", &beginning_of_range, &end_of_range);
    }

    while (beginning_of_range < end_of_range)
    {
        x = beginning_of_range;

        y = a + b*x + c*x*x + d*x*x*x + e*x*x*x*x + f*x*x*x*x*x;

        if (y == zero)
        {
            printf("the root is:%d", x);

        }
        else 
        {
            x = x+1;
        }

        break;

    }

    return 0;
}

回答1:

Your task as written is very questionable, so much so that there is perhaps a mis-interpretation or mis-communication.

A randomly given polynomial with floating point coefficients will have integer roots with probability so close to zero that it is practically impossible.

Even with integer coefficients it requires carefully reverse-engineered coefficients to get an integer root. Just change one coefficient by one and in most cases all roots will be irrational.

What you can do in your framework is to find intervals with a sign change so that there is at least one root inside the interval that can be found using one of bisection, regula falsi, Illinois, secant or Mullers methods. All of them derivative free.

Without identifying all roots, also the complex ones, it is rather hard to guarantee that all real roots have been found. Thus it is only approximately possible to find the smallest real root inside the given interval. There might be an integer interval with two real roots inside so that at the boundaries the sign is the same. You would have to analyse the signs of all derivatives at the integer points to make more reliable guesses in this case, see Descartes rule and Budan-Fourier theorem.



回答2:

Your first while loop should probably be an if statement instead of a loop.

Your second (main) while loop never increments the beginning of range. This is probably causing an endless loop for you.