在java中使用数组查找多项式的导数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21792400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the derivative of a polynomial using arrays in java
提问by user3312298
I am new to programming and I have trouble trying to program the derivative of a polynomial using arrays. Below is what I have to get the user's input.
我是编程新手,在尝试使用数组对多项式的导数进行编程时遇到了麻烦。以下是我必须获得用户输入的内容。
Scanner sc=new Scanner(System.in);
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();
}
采纳答案by jkschneider
Let's assume that the array C
contains the coefficients of an n-th degree polynomial in descending order of degree (e.g. for f(x) = C[0]*x^n + ... + C[n-1]*x + C[n]
)
让我们假设该数组C
包含按度数降序排列的第 n 次多项式的系数(例如 for f(x) = C[0]*x^n + ... + C[n-1]*x + C[n]
)
Then D
is your array of derivatives:
然后D
是您的衍生品数组:
double D[] = new double[C.length-1];
for(int i = 0; i < C.length-1; i++)
D[i] = C[i]*(C.length-i-1);
回答by Tharindu Kumara
Suppose your polynomial is like this,
假设你的多项式是这样的,
f(x) = C[0]*x^n + C[1]*x^(n-1).......C[n]
f(x) = C[0]*x^n + C[1]*x^(n-1).......C[n]
After the derivation, it becomes
推导后就变成
f'(x) = C[0]*(n)x^(n-1) + C[1](n-1)*x^(n-2)+...........+ 0*C[n]
f'(x) = C[0]*(n) x^(n-1) + C[1](n-1)*x^(n-2)+........ .+ 0*C[n]
Scanner sc=new Scanner(System.in);
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();
}
double derivative[] = new double[C.length-1];
for(int i=0;i<derivative.length;i++){
derivative[i] = C[i]*(C.length - 1 -i );
}