查找五次多项式一个根的代码 [英] A code for finding one root of fifth degree polynomial

查看:119
本文介绍了查找五次多项式一个根的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码,要求用户给出5次多项式的5个系数,并且还要求给出一个范围(两个值),供程序检查其中是否有解是否(要求我只找到一个),解决方案必须是整数,而系数可以是浮点数.

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;
}

推荐答案

您编写的任务非常有问题,以至于可能存在误解或误解.

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.

即使使用整数系数,也需要仔细地逆向工程系数才能获得整数根.只需将一个系数更改为1,在大多数情况下,所有根都是不合理的.

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.

如果不识别所有根,也不能识别复杂根,则很难保证已找到所有真实根.因此,仅在给定间隔内找到最小的实根几乎是可能的.可能有一个整数区间,里面有两个实根,因此在边界处符号是相同的.在这种情况下,您必须分析所有在整数点处的导数的符号,才能做出更可靠的猜测,请参阅笛卡尔定律和Budan-Fourier定理.

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.

这篇关于查找五次多项式一个根的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆