From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate the derivative of a function over time

Solved!
Go to solution

Hello guys ! 

 

Sorry If I don't write well, It is my first question to the community.. and I am a newbie- Labview user.(neither a good enlish speaker 🤐)

 

I need for an universitary projet to calculate the derivative dY of a function as shown with the picture attached: dY

My calculation is very simple : dY=Y(t)-Y(t-dt).

 

I also have attached the Vi I created to do it.

It works well when it is the only Vi I call , but when I use it as a Sub-Vi  in a projet ...and you may know here is my issue ..

It stays in the while loop (that is kind obsious since I need to call it to stop😅)

 

I have first thought that I could create some kind of boolean waveform whom  stops the while-loop  every Tcycle, but I am afraid the data would not be saved in the appened array...

 

Here is my question : How can I simply get this derivative, with a Sub-Vi callable, from a big projet-vi  and maybe without using a while-loop?

 

 

I have also looked at other community questions, and a Timer-loop may be the solution, but I have never used it before.. 

 

 

Regards 😊

Victorin 

Download All
0 Kudos
Message 1 of 7
(6,277 Views)
Solution
Accepted by topic author Lars_Novice

Do you really just need dY or do you need dY/dt?

 

And if you need dY/dt, should the time be based on input values for time or should it be figured automatically based on the time the calculation is done according to the PC's real-time clock?

 

Either way, the code is far more complicated than it needs to be.   For example, here's what the subvi code could look like for dY alone (assuming you want the initial dY to be 0):

 

Kevin_Price_0-1615817289579.png

 

Just one feedback node (with direction reversed and initialization) and one subtraction.  The feedback node functions similarly to an uninitialized shift register, the input is a present value and the output a prior value.

 

You could do a similar thing with your time info to calculate a dt if you need dY/dt.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 2 of 7
(6,241 Views)

It is not really clear what you want. Is dt a user input that does not correspond to the point spacing of the data? (From your picture it seems you want the average slope define between two distant points!).

 

Would it be possible to clean up your code to the essentials and get rid of all these confusing convoluted parts?

 

Shouldn't the shift register be initialized? Why are you creating errors with code=0? Your second shift register always contains zero, so it is just a glorified diagram constant. Why are your index array resized to two outputs if you only use one output each? Do you know that you only need one index array here, resized to two outputs? All your index calculations should probably be in blue.

 

 

0 Kudos
Message 3 of 7
(6,235 Views)

Hello ! 

 

Thank you for answering 🙂

 

My input is : y(t)

I have 2 parameters :  dt wich is the time where I wanna too see the Y(t-dt) and the Tcycle

The output is : dY  wich is the difference between the value at t time and the previous value at t-dt time.

 

Unfortunatly I have tried your code, within a while loop and an event case(I am pretty sur event case is not a good idea here ) and it seems not to be working to me I may have done a misstake, and yet am a newbie 😩

 

 

Victorin

0 Kudos
Message 4 of 7
(6,179 Views)

Hello ! 

 

and thank you for answering !

 

 

What I need is the Delta variance between the current value and a value saved at a  dT time.

 

 

I did it , you are completely right !  I have initalized shift registers and get ride of the other one whom I did not use.

For the error I though we have to obviously initialise it with a 0 first.

My index array is now with boths inputs at corrects indexs. 

It seems that the calculuations of index is working with double, but you are right I may get an error if I have something like "3.5 "

 

 

But I still need the while loop wich block the code in when I execute it...

To summarize, the projet is made with severals functions and somewhere I need to calculate this dY in order to have correction factor, but it stays in the while loop for ever since the data are sent only at the stop of the while loop

 

Is having a "stop" function" that stop the while loop and send the data a good idea ? I fear of losing the previous datas saved..

 

What I precisly need it to have this function always sending its outputs not matter where I am in the code of all  my other functions

 

Victorin 

0 Kudos
Message 5 of 7
(6,178 Views)

I really think we need a more complete and thorough description. A numerical derivative will tend to add "noise" to your data, possibly an unacceptable amount if not done well.  (This contrasts with numerical integration, which tends to filter and smooth out "noise").

 

But there are too many things I don't know about your app.

- What produces the Y data?  How does it end up in the array you're working with?    

- Are you controlling dt, measuring dt, or assuming dt?  Explain how or why.

- How is the dY (or dY/dt) data being used?  Does the app use it in near-real-time?  Or do you just calculate it, save it, and analyze it later?

- The little piece of code I illustrated could give you the dY between consecutive "samples" of Y.  This is known as a finite difference and is about as simple as can be, but can also amplify any "noise" in the original Y data.  I can't tell yet whether it's suitable for your app.  Please talk through whatever concerns or fears you have about using a simple finite difference.  (They may be entirely legitimate, but I'd want to confirm that before going with a more complicated approach.)

 

 

-Kevin P

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 6 of 7
(6,136 Views)

Hello again and osry for answering this late,  you were right think I did not know what I wanted to actually have ,and your solutions was actually the one.

 

Thank you, and sorry again for answering this late 🙂

 

Victorin

0 Kudos
Message 7 of 7
(5,987 Views)