The derivative function is not working as intended , logically

84 Views Asked by At

In my code, the case 4 of the switch is not working: it takes a funtion as f/g and then derivates it numerically,

case 4 takes in input from the user and then applies the functions of derivative I have used, but its not working as intended. I think the error is in the usage of pointers. could someone help fix this?

I was expecting the derivative but it does not derivate it.

Here is the code:

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

#define MAX 100

void display_polynomial(int *terms, double coefficient[], int *degree);
void derivative_polynomial(int *terms, double coefficient[], int degree[]);
void integration(int *terms, double coefficient[], int degree[]);
void input_of_polynomials(int *terms, double coefficient[], int degree[]);
void quadratic_equation();
void derivative_divide();
double function_divide(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd);
double derivative_divide_functions(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd);

int main(void)
{
    double x;
    double degree_;

    int choice;
    int terms;
    int degree[100];
    double coefficient[100];

    int term_fx, term_gx;
    double coefficient_fx1[MAX], coefficient_gx1[MAX];
    int degree_fx1[MAX], degree_gx2[MAX];

    double  xd;
    double coefficient_fx[MAX + 1], coefficient_gx[MAX + 1];
    int degree_fx, degree_gx;

    printf("|************************************************************************************************************|\n");
    printf("|\t\t        Polynomial Operations                                                                           |\n");
    printf("|************************************************************************************************************|\n");
    printf("|\t\t        Choose an Operation:                                                                            |\n");
    printf("| 1. Calculate Derivative                                                                                    |\n");
    printf("| 2. Perform Integration                                                                                     |\n");
    printf("| 3. Solve Quadratic Equation                                                                                |\n");
    printf("| 4. Calculate Derivative of Type f(x) / g(x)                                                                |\n");
    printf("|************************************************************************************************************|\n");
    printf("  Your Choice: ");

    scanf_s("%d", &choice);

    switch (choice)
    {
    case 1:
        {
            input_of_polynomials(&terms, coefficient, degree);
            printf("f(x)= ");
            display_polynomial(&terms, coefficient, degree);
            printf("\n\nf`(x)=");
            derivative_polynomial(&terms, coefficient, degree);
            printf("\n");
            break;
        }
    case 2:
        {
            input_of_polynomials(&terms, coefficient, degree);;
            printf("f(x)= ");
            display_polynomial(&terms, coefficient, degree);
            printf("\n\nIntegral = ");
            integration(&terms, coefficient, degree);
            printf("\n");
            break;
        }
    case 3:
        {
            quadratic_equation();
            break;
        }
    case 4:
        {
            printf("Enter Value of X: ");
            double x;
            scanf_s("%lf", &x);

            // Input for the first polynomial (f(x))
            int term_fx;
            double coefficient_fx[MAX];
            int degree_fx[MAX];
            input_of_polynomials(&term_fx, coefficient_fx, degree_fx);

            // Input for the second polynomial (g(x))
            int term_gx;
            double coefficient_gx[MAX];
            int degree_gx[MAX];
            input_of_polynomials(&term_gx, coefficient_gx, degree_gx);

            // Display the input polynomials
            printf("f(x)/g(x) = \n\n");
            display_polynomial(&term_fx, coefficient_fx, degree_fx);
            printf("\n------------\n");
            display_polynomial(&term_gx, coefficient_gx, degree_gx);
            printf("\n");

            // Calculate the derivative using derivative_divide_functions
            double derivative_result = derivative_divide_functions(coefficient_fx, degree_fx[0], coefficient_gx, degree_gx[0], x);
            printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.2lf\n", x, derivative_result);

            break;
        }
    default:
        {
            printf("Invalid Input \n");
        }
        break;
    }
}

void display_polynomial(int *terms, double coefficient[], int degree[])
{
    for (int i = 0; i < *terms; i++)
    {
        if (*(coefficient + i) == 0)
        {
            continue;
        }
        else if (*(coefficient + i) > 0)
        {
            if (i != 0)
            {
                printf(" + ");
            }
        }
        else if (*(coefficient + i) < 0)
        {
            printf(" - ");
        }

        if (*(degree + i) == 0)
        {
            printf("%.1lf", fabs(*(coefficient + i)));
        }
        else if (*(degree + i) == 1)
        {
            if (fabs(*(coefficient + i)) != 1)
            {
                printf("%.1lf", fabs(*(coefficient + i)));
            }
            printf("x");
        }
        else
        {
            printf("%.1lf", fabs(*(coefficient + i)));
            printf("x^%d", *(degree + i));
        }
    }
}

void derivative_polynomial(int *terms, double coefficient[], int degree[])
{
    for (int i = 0; i < *terms; i++)
    {
        *(coefficient + i) = *(coefficient + i) * *(degree + i);
        *(degree + i) = *(degree + i) - 1;
    }
    display_polynomial(terms, coefficient, degree);
}

void integration(int *terms, float coefficient[], int degree[])
{
    for (int i = 0; i < *terms; i++)
    {
        *(degree + i) = *(degree + i) + 1;
        *(coefficient + i) = *(coefficient + i) / *(degree + i);
    }
    display_polynomial(terms, coefficient, degree);
    printf(" + C \n");
}

void input_of_polynomials(int *terms, double coefficient[], int degree[]) {
    printf("Enter Number of Terms in the polynomial: ");
    scanf_s("%d", terms);

    for (int i = 0; i < *terms; i++) {
        printf("Enter Co-Efficient of Term %d: ", i + 1);
        scanf_s("%lf", &coefficient[i]);

        printf("Enter Degree of Term %d: ", i + 1);
        scanf_s("%d", &degree[i]);
        printf("\n");
    }
}

void quadratic_equation()
{
    double root1, root2;
    double a, b, c;

    printf("     QUADRATIC ROOTS CALCULATOR   \n\n");

    printf("Enter the Co-Efficient of x^2 (a) \n");
    scanf_s("%lf", &a);
    printf("Enter the Co-Efficient of x (b) \n");
    scanf_s("%lf", &b);
    printf("Enter the Constanct (c) \n");
    scanf_s("%lf", &c);

    double d = sqrt((b * b - (4 * a * c)));

    if (d == 0)
    {
        printf("\t\t Roots Are Real And Equal\n");

        root1 = root2 = -(b / 2 * a);
        printf("Roots of the Equation\t  ( %.1lfx^2 + %.1lfx + %.1lf ) Are :\n\t Root 1 : %.1lf \n\t Root 2 : %.1lf", a, b, c, root1, root2);
    }
    else if (d > 0)
    {

        root1 = (-b + d) / (2 * a);
        root2 = (-b - d) / (2 * a);
        printf("Roots Are Real and Distinct \n");
        printf(" Roots of the Equation %.1lfx^2 + %.1lfx + %.1lf Are :\n\t Root 1 : %.1lf \tRoot2 :   %.1lf", a, b, c, root1, root2);

    }
    else
    {
        printf("Roots Are Imaginary\n");
    }
}

void derivative_divide() {
    printf("Function of Type f(x) / g(x)\n");

    int term_fx;
    double coefficient_fx[MAX];
    int degree_fx[MAX];

    input_of_polynomials(&term_fx, coefficient_fx, degree_fx);

    int term_gx;
    double coefficient_gx[MAX];
    int degree_gx[MAX];

    input_of_polynomials(&term_gx, coefficient_gx, degree_gx);

    printf("Function is:\n");
    display_polynomial(&term_fx, coefficient_fx, degree_fx);
    printf("\n-----\n");
    display_polynomial(&term_gx, coefficient_gx, degree_gx);

    // Calculate and display the derivative
    double x_value;
    printf("Enter the value of x for the derivative: ");
    scanf_s("%lf", &x_value);

    double derivative_result = derivative_divide_functions(coefficient_fx, degree_fx[0], coefficient_gx, degree_gx[0], x_value);
    printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.2lf\n", x_value, derivative_result);
}

double function_divide(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd) {
    double result_fx = 0.0, result_gx = 0.0;

    // Calculate f(x)
    for (int i = degree_fx; i >= 0; i--) {
        result_fx += coefficient_fx[i] * pow(xd, i);
    }

    // Calculate g(x)
    for (int i = degree_gx; i >= 0; i--) {
        result_gx += coefficient_gx[i] * pow(xd, i);
    }

    double result_divide_function = result_fx / result_gx;
    return result_divide_function;
}

double derivative_divide_functions(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd) {
    double const h = 1.0e-6;

    // Calculate f(x + h)
    double fx_plus_h = function_divide(coefficient_fx, degree_fx, coefficient_gx, degree_gx, xd + h);

    // Calculate f(x)
    double fx = function_divide(coefficient_fx, degree_fx, coefficient_gx, degree_gx, xd);

    // Calculate the derivative
    double derivative = (fx_plus_h - fx) / h;

    return derivative;
}
2

There are 2 best solutions below

0
Eric Postpischil On

You mishandled the polynomials.

input_of_polynomials fills the objects pointed to by its parameters terms, coefficient, and degree with the number of terms in a polynomial, a list of the coefficients of the terms, and a list of the degrees of the terms. In case 4, after getting polynomials that way, you pass to derivative_divide_functions the list of coefficients and the degree of the first item (array index zero) in the list of degrees. The number of terms and the degrees after the first are omitted.

derivative_divide_functions passes these to function_divide, which uses the degree d in a loop that expects the coefficients to be a list of the coefficients for the term with degree d, then d−1, then d−2, and so on down to degree zero. That is an entirely different representation of a polynomial than input_of_polynomials used.

You have to rewrite the code to use the same representation of polynomials throughout.

0
LORDTEK On

Your integration function declared as

int *, double [], int []

But you used it as:

int *, float [], int []

I changed that line of your code and put below:

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

#define MAX 100

void display_polynomial (int *terms, double coefficient[], int *degree);
void derivative_polynomial (int *terms, double coefficient[], int degree[]);
void integration (int *terms, double coefficient [], int degree []);
void input_of_polynomials (int *terms, double coefficient[], int degree[]);
void quadratic_equation ();
void derivative_divide ();
double function_divide (double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd);
double derivative_divide_functions (double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd);

int main (void) {
    double x;
    double degree_;

    int choice;
    int terms;
    int degree [100];
    double coefficient [100];

    int term_fx, term_gx;
    double coefficient_fx1 [MAX], coefficient_gx1 [MAX];
    int degree_fx1[MAX], degree_gx2[MAX];

    double  xd;
    double coefficient_fx [MAX + 1], coefficient_gx [MAX + 1];
    int degree_fx, degree_gx;

    printf("|************************************************************************************************************|\n");
    printf("|\t\t        Polynomial Operations                                                                           |\n");
    printf("|************************************************************************************************************|\n");
    printf("|\t\t        Choose an Operation:                                                                            |\n");
    printf("| 1. Calculate Derivative                                                                                    |\n");
    printf("| 2. Perform Integration                                                                                     |\n");
    printf("| 3. Solve Quadratic Equation                                                                                |\n");
    printf("| 4. Calculate Derivative of Type f(x) / g(x)                                                                |\n");
    printf("|************************************************************************************************************|\n");
    printf ("  Your Choice: ");

    scanf_s ("%d", &choice);

    switch (choice) {
    case 1: {
            input_of_polynomials(&terms, coefficient, degree);
            printf("f(x)= ");
            display_polynomial(&terms, coefficient, degree);
            printf("\n\nf`(x)=");
            derivative_polynomial(&terms, coefficient, degree);
            printf("\n");
            break;
        }
    case 2: {
            input_of_polynomials(&terms, coefficient, degree);;
            printf("f(x)= ");
            display_polynomial(&terms, coefficient, degree);
            printf("\n\nIntegral = ");
            integration(&terms, coefficient, degree);
            printf("\n");
            break;
        }
    case 3: {
            quadratic_equation();
            break;
        }
    case 4: {
            printf("Enter Value of X: ");
            double x;
            scanf_s("%lf", &x);

            // Input for the first polynomial (f(x))
            int term_fx;
            double coefficient_fx[MAX];
            int degree_fx[MAX];
            input_of_polynomials(&term_fx, coefficient_fx, degree_fx);

            // Input for the second polynomial (g(x))
            int term_gx;
            double coefficient_gx[MAX];
            int degree_gx[MAX];
            input_of_polynomials(&term_gx, coefficient_gx, degree_gx);

            // Display the input polynomials
            printf("f(x)/g(x) = \n\n");
            display_polynomial(&term_fx, coefficient_fx, degree_fx);
            printf("\n------------\n");
            display_polynomial(&term_gx, coefficient_gx, degree_gx);
            printf("\n");

            // Calculate the derivative using derivative_divide_functions
            double derivative_result = derivative_divide_functions(coefficient_fx, degree_fx[0], coefficient_gx, degree_gx[0], x);
            printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.2lf\n", x, derivative_result);

            break;
        }
    default: {
            printf("Invalid Input \n");
        }
        break;
    }
}

void display_polynomial (int *terms, double coefficient[], int degree[]) {
    for (int i = 0; i < *terms; i++) {
        if (*(coefficient + i) == 0) {
            continue;
        }
        else if (*(coefficient + i) > 0) {
            if (i != 0) {
                printf(" + ");
            }
        }
        else if (*(coefficient + i) < 0) {
            printf(" - ");
        }

        if (*(degree + i) == 0) {
            printf("%.1lf", fabs(*(coefficient + i)));
        }
        else if (*(degree + i) == 1) {
            if (fabs(*(coefficient + i)) != 1) {
                printf("%.1lf", fabs(*(coefficient + i)));
            }
            printf("x");
        }
        else {
            printf("%.1lf", fabs(*(coefficient + i)));
            printf("x^%d", *(degree + i));
        }
    }
}

void derivative_polynomial (int *terms, double coefficient[], int degree[]) {
    for (int i = 0; i < *terms; i++) {
        *(coefficient + i) = *(coefficient + i) * *(degree + i);
        *(degree + i) = *(degree + i) - 1;
    }
    display_polynomial(terms, coefficient, degree);
}

void integration (int *terms, double coefficient [], int degree []) {
    for (int i = 0; i < *terms; i++) {
        *(degree + i) = *(degree + i) + 1;
        *(coefficient + i) = *(coefficient + i) / *(degree + i);
    }
    display_polynomial (terms, coefficient, degree);
    printf (" + C \n");
}

void input_of_polynomials (int *terms, double coefficient[], int degree[]) {
    printf("Enter Number of Terms in the polynomial: ");
    scanf_s("%d", terms);

    for (int i = 0; i < *terms; i++) {
        printf("Enter Co-Efficient of Term %d: ", i + 1);
        scanf_s("%lf", &coefficient[i]);

        printf("Enter Degree of Term %d: ", i + 1);
        scanf_s("%d", &degree[i]);
        printf("\n");
    }
}

void quadratic_equation() {
    double root1, root2;
    double a, b, c;

    printf("     QUADRATIC ROOTS CALCULATOR   \n\n");

    printf("Enter the Co-Efficient of x^2 (a) \n");
    scanf_s("%lf", &a);
    printf("Enter the Co-Efficient of x (b) \n");
    scanf_s("%lf", &b);
    printf("Enter the Constanct (c) \n");
    scanf_s("%lf", &c);

    double d = sqrt((b * b - (4 * a * c)));

    if (d == 0) {
        printf("\t\t Roots Are Real And Equal\n");

        root1 = root2 = -(b / 2 * a);
        printf("Roots of the Equation\t  ( %.1lfx^2 + %.1lfx + %.1lf ) Are :\n\t Root 1 : %.1lf \n\t Root 2 : %.1lf", a, b, c, root1, root2);
    }
    else if (d > 0) {

        root1 = (-b + d) / (2 * a);
        root2 = (-b - d) / (2 * a);
        printf("Roots Are Real and Distinct \n");
        printf(" Roots of the Equation %.1lfx^2 + %.1lfx + %.1lf Are :\n\t Root 1 : %.1lf \tRoot2 :   %.1lf", a, b, c, root1, root2);

    }
    else {
        printf("Roots Are Imaginary\n");
    }
}

void derivative_divide() {
    printf("Function of Type f(x) / g(x)\n");

    int term_fx;
    double coefficient_fx[MAX];
    int degree_fx[MAX];

    input_of_polynomials(&term_fx, coefficient_fx, degree_fx);

    int term_gx;
    double coefficient_gx[MAX];
    int degree_gx[MAX];

    input_of_polynomials(&term_gx, coefficient_gx, degree_gx);

    printf("Function is:\n");
    display_polynomial(&term_fx, coefficient_fx, degree_fx);
    printf("\n-----\n");
    display_polynomial(&term_gx, coefficient_gx, degree_gx);

    // Calculate and display the derivative
    double x_value;
    printf("Enter the value of x for the derivative: ");
    scanf_s("%lf", &x_value);

    double derivative_result = derivative_divide_functions(coefficient_fx, degree_fx[0], coefficient_gx, degree_gx[0], x_value);
    printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.2lf\n", x_value, derivative_result);
}

double function_divide (double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd) {
    double result_fx = 0.0, result_gx = 0.0;

    // Calculate f(x)
    for (int i = degree_fx; i >= 0; i--) {
        result_fx += coefficient_fx[i] * pow(xd, i);
    }

    // Calculate g(x)
    for (int i = degree_gx; i >= 0; i--) {
        result_gx += coefficient_gx[i] * pow(xd, i);
    }

    double result_divide_function = result_fx / result_gx;
    return result_divide_function;
}

double derivative_divide_functions (double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd) {
    double const h = 1.0e-6;

    // Calculate f(x + h)
    double fx_plus_h = function_divide(coefficient_fx, degree_fx, coefficient_gx, degree_gx, xd + h);

    // Calculate f(x)
    double fx = function_divide(coefficient_fx, degree_fx, coefficient_gx, degree_gx, xd);

    // Calculate the derivative
    double derivative = (fx_plus_h - fx) / h;

    return derivative;
}