二分法求解一元多次方程

题意:

对于给定的一个方程:8 * x ^ 4 + 7 * x ^ 3 + 2 * x ^ 2 + 3 * x + 6 == Y;给定一个数Y,求x(0,100)值

分析:

先求函数 f(x)=8 * x ^ 4 + 7 * x ^ 3 + 2 * x ^ 2 + 3 * x + 6的导数,确定导数在定义域内的单调性,然后二分答案

若导数ff(x)>0,则f(x)单调递增;若导数ff(x)<0,则f(x)单调递减;

#include<bits/stdc++.h>
using namespace std;
#define eps 1e-8
double f(double x)
{
    return 8 * pow(x , 4) + 7 * pow(x , 3) + 2 * pow(x , 2) + 3 * x + 6;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        int y;
        cin>>y;
        double l=0,h=100,m;
        while(h-l>eps)
        {
            m=(l+h)/2;
            if(f(m)>y)  h=m;
            else l=m;
        }
        if(f(0)>y||f(100)<y)
            cout<<"No solution!\n"<<endl;
        else printf("%.4lf\n",l);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40507857/article/details/81154842
今日推荐