17

Is it possible to find derivative of a function using c program. I am using matlab in that it has an inbuilt function diff() which can be used for finding derivative of a function.

f(x)=x^2

Is it possible to find the derivative of above function using c. What is the algorithm for that?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Eka
  • 14,170
  • 38
  • 128
  • 212
  • if you want a locale derivative you could use the limit, and get a good approximation. – elyashiv Oct 16 '12 at 13:05
  • The de facto standard reference: http://www.nr.com/ – alk Oct 16 '12 at 14:08
  • Do you want a symbolic or analytic solution? The solution by @H2CO3 would provide an analytic solution (ie: discrete differentiation). If you want a symbolic answer (ie: f'(x)=2x) then you would need to write a parser, which is more complicated. – Cloud Oct 16 '12 at 16:36
  • For the curious peeps who want the maths behind f'(x) we use the standard definition of the derivative obtained from the limits see :[Formula for derivative. ](https://i.stack.imgur.com/3NG5q.jpg) Here, h->0 (h tends to 0) means that h is a very small number. You can take this number to be 10^-5 for most calculations. Although both equations work perfectly well, in practice, the second one gives better values. However, one may also note that these computations are highly expensive and should be avoided when the actual function can be derived by hand. – Farhan Hai Khan Dec 01 '19 at 17:42

4 Answers4

22

Yes, it is quite possible. However, the solution depends on your needs. If you need a simple numerical solution, the following will do (to a certain extent, with some constraints - naive implementation):

double derive(double (*f)(double), double x0)
{
    const double delta = 1.0e-6; // or similar
    double x1 = x0 - delta;
    double x2 = x0 + delta;
    double y1 = f(x1);
    double y2 = f(x2);
    return (y2 - y1) / (x2 - x1);
}

// call it as follows:
#include <math.h>

double der = derive(sin, 0.0);
printf("%lf\n", der); // should be around 1.0

For more advanced numerical calculations, you can use the GNU Scientific Library.

However, if you need to analitically find the formula of the derivative of a given function, then you have to:

  1. Parse the input formula to some abstract data type, for example an AST;
  2. Derivate it using the identities and rules of derivation (there's only a few of them, this part should be the easiest),
  3. Serialize the abstract data type you got as the result of the derivation process to a string and output that as the result.

However, you won't need to do all this; there are great C mathematical libraries that provide such functionality.

Edit: after some Googling, I couldn't find one. The closest solution for getting you started I can think of is having a look at GeoGebra's source code - although it's written in Java, it's fairly easy to read for anybody fluent enough in a C-like language. If not, just go ahead and implement that algorithm yourself :)

  • "I couldn't find one" -- well, there's Matlab (http://stackoverflow.com/questions/1513583/how-to-call-matlab-code-from-c) ;-) – Steve Jessop Oct 16 '12 at 13:55
  • @SteveJessop is it opensource in order to examine how it accomplises analytical derivation? –  Oct 16 '12 at 14:00
  • (@SteveJessop I hope you haven't seriously believed I didn't know about the existence of MatLab.) –  Oct 16 '12 at 14:00
  • 1
    I just meant to highlight that Matlab can (with some effort) be used in place of that C library you were looking for and didn't find. In some sense anything with C bindings is a C library. The `;-)` was simply because Matlab is where the questioner started from, and may be where he ends up. Matlab is not open source, but searching "matlab alternative" provides some candidates. – Steve Jessop Oct 16 '12 at 14:08
3

There is nothing built into the C language to enable this. You might be able to find a numerical library to do it though if you search online, although I would doubt that there is anything available that will provide symbolic derivatives. You could consider coding approximate numerical derivatives yourself using forward, backward and/or central differences.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
3

For simple functions the following numerical differentiation works quite well:

typedef double (*TFunc)(double);

// general approximation of derivative using central difference
double diff(TFunc f, double x, double dx=1e-10)
{
  double dy = f(x+dx)-f(x-dx);
  return dy/(2.*dx);
}

// more or less arbitrary function from double to double:
double f(double x)
{
   return x*x;
}

// and here is how you get the derivative of f at specified location
double fp = diff(f, 5.);
coproc
  • 6,027
  • 2
  • 20
  • 31
0

In C, you can do rough numerical differentiation relatively easy, but any kind of symbolic differentiation requires a third-party framework or rolling your own.

C is a general-purpose and low-level programming language, unlike Matlab, which is specialized for mathematical computations and has advanced tools for symbolic computations.

Dmytro Sirenko
  • 5,003
  • 21
  • 26