# C/C++ First Derivative Code

In calculus, the first derivative test uses the first derivative of a function to determine whether a given critical point of a function is a local maximum, a local minimum, or neither. The idea behind the first derivative test is to examine the monotonicity properties of a function just to the left and right of a given point in its domain. If the function “switches” from increasing to decreasing at the point, then close to that point, it will achieve a highest value at that point. Similarly, if the function “switches” from decreasing to increasing at the point, then close to that point, it will achieve a least value at that point. If the function fails to “switch”, and remains increasing or remains decreasing, then no highest or least value is achieved.

From this first derivative description, I create a C/C++ first derivative code.

Output from this first derivative function is firstderv. firstderv have same size with input data (variable data). This is output from data processed with first derivative code : Source :

http://en.wikipedia.org/wiki/First_derivative_test

## 6 thoughts on “C/C++ First Derivative Code”

1. naxgo says:

hello
i’m trying to do a program that finds the plasma parameters from a volt/amperic caracteristic.
i need to include in my program a piece of code that does the first derivative and i can’t understand what your code does exactly, and how could adapt it to fit my needs.
my data is stored in a matrix a where a[i] is my x data and a[i] is my y data, and using this data i can plot a graph.
i need to be able to plot a graph with a[i] as my x data and firstderiv as my y data. could you please help?

• totosugito says:

Thank for visiting my site.
Please check first derivative definition (in my site or from wiki). From your problem, You can process first derivative for your Y data. So, use my code with function
void firstDerivative(float *data, float *firstderv, int nx)

firstderv is output first derivative from your data input.
nx is length of data (same with firstderv)

So, from your main program, create array of data and firstderv.
If you use C, You can use command :
Y = (float*) calloc (nx, sizeof(float));
Yfirstderv= (float*) calloc (nx, sizeof(float));
and call firstderivative function with command :
firstDerivative(Y, Yfirstderv, nx);

2. victor says:

very interest solution for derivate array be the same lenght with signal array.

you can improve this code by using vectors like dynamic arrays:

vector firstDerivative(vector data){
vector firstderv
double tmp1, tmp2;
int i;

//compute derivative first index
firstderv.push_back(data-data);

for(i=1; i<data.size()-2; i++) //compute first derivative
{
tmp1 = data[i]-data[i-1];
tmp2 = data[i+1]-data[i];
firstderv.push_back((tmp2+tmp1)/2);
}

//compute derivative last index
firstderv.push_back(data[data.size()-1]-data[data.size()-2]);
return firstderv;
}

• totosugito says:

Hi victor,
Thank you for visiting and give other method for First Derivative method 🙂
Best regards,

Toto

3. Cro says:

thanks Sir for this code but what is nx?

4. Nick says:

Thanks for sharing 😉 Simple & well-explained .. will modify it to find peaks in my ecg signal.

This site uses Akismet to reduce spam. Learn how your comment data is processed.