-1

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.

Sathish
  • 3,740
  • 1
  • 17
  • 28
Akkyen
  • 83
  • 1
  • 6
  • 1
    Take delta=0.01 and try to implement the formula you gave. – Étienne Jul 31 '14 at 07:19
  • 2
    Suppose you wanted to approximate the derivative of a function by hand. How would you do that? – user2357112 Jul 31 '14 at 07:19
  • What does your function look like? You say its just X^1 which seems to imply it is a linear function `a x + b`. If so the derivative is just `a`. You can prove this from first principles putting the values `x0+∆x` and `x0` into the equation. – Salix alba Jul 31 '14 at 08:17
  • It has to be able to work with all kind of functions so there is no specific one – Akkyen Jul 31 '14 at 08:19

2 Answers2

1

The idea is approximate the first derivative of f() at x with the slope of the secant line through the points (x, f(x)) and (x+∆x, f(x+∆x)).

The Wikipedia article should get you started.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

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;
}
chouaib
  • 2,763
  • 5
  • 20
  • 35
  • I once had it like that but thought it was wrong because I had no mathematical reference but I just found a reference thanks to NPEs link and thanks to you I had a code reference. Now I also understand what I did wrong in the first place besides from the wrong description. And I would really like to give both of you a correct answer but so I will have to flip a coin :D – Akkyen Jul 31 '14 at 08:56
  • @chouaib Do you maybe also know how to make it so that it also works for the second derivation? – Akkyen Jul 31 '14 at 09:38
  • @Akkyen I used `(f(x+h)-f(x))/h` for first derivative, you can use `(f(x+h) - 2f(x) + f(x-h))/h*h` for second order and so on ... – chouaib Jul 31 '14 at 10:30
  • @duffymo: no it is not wrong! I said this is a **SAMPLE** not a **SOLUTION** so I can use any sample function to demonstrate the way to solve it. after that it's up to him to understand and change what's necissary to have his desired solution – chouaib Jul 31 '14 at 10:32
  • Since he explicitly asked for 2*x*x+1, I assumed that you'd follow suit with his example. A student who fails to understand this simple idea might be silly enough to cut & paste code without looking at it. I understand your point, though. – duffymo Jul 31 '14 at 12:40