How do I compute the second derivative of a one-dimensional array?

In summary: Time (sec.) | Distance (m) | Velocity (m/s) | Acceleration (m/s^2) ||-------------|---------------|----------------|----------------------|| 0.0 | 0.0 | | || 0.1 | 5.0 | 50.0 | || 0.2 | 12.0 | 70.0 | 200.0 || 0.3 | 20.0 | 80.0 | 100.0 || 0.4 | 30.0 | 100.0 | 200.0
  • #1
btb4198
572
10
How do I compute the second derivative of an one dimensional array?
 
Physics news on Phys.org
  • #2
btb4198 said:
How do I compute the second derivative of an one dimensional array?
An array of what?

You simply differentiate twice. If you have a function ##f\, : \,\mathbb{R}^m\longrightarrow \mathbb{R}^n## you get an array of Hesse matrices as a result, a tensor, a single matrix in case ##n=1.##
 
  • #3
array of doubles
 
  • #4
btb4198 said:
array of doubles
LOL. Well, that's super helpful, thanks.

Seriously though, if you have two columns of numbers (in Excel for example), with the first column the time stamps of the samples, and the 2nd column the position at that time stamp, make two more columns for the velocity and acceleration as a function of time.

For those last 2 columns, you would compute each entry as the change in the previous column divided by the change in the time column.

Have you made these types of Excel spreadsheets in the past? You can do the same thing with a CSV file and doing the computations with your language of choice (C, Python, etc.).
 
  • #5
btb4198 said:
How do I compute the second derivative of an one dimensional array?

fresh_42 said:
An array of what?

btb4198 said:
array of doubles
If the array consists of constants of type double, and not functions of some variable, then it doesn't make much sense to talk about the first derivative, let alone the second derivative. The derivative with respect to any variable of a constant is zero.

If you're asking about first differences and second differences, rather than first and second derivatives, that's something different. Solving difference equations, as opposed to differential equations (which involve derivatives) might be related to what you're asking. If you do a web search for either of these terms, or for difference equations, you'll get lots of hits.
 
  • Like
Likes Delta2
  • #6
The array is of X-ray value sorted into bins from a DPP. so kve vs bin.
but I am not sure what that matter
 
  • #7
berkeman said:
LOL. Well, that's super helpful, thanks.

Seriously though, if you have two columns of numbers (in Excel for example), with the first column the time stamps of the samples, and the 2nd column the position at that time stamp, make two more columns for the velocity and acceleration as a function of time.

For those last 2 columns, you would compute each entry as the change in the previous column divided by the change in the time column.

Have you made these types of Excel spreadsheets in the past? You can do the same thing with a CSV file and doing the computations with your language of choice (C, Python, etc.).
Can you do an example by hand?
 
  • #8
How do you get the change ?
 
  • #9
btb4198 said:
Can you do an example by hand?
I'd prefer to do it in Excel and post a PDF of it with the cell math shown.

Have you never done this in Excel? What is your exact task? Is it from a lab or a homework assignment?

I have meetings for the rest of the night, but will try to post an example Excel spreadsheet in my morning if nobody else beats me to it.
 
  • Like
Likes WWGD
  • #10
It is not for school ... I am going to be coding this
 
  • #11
btb4198 said:
How do I compute the second derivative of an one dimensional array?
As already stated, this doesn't make much sense. You can't take the derivative of an array.
Also, it's meaningless to "take the derivative" of something. Differentiation always represents the change of one variable with respect to some other variable. So far you haven't told us what the other variable is.
btb4198 said:
The array is of X-ray value sorted into bins from a DPP. so kve vs bin.
DPP?
kve == kilovolt electrons?
What does "kve vs bin" mean?
How are the bins determined?
Please stop with the overly terse responses that tell us almost nothing.
btb4198 said:
It is not for school ... I am going to be coding this
Good luck with that. You haven't given us a clear explanation of what you're doing, which makes it extremely difficult for us to give meaningful help or for you to implement code for it.
 
  • Like
Likes Delta2 and berkeman
  • #12
berkeman said:
I'd prefer to do it in Excel and post a PDF of it with the cell math shown.

Here is a simple example using Excel. The sample time base is 0.1 seconds between samples, and I made up the position (Distance) numbers. The equations for the velocity and acceleration are as follows:

Velocity calculation in cell C3: =(B3-B2)/$A$2

Acceleration calculation in cell D4: =(C4-C3)/$A$2
1652976432182.png
 
  • Like
Likes btb4198
  • #13
berkeman said:
Here is a simple example using Excel. The sample time base is 0.1 seconds between samples, and I made up the position (Distance) numbers. The equations for the velocity and acceleration are as follows:

Velocity calculation in cell C3: =(B3-B2)/$A$2

Acceleration calculation in cell D4: =(C4-C3)/$A$2View attachment 301649

Just as information. You can copy cells into the PF edit window which automatically turns into an editable table instead of an image.
14.502,4100​
7,0000​
4.395,0483​
14.248,9600​
7,0000​
4.386,3770​
13.974,7500​
7,0000​
4.238,1300​
13.975,2500​
7,0000​
4.222,6772​
13.749,2900​
7,0000​
4.196,6640​
13.915,9700​
7,0000​
4.203,5547​
14.097,8800​
7,5000​
4.203,5737​
13.947,2100​
7,5000​
4.155,7250​
14.039,4700​
7,5000​
4.173,9976​
 
  • Informative
  • Like
Likes btb4198 and berkeman
  • #14
fresh_42 said:
Just as information. You can copy cells into the PF edit window which automatically turns into an editable table instead of an image.

Neat. Trying that here:

TimeDistanceVelocityAcceleration
0.10
0.2110
0.3320100
0.4410-100
0.5620100
0.68200
0.710200
0.89-10-300
0.95-40-300
11050900
1.120100500
1.22110-900
1.322100
 
Last edited:
  • #15
Hmm, do I need to paste it in with some option, or just ctrl-v? I don't see the cells above as editable or showing the formulas for the calculations.
 
Last edited:
  • #16
berkeman said:
Hmm, do I need to pate it in with some option, or just ctrl-v? I don't see the cells above as editable or showing the formulas for the calculations.
I only used Ctrl+V. And I edited your table. Your last row has now a mistake. Of course, it does not copy the formulas, only the content.
 
  • #17
fresh_42 said:
I only used Ctrl+V. And I edited your table. Your last row has now a mistake. Of course, it does not copy the formulas, only the content.
Okay, I fixed the error in the last row of the table now. I agree that it just does paste-by-value.
 
  • Like
Likes fresh_42
  • #18
berkeman

Thanks! I understand how to do this now. One question
should it be
(y2-y1) / ( x2-x1)

because you have :
C3: =(B3-B2)/$A$2

what about x1 ?
 
  • Like
Likes berkeman
  • #19
btb4198 said:
berkeman

Thanks! I understand how to do this now. One question
should it be
(y2-y1) / ( x2-x1)

because you have :
C3: =(B3-B2)/$A$2

what about x1 ?
My sample intervals were on the even 0.1 second marks, so t2-t1 = t1 for that case. Makes sense? If the sampling were not on the same time interval, then yes you would need to compute the delta-t for each case.
 
  • #20
btb4198 said:
One question
should it be
(y2-y1) / ( x2-x1)
In post #1 you asked about the "derivative of an array." As I mentioned earlier, a derivative represents the change of one variable with respect to or relative to another. As far as I can tell, and from what you described, you have only one quantity. @berkeman's spreadsheet had variables of distance vs. time and velocity vs. time.
 
  • #21
btb4198 said:
How do I compute the second derivative of an one dimensional array?
A better question, IMO, is to first tell us what you are trying to do rather than jumping into the details of how to do it. This is, in part, why we're 20 posts into this thread.
 
  • Like
Likes PhDeezNutz, jbriggs444, berkeman and 1 other person
  • #22
Mark44 said:
In post #1 you asked about the "derivative of an array." As I mentioned earlier, a derivative represents the change of one variable with respect to or relative to another. As far as I can tell, and from what you described, you have only one quantity. @berkeman's spreadsheet had variables of distance vs. time and velocity vs. time.
If the data are not from evenly spaced intervals then the question of what "time" values one needs to use becomes trickier.

What is calculated for the "first derivative" is a reasonable estimate for the "velocity" of the measured "position" at the midpoint of each "time" interval. It is the average "velocity" for the interval. The "time" imputed for this estimated velocity should be the "time" of the interval midpoint.

The calculation for the "second derivative" would then be a reasonable estimate for the "acceleration" between the midpoint of the first interval and the midpoint of the second interval. But only if the "time" that one divides by is the "time" between the interval midpoints rather than "time" between the interval endpoints. The "time" imputed for this estimated "acceleration" should be the "time" of the midpoint between the two midpoints.

I do not know whether this will eliminate all bias in your estimation process, but it will at least eliminate some. Depending on what you are really trying to do, entirely different approaches might be more appropriate.

btb4198 said:
what about x1 ?
If I understand that concern properly, the above discussion takes care of it. If you had ##n## samples, you only get to calculate ##n-1## average rates of change.

Edit: DPP in context of X-Rays. Perhaps https://moxtek.com/xray-product/mxdpp-50-digital-pulse-processor/. KVe appears to be Kilo Volt effective.
 
Last edited:
  • Like
Likes berkeman
  • #23
jbriggs444 said:
If the data are not from evenly spaced intervals then the question of what "time" values you need to use becomes trickier.
I don't think we've ever found out anything about intervals yet. In post #6 the OP said something about Xray values in bins, "kve vs bin," whatever that means.
 
  • Like
Likes jbriggs444
  • #24
Mark44 said:
I don't think we've ever found out anything about intervals yet. In post #6 the OP said something about Xray values in bins, "kve vs bin," whatever that means.
Mark,
Please watch this video : What is X-ray Fluorescence (XRF)?
It will explain the spectrum output of my Digital Pulse Processor (DPP).

Does this help?
 
  • #25
jbriggs444 said:
If the data are not from evenly spaced intervals then the question of what "time" values one needs to use becomes trickier.

What is calculated for the "first derivative" is a reasonable estimate for the "velocity" of the measured "position" at the midpoint of each "time" interval. It is the average "velocity" for the interval. The "time" imputed for this estimated velocity should be the "time" of the interval midpoint.

The calculation for the "second derivative" would then be a reasonable estimate for the "acceleration" between the midpoint of the first interval and the midpoint of the second interval. But only if the "time" that one divides by is the "time" between the interval midpoints rather than "time" between the interval endpoints. The "time" imputed for this estimated "acceleration" should be the "time" of the midpoint between the two midpoints.

I do not know whether this will eliminate all bias in your estimation process, but it will at least eliminate some. Depending on what you are really trying to do, entirely different approaches might be more appropriate.If I understand that concern properly, the above discussion takes care of it. If you had ##n## samples, you only get to calculate ##n-1## average rates of change.

Edit: DPP in context of X-Rays. Perhaps https://moxtek.com/xray-product/mxdpp-50-digital-pulse-processor/. KVe appears to be Kilo Volt effective.
Hi sorry I just update. That is the DPP I am using.
 
  • #26
btb4198 said:
Please watch this video : What is X-ray Fluorescence (XRF)?
It will explain the spectrum output of my Digital Pulse Processor (DPP).

Does this help?
So your "1-dimensional data" is really this 2-dimensional data in the plot (counts per energy bin), and you want to take the 2nd derivative of the data for some reason?

1655142965402.png
 
  • #27
btb4198 said:
Hi sorry I just update. That is the DPP I am using.
But what do you want to compute the second derivative from and why? From the curve that can be sensed from that graphic? If so, then you need an analytical approximation for that curve. One way to do so is to mark several crucial points ##(x,y)## where ##x## denotes the value of the horizontal axis and ##y## the one of the vertical axis. Then you can set up a polynomial ##p(x)=a_0+a_1x+a_nx^2+\ldots+a_nx^n## with ##n## one less than the number of points you have chosen, and solve for ##p(x)=y## for your points. This gives you the values for the ##a_k## and you can differentiate ##p(x).##
 
  • #28
berkeman said:
So your "1-dimensional data" is really this 2-dimensional data in the plot (counts per energy bin), and you want to take the 2nd derivative of the data for some reason?
Sorry, I am coding someone else math. I really do not have too many answers as to why.

Question: when you have:
C3: =(B3-B2)/$A$2

My A2 would always be 1 right ?
 
  • #29
btb4198 said:
Sorry, I am coding someone else math. I really do not have too many answers as to why.

Question: when you have:
C3: =(B3-B2)/$A$2

My A2 would always be 1 right ?
This is pretty much off the wall in this thread. The above looks to be an Excel formula. If you copy this formula to the cell below, the formula in cell C4 will be "=(B4 - B3)/ $A$2". The purpose of the $ signs is to leave this part of the formula the same whether it's copied to columns to the right ($A) or rows below ($2). If the value in A2 is 1, then that's the same value that will be used when the formula is copied.

But if A2 is 1, why is it in the formula? Division by 1 makes no change in the expression it's in.
 
  • #30
Mark44 said:
This is pretty much off the wall in this thread. The above looks to be an Excel formula. If you copy this formula to the cell below, the formula in cell C4 will be "=(B4 - B3)/ $A$2". The purpose of the $ signs is to leave this part of the formula the same whether it's copied to columns to the right ($A) or rows below ($2). If the value in A2 is 1, then that's the same value that will be used when the formula is copied.

But if A2 is 1, why is it in the formula? Division by 1 makes no change in the expression it's in.
Yeah, I am going to remove it. Sorry, I never code a function to take a derivative of a graph before and I could not find any YouTube videos on how to do it by hand.
So I post the question here.
anyhow,
So Basically to take the first derivative, I am going to go through the array and subtract index + 1 from index.
So:
double value = spectrumData[i + 1] - spectrumData;and repeat the same process for the second derivative .

is this how Matlab would do it ?
 
  • #31
btb4198 said:
Yeah, I am going to remove it. Sorry, I never code a function to take a derivative of a graph before and I could not find any YouTube videos on how to do it by hand.
See my post #27. It is an instruction.
 
  • #32
btb4198 said:
So Basically to take the first derivative, I am going to go through the array and subtract index + 1 from index.
So:
double value = spectrumData[i + 1] - spectrumData;
You need to get your units right in your derivative calculations (both for the numerator and denominator). The units for the numerator look to be "counts" right? And for the denominator, you have bins separated by some delta-energy in keV? So the units of the first derivative would be delta-counts per delta keV?
 
  • #33
berkeman said:
So the units of the first derivative would be delta-counts per delta keV?
It looks like the horizontal bins are spaced linearly? What is the increment in delta-keV per bin? It looks like a small fraction of a keV delta per bin?
 
  • #34
berkeman said:
You need to get your units right in your derivative calculations (both for the numerator and denominator). The units for the numerator look to be "counts" right? And for the denominator, you have bins separated by some delta-energy in keV? So the units of the first derivative would be delta-counts per delta keV?
Yes, that is all correct.
 
  • #35
berkeman said:
It looks like the horizontal bins are spaced linearly? What is the increment in delta-keV per bin? It looks like a small fraction of a keV delta per bin?
1655149886661.png


From this picture is looks like the bins increase by one .
 
<h2>1. What is the formula for computing the second derivative of a one-dimensional array?</h2><p>The formula for computing the second derivative of a one-dimensional array is (f(x+1) - 2f(x) + f(x-1)) / h^2, where h is the step size of the array.</p><h2>2. What is the purpose of computing the second derivative of a one-dimensional array?</h2><p>The second derivative of a one-dimensional array is used to measure the rate of change of the first derivative. It can also provide information about the concavity of the function represented by the array.</p><h2>3. How do I determine the step size (h) of the array for computing the second derivative?</h2><p>The step size (h) of the array can be determined by taking the difference between two consecutive elements in the array. It is important to choose a small enough step size to ensure accuracy in the calculation.</p><h2>4. Can the second derivative of a one-dimensional array be negative?</h2><p>Yes, the second derivative of a one-dimensional array can be negative. This indicates a concave down shape of the function represented by the array.</p><h2>5. Are there any limitations to computing the second derivative of a one-dimensional array?</h2><p>One limitation is that the array must represent a continuous function. Additionally, the accuracy of the calculation may be affected by the step size chosen. It is also important to consider the precision of the data in the array.</p>

1. What is the formula for computing the second derivative of a one-dimensional array?

The formula for computing the second derivative of a one-dimensional array is (f(x+1) - 2f(x) + f(x-1)) / h^2, where h is the step size of the array.

2. What is the purpose of computing the second derivative of a one-dimensional array?

The second derivative of a one-dimensional array is used to measure the rate of change of the first derivative. It can also provide information about the concavity of the function represented by the array.

3. How do I determine the step size (h) of the array for computing the second derivative?

The step size (h) of the array can be determined by taking the difference between two consecutive elements in the array. It is important to choose a small enough step size to ensure accuracy in the calculation.

4. Can the second derivative of a one-dimensional array be negative?

Yes, the second derivative of a one-dimensional array can be negative. This indicates a concave down shape of the function represented by the array.

5. Are there any limitations to computing the second derivative of a one-dimensional array?

One limitation is that the array must represent a continuous function. Additionally, the accuracy of the calculation may be affected by the step size chosen. It is also important to consider the precision of the data in the array.

Similar threads

  • Calculus
Replies
14
Views
1K
Replies
5
Views
1K
  • Calculus
Replies
3
Views
2K
Replies
6
Views
2K
Replies
3
Views
2K
Replies
1
Views
1K
  • Calculus
Replies
4
Views
2K
  • Calculus
Replies
2
Views
1K
Replies
5
Views
2K
Back
Top