Question

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();
}
Était-ce utile?

La solution

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])

Then D is your array of derivatives:

double D[] = new double[C.length-1];
for(int i = 0; i < C.length-1; i++)
   D[i] = C[i]*(C.length-i-1);

Autres conseils

Suppose your polynomial is like this,

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]

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 );
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top