Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// Online C compiler to run C program online
// Online C compiler to run C program online
/*emphasized text*following is my code to find derivates of a 3 degree polynomial function
My answer for 2 and 3 derivates are wrong can you plz tell me my mistake */
#include <stdio.h>
#include <math.h>

float poly(float a[], int, float);
float deriv(float a[], int, float);
float deriv2(float a[], int, float);
float deriv3(float a[], int, float);

int main()
{
    float x, a[10], y1, dy1,dy2,dy3;
    int deg, i,degg,deggg;

    printf("Enter the degree of polynomial equation: ");
    scanf("%d", °);

    printf("Ehter the value of x for which the equation is to be evaluated: ");
    scanf("%f", &x);

    for (i = 0; i <= deg; i++) {
        printf("Enter the coefficient of x to the power %d: ", i);
        scanf("%f", &a[i]);
    }

    y1 = poly(a, deg, x);
    dy1 = deriv(a, deg, x);
     dy2 = deriv2(a, degg, x);
     dy3 = deriv3(a, deggg, x);

    printf("The value of polynomial equation for the value of x = %.2f is: %.2f", x, y1);
    printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy2);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f", x, dy3);

    return 0;
}

/* function for finding the value of polynomial at some value of x */

float poly(float a[], int deg, float x)
{
    float p;
    int i;

    p = a[deg];

    for (i = deg; i >= 1; i--) {
        p = (a[i - 1] + x * p);
    }

    return p;
}

/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
    float d[10], pd = 0, ps;
    int i;

    for (i = 0; i <= deg; i++) {
        ps = pow(x, deg - (i + 1));
        d[i] = (deg - i) * a[deg - i] * ps;
        pd = pd + d[i];
    }

    return pd;
}
float deriv2(float b[],int deg,float x)
{float c[10],a[10];
int j,degg;

for (j=degg;j>=0;j--)
{
if (b[j]>0)
{
c[j]=a[j]*b[j];
b[j]=b[j]-1;}
else {
break;
}
} degg=deg-1;
}

float deriv3(float c[],int deg,float x)

{float d[10],q[10],b[10];
int k,deggg,degg;

for (k=deggg;k>=0;k--)
{
if (b[k]>0)
{
q[k]=c[k]*b[k];
b[k]=b[k]-1; }
else { 
    break;}
  
}deggg=degg-1;
}


What I have tried:

The program I have made is written above
Posted
Updated 23-Jul-22 0:25am

1 solution

Why don't you accept the suggestions from the previous posts first? Currently I can't compile the program and it can't run like this.
error C3873: "0xb0": This character is not allowed as the first character of an identifier; error C2065: "°": undeclared identifier
scanf("%d", °);

Also an already discussed bug:
warning C4700: The uninitialized local variable "degg" was used.
warning C4700: The uninitialized local variable "deggg" was used.
error C4716: "deriv2": Must return a value

The errors in deriv2() and deriv3() have already been discussed there. Please read and consider the solutions. So far, most solutions have also lacked feedback in the form of comments or stars. All solutions that have helped you to solve your problem should be accepted accordingly.

see also:
www.codeproject.com/Questions/5337982/Correction-of-error-ln-C-program
www.codeproject.com/Questions/5337808/How-can-I-correct-the-degree-error

You currently have two functions:
float poly(float a[], int deg, float x);

and
float deriv(float a[], int deg, float x);

However, it would make more sense to calculate the result of the polynomial calculation with poly() and with the deriv() function derive the polynomial and then use poly() to calculate the value for it. The question of deriv2 and 3 would then not arise at all and it would also work for polynomials of any degree.
 
Share this answer
 
v3
Comments
CPallini 23-Jul-22 7:16am    
5.
Patrice T 23-Jul-22 7:20am    
It is much simpler if someone do the job and give full solution all cooked :)
merano99 23-Jul-22 7:54am    
Yes, unfortunately, but the misery must come to an end at some point.
Patrice T 23-Jul-22 7:57am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900