Perry Chapman
Perry Chapman

Reputation: 35

Evaluate Polynomial Function

Not sure where I'm going wrong in this, and it has to be in my polyEvaluate function, but any help would be appreciated. The problem is coming in the final calculation. I've tried modifying the function but that doesn't seem to fix the issue.

#include <stdio.h>
#include <math.h>

double polyEvaluate(double coefficients[], int order, double x)
{
    int i, j;
    double polyExp[order], polyProd[order];
    double sum;
    double constant = coefficients[order];
    double temp;

    for(i=order-1; i>0; i--){
        polyExp[i]=pow(x, i);
    }

    j=order;
    i=0;

    while (i<j){
        temp=polyExp[i];
        polyExp[i]=polyExp[j];
        polyExp[j]=temp;
        i++;
        j--;
    }

    for(i=0; i<order; i++){
        polyProd[i]=coefficients[i] * polyExp[i];
    }

    for(i=0; i<order; i++) {
        sum+=polyProd[i];
    }

    sum+=constant;

    return(sum);
}

int main(void)
{
    int order, i;
    double input;
    double coefficients[order+1];

    printf("Enter the order of the polynomial: ");
    scanf("%d", &order);

    printf("\nEnter the coefficients: \n");

    for(i=0; i<=order; i++)
    {
        printf(" >> ");
        scanf("%lf", &coefficients[i]);
    }

    printf("\nEnter the value of x: ");
    scanf("%lf", &input);

    printf("\nThe value of the function is: %lf", polyEvaluate(coefficients, order, input));

    return(0);
}

Upvotes: 0

Views: 3217

Answers (2)

M Oehm
M Oehm

Reputation: 29116

Your function to evaluate the polynom is way to complicated. A mathematical representation of the polynom could look like this:

    f(x) = ∑ c[i]∙xⁱ

There's no need to make the constant a special case, it is c[0]∙x⁰. This formula is represented in C as:

    double polyEvaluate(double coeff[], int order, double x)
    {
        double res = 0.0;

        for (int i = 0; i <= order; i++) {
            res += coeff[i] * pow(x, i);
        }

        return res;
    }

This simple code requires that the index of the coefficient refers to the exponent, which is a more natural representation for the evaluation function. If you want the user to enter the coefficients with the large exponents first, this should in my opinion be handled once during the input.

(You reverse of the auxiliary array polyExp, but that's not really needed; it would be enough to access the coefficient with order - i when you sum the terms. Still, it is better to choose a representation that represents your code well in code. With your current code, you create an auxiliary array and reverse it for every evaluation.)

You can evaluate the polynom without the pow function and hence without the math library, just calculate the power of x as you go:

    double polyEvaluate(double coeff[], int order, double x)
    {
        double fact = 1.0;
        double res = 0.0;
        int i;

        for (i = 0; i <= order; i++) {
            res += coeff[i] * fact;
            fact *= x;
        }

        return res;
    }

Upvotes: 3

mfro
mfro

Reputation: 3335

Your main problem is probably the variable length array you have defined as length order + 1 while order doesn't have a value assigned at that point in time.

I'd advise to stick with fixed-length arrays for the time being (just make them long enough).

Upvotes: 1

Related Questions