# 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**.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
void firstDerivative(float *data, float *firstderv, int nx) { int i; float tmp1, tmp2; //compute derivative first index firstderv[0] = data[1]-data[0]; for(i=1; i<nx-2; i++) //compute first derivative { tmp1 = data[i]-data[i-1]; tmp2 = data[i+1]-data[i]; firstderv[i] = (tmp2+tmp1)/2; } //compute derivative last index firstderv[nx-1] = data[nx-1]-data[nx-2]; } |

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
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[500][2] where a[i][1] is my x data and a[i][2] 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][1] as my x data and firstderiv as my y data. could you please help?

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)

data is your input data (Y data from your problem)

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

Maybe this can help you. If you have any problem or confused with my explaining please contact me. Thank you

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[1]-data[0]);

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;

}

Hi victor,

Thank you for visiting and give other method for First Derivative method 🙂

Best regards,

Toto

thanks Sir for this code but what is nx?

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