views:

92

answers:

2

I have some function,

int somefunction( //parameters here, let's say  int x) {
    return something  Let's say x*x+2*x+3 or does not matter
}

How do I find the derivative of this function? If I have

int f(int x) {
    return sin(x);
}

after derivative it must return cos(x).

+1  A: 

You can get the numerical integral of mostly any function using one of many numerical techniques such as Numerical ordinary Differential Equations

Look at: Another question

But you can get the integration result as a function definition with a library such as Maple, Mathematica, Sage, or SymPy

Betamoo
@Betamoo: I believe he is talking about a 'black box' function f (i.e. you don't know what it is). If you can evaluate f, can you evaluate the derivative of f at certain point, is the question then, I believe.
Moron
+4  A: 

You can approximate the derivative by looking at the gradient over a small interval. Eg

const double DELTA=0.0001;
double dfbydx(int x) {
  return (f(x+DELTA) - f(x)) / DELTA;
}

Depending on where you're evaluating the function, you might get better results from (f(x+DELTA) - f(x-DELTA)) / 2*DELTA instead.

(I assume 'int' in your question was a typo. If they really are using integers you might have problems with precision this way.)

Andy Mortimer
aaa thanks Andy then i should use float or double yes?it will be more correct
@davit yes, when you start looking at small ranges like this you'll need to use floats or doubles. Your original functions are not incorrect for some uses, but they are not precise enough to get derivatives from.
Andy Mortimer
@Andy thanks for example if we want to write program which uses Newtons method or something like this i want to learn how implement derivatives in programming because sometimes it is necessary find maximum of function open some interval or like this things
+1. Use the definition of derivative!
Moron