Search code examples
cmathnumerical-methodsderivativedifferentiation

How to calculate the differential coefficient in c


Thanks a lot people for your help so far but I made a big mistake I need the derivation of a function at a specific point!

I have to calculate the first derivation of a function and I really have no clue how to get there. If I just had to calculate it for a function with just a X^1 I would know how to but I'm really stuck here.

Old Stuff: A function can look like 2*x^2+1.

The method has to look like this: double ab(double (f)(double),double x) and my professor gave us the hint that we might should use the function: (f(x0+∆x)−f(x0))/((x0+∆x)−x0).

Sorry for my bad English and thanks for any kind of hint or tip in advance.


Solution

  • this sample will get you started :

    #include<stdio.h>
    #include <stdlib.h>
    
    
    float func(float x)
    {
        return(2*x*x + 1);
    }
    
    int main(){
        float h=0.01;
        float x;
        float deriv, second;
    
        printf("Enter x value: ");
        scanf("%f", &x);
        // derivative at x is the slope of infinitely small
        // line of the function 
    
        deriv = (func(x+h) - func(x))/h; // I assumed the length to be h
    
        //for second derivative you can use:
        second = (func(x+h) - 2*func(x) + func(x-h))/(h*h);
    
        printf("%f\n", deriv);
        return 0;
    }