LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Derivative for different sampling interval

 

Hi,

I have an issue with derivative function.

Yi = 1/2dt * (Xi+1 - Xi-1)

I did expect that changing the dt also the dX change accordingly. I mean, if I have an equation of a straight line, the derivative should not be dependent by dt, but this is not the case in that formula.

What I need is to calculate the derivative for each single element of an array, but with a dt variable with dX. If dt is 3, then also the indexing of dX must be 3 (Xi – Xi-3). And in theory for not integer values as well, then it should make interpolation for dX values.

 

Can you tell me whether exist any function like this or I have to implement it? I attached a picture to make it more clear.

 

Thank you.

0 Kudos
Message 1 of 9
(2,663 Views)

Maybe it is a language problem, but little that you said makes sense.  If you have a function F evaluated at two points P1 and P2 (having coordinates (x1, y1) and (x2, y2)), you can estimate the derivative of F at, say, P1 by computing (y1-y2)/(x1-x2).  The formula you showed uses three points and estimates the derivative at the middle point by taking the mean of the derivative of the left two and right two points.

 

Bob Schor

0 Kudos
Message 2 of 9
(2,632 Views)

Perhaps I'm missing a key point regarding your specific method choice, but LabVIEW has a built-in VI to calculate derivatives of an evenly spaced array using one of 4 methods.

 

It's documented here: Derivative X(t). Does that help?


GCentral
0 Kudos
Message 3 of 9
(2,600 Views)

That function is exactly my issue.

Sorry I was not clear. The formula I posted is the first method (2nd order central) of that function, but the others are based on the same concept, the delta of the function is calculated between adjacent element of the array.

I need a derivative of the function F calculated for each element of the array as per (F(x1) - F(x2)) / (x1 - x2), not matter what x1 and x2 are (eventually even not integer).

 

I mean, I can easily implement it, but since In the code I have to recall it many times It might become time consuming , and a native LabVIEW function might be faster.

 

Thank you

  

0 Kudos
Message 4 of 9
(2,588 Views)

Maybe I'm still misunderstanding, but I think I'm a little closer now.

 

The built-in function calculates the derivative at each point of the original array (with integer steps), using a few of the surrounding points (depending on method).

It sounds like you instead want the straight line slope between any two points of an X-Y graph/pair of arrays (specifying X).

 

Do you have two arrays to start with? I'm not sure there is a built-in function for what I think you want, but it should be easy to quickly program it into a subVI and then call repeatedly. If you set the VI to use inlining (and the required preallocated clones setting) that might be what you're looking for. There are built-in functions to interpolate and get fractional indices, which will probably help you as you move to non-existing values of X.

 


GCentral
0 Kudos
Message 5 of 9
(2,583 Views)

Hi, here attached what I meant.

 

I still need to manage the shift and the initial / final condition, but this is how it should work.

 

Thank you.

 

0 Kudos
Message 6 of 9
(2,574 Views)

I'm trying to work out exactly what you're doing, but your "derivative" generated by that function is -cos(x), where y=sin(x). You probably need to at least invert your subtraction. But I don't think this is doing what you need.

 

In your second loop, you're taking the fractional index (i+dt), which for small dt is basically i in range [0:359]. The output of that function is the value of sin(x) for x = (i+dt), where x_evaluation is in degrees (sin calculated for 0 to 2pi radians).

You're subtracting that value from sin(x), which gives you Y(x) - Y(x+dt). (hence the inverted subtraction). Then divide by dt, gives you a derivative. When you divide again, you're getting a mis-scaled derivative (set dt small and check the scale on the RHS).

 

I'd have thought you'd want to evaluate the derivative over the range of Y, every dt in X, which gives non-equal length input and output arrays (you'd need to use clusters or something to graph that).

 

Edit: I tried using the dt value set to the actual dt value (2pi/180) and get reasonable results. In this case, they incidentally approximate the Derivative function pretty closely (after switching the subtraction). I uploaded a slightly modified version - you can freely change dt2 to have different step sizes, but dt 1 must be fixed to get the right magnitude.

 


GCentral
0 Kudos
Message 7 of 9
(2,570 Views)

sorry, you are right. I was doing a quick example, just before attached it I realized that the division inside the For loop is not efficient, I moved it out of the For loop but I forgot to delete the one inside.

About the length and sign you are right, but as mentioned it is just a quick example. I used the sin function just as an example. I still have to managed the shift and the initial / final condition.

0 Kudos
Message 8 of 9
(2,560 Views)

@logatto wrote:

I still have to managed the shift and the initial / final condition.


Yes, absolutely!  If you gave any thought to the problem, you would realize that this is equivalent to being unable to precisely and accurately predict the future (think about it -- if you know the derivative, you effectively know the function at an infinitesimal time in the future).  If you learn about "numerical differentiation", and especially if you do some pencil/paper exercises for yourself, you'll see that the "shift" and "end condition" situations are inescapable parts of the computation.

 

Bob Schor

0 Kudos
Message 9 of 9
(2,545 Views)