質問

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?

役に立ちましたか?

解決

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 :)

他のヒント

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.);

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.

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.

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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top