Implementing the Derivative in C/C++

Implementing the derivative in C/C++

I agree with @erikkallen that (f(x + h) - f(x - h)) / 2 * h is the usual approach for numerically approximating derivatives. However, getting the right step size h is a little subtle.

The approximation error in (f(x + h) - f(x - h)) / 2 * h decreases as h gets smaller, which says you should take h as small as possible. But as h gets smaller, the error from floating point subtraction increases since the numerator requires subtracting nearly equal numbers. If h is too small, you can loose a lot of precision in the subtraction. So in practice you have to pick a not-too-small value of h that minimizes the combination of approximation error and numerical error.

As a rule of thumb, you can try h = SQRT(DBL_EPSILON) where DBL_EPSILON is the smallest double precision number e such that 1 + e != 1 in machine precision. DBL_EPSILON is about 10^-15 so you could use h = 10^-7 or 10^-8.

For more details, see these notes on picking the step size for differential equations.

How to find derivative of a function using c

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

Numerical implementation of n-th derivative of f(x)?

It is not a good implementation

At least these problems.

Integer math

Use FP math as 1/3 is zero.

1/3 --> 1.0/3

Using the cube root optimal for n==1

But not certainly other n. @Eugene

Wrong epsilon

Below code is only useful for |x_0| about 1.0. When x_0 is large, x_0 - h may equal x_0. When x_0 is small, x_0 - h may equal -h.

OP's +/- some epsilon is good for fixed point, but double is a floating point.

// Bad
const double h = pow( __DBL_EPSILON__, 1.0/3 );
double x_1 = x_0 - h;

A relative scaling is needed.

#define EPS cbrt(DBL_EPSILON) // TBD code to well select this 
if (fabs(x_0) >= DBL_MIN && isfinite(x_0)) {
double x_1 = x_0*(1.0 - EP3);
double x_2 = x_0*(1.0 + EPS);
double h2 = x_2 - x_1;
...
} else {
TBD_Code for special cases
}

Invalid code

f is double ( *f )( int, double ), but call is f( x_0 )

Minor: confusing names

Why first_term with x_2 and second_term with x_1?

Implementation Of Differentiation & Integration(Calculus) In C++

You can use Calculus c++ library.

It is easy to use. You can declare variables as Variable x="x",y="y";, and functions as Function f=sin(x)*sin(y);.

And you can differentiate it for example with respect to x as
Function dfdx= f->get_partial_derivative(x);

C++ -- How write a function, that return the derivative of a real valued function, not the value of the derivative

Once you can compute the value of a function at one point, you can use that to implement your general function. Lambda expressions allow you to generate those derived functions easily:

auto MakeDerivative(double (&f)(double)) {
return [=](double x) { return Deriv(f, x); };
}

If you want to be able to use stateful functions, you may need to update your Deriv to be a function template whose first parameter type is a template parameter. This is true in particular if you want to apply MakeDerivative repeatedly (since its return types are stateful closures):

template <typename F>
double Deriv(F f, double x) {
// your code here
}

template <typename F>
auto MakeDerivative(F f) {
return [=](double x) { return Deriv(f, x); };
}

However, you may be interested in techniques like "automatic differentiation" which allow you to express the derivative directly in terms of the definition of the original function, at the cost of working on an enlarged domain (an infinitesimal neighbourhood, essentially).

Calculate derivative using limit definition in C

I think a good way to do this is with one function that calculates the derivative based on that definition, as well as with one function that implements that specific formula.

float deriv (float x, float h) {

float dydx = (function(x+h) - function(x))/h;
return dydx;
}

float function(float x) {
// Implement your sin function here evaluated for the argument
}

Keep in mind that the definition of a derivative works for as h->0 and to get f'(x) requires stuff to cancel. What we have here is a numerical estimate that is a glorified gradient equation. Good luck!

How to write code to calculate partial derivative of several variable function in C++?

You seem to be very close to the solution, but struggling with the step to functions defined over multiple dimensions.

Instead of having one member variable df, you need several of them, one for each partial derivative. You could hard-code them:

double dfx, dfy, dfz;

or use a container:

double df[3];

I'll use hard-coding for now. (Containers are a vitally important topic, and a std::vector is better than an array in almost all respects, but one thing at a time.)

It would also be wise to rename the other variable from x to v, since it represents the value of the function at the point of interest, not the location of the point. (This is worth thinking about.)

The old constructor took one value and one derivative:

Der :: Der(double x){
this->f = x;
this->df = 1;
}

This can be better written using initializers:

Der :: Der(double nx): x(nx), df(1)
{}

which makes it easy to rewrite the constructor to take three partial derivatives:

Der :: Der(double nv, double dx, double dy, double dz): v(nv), dfx(dx), dfy(dy), dfz(dz)
{}

Now we can declare functions:

Der x(2, 1, 0, 0), y(4, 0, 1, 0), z(5, 0, 0, 1);

And the logic of the arithmetical operations is straightforward. For instance, the first partial of the product of two functions:

dfx = A.v * B.dfx + A.dfx * B.v;

(In fact, you could abstract the arithmetic out of your current class and use it for both old and new classes, but one thing at a time.)



Related Topics



Leave a reply



Submit