我是一個初學者,特別是在java編程中,我已經嘗試了很多次,如何在給定範圍內找到多項式的實根。該程序應該找出用戶提供的給定多項式的所有實根。例如,程序運行如下: 輸入度數:3 輸入4個係數:-6 11 -6 1 輸入左右端點:-10 10 找到的根:1.00000找到的根:2.00000根發現在:3.00000。 以下附件是我的程序格式。確定特定範圍內的多項式的實根
import java.util.Scanner;
class Roots{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double resolution=0.01;
double tolerance=0.0000001;
double threshold=0.001;
double roots;
System.out.print("Enter the degree: ");
int degree =sc.nextInt();
System.out.print("Enter "+(degree+1)+" coefficients: ");
double[] C=new double[degree+1];
for(int i=0; i<C.length;i++){
C[i]=sc.nextDouble();
}
System.out.print("Enter the left and right endpoints: ");
double a=sc.nextInt();
double b=sc.nextInt();
if(poly(C,a)*poly(C,b)<0){
roots=findRoot(C,a,b,tolerance);
}
}
}
static double poly(double[] C, double x){
int n=C.length-1;
int K;
double sum=0.0;
for(int i=0;i<n;i++){
sum+=C[i]*(Math.pow((x-i),n));
}
return sum;
}
static double[] diff(double[] C){
int n=C.length-1;
int K;
double[]D=new double[n];
for(int i=0;i<n;i++){
D[i]=C[i]*(n-1);
}
return D;
}
static double findRoot(double[] C, double a, double b, double tolerance){
//loops here
}
}你必須解決
您能否詳細介紹一下問題的由來?有沒有提供任何測試用例?對學位的限制?允許「作弊」,即通過Laguerres方法和通貨膨脹計算包括複數根在內的所有根,然後過濾出間隔內的那些根? – LutzL