Now,given the equation 8*x^4 - 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
-
输入
- The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10); 输出
- For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100. 样例输入
- 2
- 100
- -4
- 样例输出
2.0422
No solution!
关于牛顿迭代的和迭代的:
牛顿迭代法(Newton's method)又称为牛顿-拉夫逊方法(Newton-Raphson method),它是牛顿在17世纪提出的一种在实数域和复数域上近似求解方程的方法。多数方程不存在求根公式,因此求精确根非常困难,甚至不可能,从而寻找方程的近似根就显得特别重要。方法使用函数f(x)的泰勒级数的前面几项来寻找方程f(x) = 0的根。牛顿迭代法是求方程根的重要方法之一,其最大优点是在方程f(x) = 0的单根附近具有平方收敛,而且该法还可以用来求方程的重根、复根。另外该方法广泛用于计算机编程中。
设r是f(x) = 0的根,选取x0作为r初始近似值,过点(x0,f(x0))做曲线y = f(x)的切线L,L的方程为y = f(x0)+f'(x0)(x-x0),求出L与x轴交点的横坐标 x1 = x0-f(x0)/f'(x0),称x1为r的一次近似值。过点(x1,f(x1))做曲线y = f(x)的切线,并求该切线与x轴交点的横坐标 x2 = x1-f(x1)/f'(x1),称x2为r的二次近似值。重复以上过程,得r的近似值序列,其中x(n+1)=x(n)-f(x(n))/f'(x(n)),称为r的n+1次近似值,上式称为牛顿迭代公式。
解非线性方程f(x)=0的牛顿法是把非线性方程线性化的一种近似方法。把f(x)在x0点附近展开成泰勒级数 f(x) = f(x0)+(x-x0)f'(x0)+(x-x0)^2*f''(x0)/2! +… 取其线性部分,作为非线性方程f(x) = 0的近似方程,即泰勒展开的前两项,则有f(x0)+f'(x0)(x-x0)=f(x)=0 设f'(x0)≠0则其解为x1=x0-f(x0)/f'(x0) 这样,得到牛顿法的一个迭代序列:x(n+1)=x(n)-f(x(n))/f'(x(n))。
//此函数是用来求3元一次方程ax^3+bx^2+cx+d=0的解
//比如 x^3-27=0,我们就可以输入1 0 0 -27,这样我们就可以得到一个解
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double diedai(double a,double b,double c,double d,double x);
double a,b,c,d;
double x=10000.0;
cout<<"请依次输入方程四个系数:";
cin>>a>>b>>c>>d;
x=diedai(a,b,c,d,x);
cout<<x<<endl;
return 0;
}
double diedai(double a,double b,double c,double d,double x)
{
while(abs(a*x*x*x+b*x*x+c*x+d)>0.000001)
{
x=x-(a*x*x*x+b*x*x+c*x+d)/(3*a*x*x+2*b*x+c);
}
return x;
}
ac代码:
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
double m;
double newton(double x)
{
int z=1;
while(fabs(8*x*x*x*x-7*x*x*x+2*x*x+3*x+6-m)>0.000001)
{
x=x-(8*x*x*x*x-7*x*x*x+2*x*x+3*x+6-m)/(32*x*x*x-21*x*x+4*x+3);
z++;
if(z>30) return 0;
//如果迭代30次以上就认为没有解跳出
}
return x;
}
int main()
{
int n,flag; double u,k;
scanf("%d",&n);
while(n--)
{
scanf("%lf",&m);
flag=0;
for(u=0.0;u<100;u++)
{
k=newton(u);
if(k&&k>=0.0&&k<=100)
{
flag=1;break;
}
}
if(flag==1) printf("%.4lf\n",k);
else printf("No solution!\n");
}
return 0;
}
java代码:
import java.util.Scanner;
public class Main {
static double number;
public static double deidai(double x)
{
int count=0;
while(Math.abs(8*x*x*x*x-7*x*x*x+2*x*x+3*x+6-number)>0.000001)
{
x=x-(8*x*x*x*x-7*x*x*x+2*x*x+3*x+6-number)/(double)(32*x*x*x-21*x*x+4*x+3);
count++;
if(count>30)
{
return 999.0;
}
}
return x;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int cases=scanner.nextInt();
while(cases--!=0)
{
number=scanner.nextDouble();
double result = 0;
int flag=0;
for(double u=0.0;u<=100;u+=1.0)
{
result=deidai(u);
if(result>=0.0&&result<=100.0)
{
flag=1;break;
}
}
if(flag==1)
System.out.printf("%.4f\n",result);
else {
System.out.println("No solution!");
}
}
}
}