Numerical Derivative & Integral❓

🧮 Calculator 🏠 Home Video

How to calculate the slope and area from measured x/y data: finite differences, Savitzky-Golay, trapezoid and Simpson rules, noise, step size and uneven x.

Contents
  1. What Are a Numerical Derivative and Integral? Slope and Area from Numbers
  2. Methods
    1. Derivatives
      1. Forward and Backward Difference
      2. Central Difference
      3. Second and Higher Derivatives
      4. Savitzky-Golay Derivative
    2. Integrals
      1. Trapezoid Rule
      2. Simpson's Rule
      3. Cumulative Integral
  3. Which One Should You Choose?
  4. Key Ideas
    1. Step Size: The Smallest Is Not the Best
    2. Order of Accuracy
    3. Noise: Derivatives Amplify, Integrals Average
    4. Higher Orders
    5. Uneven x
    6. Edges
    7. Integration Constant and Area
  5. Example: One Noisy Record, Differentiated and Integrated
  6. REFs

What Are a Numerical Derivative and Integral? Slope and Area from Numbers

A position recorded every tenth of a second, a power reading from a meter, or a current trace from a detector: each is a set of numbers, not a formula. Yet the questions are the same ones calculus answers. How quickly does the value change at each point (the derivative, or slope)? How much accumulates over a range (the integral, or area under the curve)? For example, velocity comes from position, energy from power, and charge from current.

With a formula, both operations follow directly from calculus rules: the derivative of $\sin x$ is $\cos x$. Measured data, however, provide no formula to differentiate or integrate. A numerical derivative is calculated from differences between the available values; a numerical integral is calculated by summing contributions across the data range. Their outputs have different units from the original $y$: the derivative has units of $y$ per $x$, while the integral has units of $y$ multiplied by $x$. To estimate the value of $y$ between two samples, see interpolation instead.

Consider three data columns, each sampled at 101 points from $x$ = 0 to 10 with a step size of $h$ = 0.1. Chart 1 and Chart 2 show the data, and Table 1 lists ten representative rows.

$x$00.10.20.30.40.911.1510
$y_1$0.00000.09980.19870.29550.38940.78330.84150.8912−0.9589−0.5440
$y_2$0.00000.10580.19320.27770.38030.77090.85130.8983−0.9437−0.5501
$y_3$00.00010.00080.00270.00640.07290.10.133112.5100

Table 1. Ten of the 101 rows of the example, as measured

y1 in blue and y2 in red as dots at every sample from x = 0 to 10: two sine cycles between −1 and 1, the red dots scattered slightly around the blue

Chart 1. $y_1$ and $y_2$ of Table 1 at all 101 samples

y3 as green dots from x = 0 to 10, rising from 0 to 100 along a cubic curve

Chart 2. $y_3$ of Table 1 at all 101 samples

Chart 3 illustrates both operations using $y_1$ from Chart 1. At $x$ = 1, the blue line has the slope estimated by the numerical derivative: 0.539 (the true slope, $\cos 1$, is 0.540). The shaded region beneath the samples from $x$ = 2 to 3 represents the area estimated by numerical integration.

The samples of y1 from x = 0 to 6.3 as black dots; a short blue line touching them at x = 1 labeled slope 0.539; the region under the dots from x = 2 to 3 shaded red, labeled area

Chart 3. $y_1$ of Chart 1: the slope at one point, and the area over one range

All subsequent charts use the same $y_1$, $y_2$, and $y_3$ data.

Methods

The methods fall into two families. Derivatives subtract neighbouring samples and divide by the distance between them in $x$. Integrals add the areas of thin strips between neighbouring samples. Both operate directly on the data, without requiring an underlying formula.

Derivatives

Forward and Backward Difference

In plain words. The slope at a sample is the rise to the next sample divided by the run: the forward difference. Looking back to the previous sample instead gives the backward difference. Each uses one neighbour only, so each describes the slope at the midpoint of its chord, half a step away from the point it is reported at: there, at $x_i + h/2$ for the forward difference, it is second-order accurate; at $x_i$ itself only first-order.

Control parameters. method = forward or backward, and order ($n$, 1 by default; the advanced calculator only). There are no other settings: $h$ is determined by the spacing of the data.

Basic idea. With $h$ = $x_{i+1} - x_i$ (Math 1):

$$y'_i \approx \frac{y_{i+1} - y_i}{x_{i+1} - x_i} \ \ \text{(forward)}, \qquad y'_i \approx \frac{y_i - y_{i-1}}{x_i - x_{i-1}} \ \ \text{(backward)}$$
$$\begin{aligned} y'_i &\approx \frac{y_{i+1} - y_i}{x_{i+1} - x_i} && \text{(forward)} \\ y'_i &\approx \frac{y_i - y_{i-1}}{x_i - x_{i-1}} && \text{(backward)} \end{aligned}$$

Math 1.

methodsamples of $y_1$ usedslope at $x$ = 1error against $\cos 1$ = 0.5403
forward (Math 1)$x$ = 1: 0.8415, $x$ = 1.1: 0.89120.4970−0.0433
backward (Math 1)$x$ = 0.9: 0.7833, $x$ = 1: 0.84150.5820+0.0417
central (Math 2)$x$ = 0.9: 0.7833, $x$ = 1.1: 0.89120.5395−0.0008

Table 2. Three samples of $y_1$ around $x$ = 1, and the slope each method takes from them

y1 from x = 0.5 to 1.5 as black dots on a gray dotted sine; four lines through the dot at x = 1: red forward 0.4970, green backward 0.5820, blue central 0.5395 and a black dashed true tangent 0.5403, the blue almost on top of the black

Chart 4. The slopes of Table 2 drawn through $y_1$ at $x$ = 1

Advantages

Disadvantages

Central Difference

In plain words. Draw a chord between the sample before and the sample after, then use its slope at the central sample. The two one-sided errors of Table 2, one too low and one too high, cancel: the central difference is their average.

Control parameters. method = central, and order ($n$). The basic calculator uses this method.

Basic idea. On evenly spaced data, with $h$ the constant step between neighbouring samples (Math 2; uneven spacing has its own form, Math 13):

$$y'_i \approx \frac{y_{i+1} - y_{i-1}}{x_{i+1} - x_{i-1}} = \frac{y_{i+1} - y_{i-1}}{2h}$$

Math 2.

Error against cos x from x = 0 to 10: red forward and green backward swing between −0.05 and +0.05 as mirror images; blue central stays on zero except a jump at x = 10

Chart 5. Error of forward, backward and central (Math 1, Math 2) on $y_1$, against the true $\cos x$

Advantages

Disadvantages

Second and Higher Derivatives

In plain words. The derivative of a derivative is the second derivative. It describes curvature, or acceleration when $y$ represents position. Numerically, it is obtained by applying the same differentiation step again to the first derivative.

Control parameters. order ($n$ below), 1 to 4, on the advanced calculator. Order 2 runs the chosen method twice, order 4 four times.

Basic idea. Math 2 applied twice on even spacing reaches two samples to each side, with a step of $2h$ (Math 3, first line). The conventional three-point formula uses the nearest neighbours instead, with a step of $h$ (Math 3, second line). They are different formulas:

$$\begin{aligned} y''_i &\approx \frac{y'_{i+1} - y'_{i-1}}{2h} = \frac{y_{i+2} - 2y_i + y_{i-2}}{4h^2} && \text{(repeated central)} \\ y''_i &\approx \frac{y_{i+1} - 2y_i + y_{i-1}}{h^2} && \text{(three-point)} \end{aligned}$$
$$\begin{aligned} &\text{repeated central:} \\ y''_i &\approx \frac{y'_{i+1} - y'_{i-1}}{2h} \\ &= \frac{y_{i+2} - 2y_i + y_{i-2}}{4h^2} \\ &\text{three-point:} \\ y''_i &\approx \frac{y_{i+1} - 2y_i + y_{i-1}}{h^2} \end{aligned}$$

Math 3.

The second derivative of y1 by the central difference applied twice, as blue dots from x = 0 to 10, lying on a gray dashed −sin x curve except slightly off at the two ends

Chart 6. Central difference applied twice to $y_1$ (Math 3), against the true $-\sin x$

Advantages

Disadvantages

Savitzky-Golay Derivative

In plain words. Instead of drawing a chord through two samples, fit a polynomial to a window of samples using least squares, then calculate the polynomial's slope at the centre of the window. The fit reduces the effect of noise before the slope is calculated: a smoothed derivative, the derivative form of the Savitzky-Golay smoother.

Control parameters. window ($2k+1$ samples, odd, 9 by default), polyorder ($p$, 3 by default; at least the order $n$), and order ($n$). Advanced calculator only. It needs evenly spaced $x$.

Basic idea. Around sample $i$, with $t = (x - x_i)/h$, fit $\sum_{j=0}^{p} a_j t^j$ to the $2k+1$ samples $y_{i-k}, \dots, y_{i+k}$; the $n$-th derivative at the centre is read off one coefficient (Math 4):

$$\min_{a_0,\dots,a_p} \sum_{m=-k}^{k} \Big(y_{i+m} - \sum_{j=0}^{p} a_j m^j\Big)^2, \qquad y^{(n)}_i \approx \frac{n!\,a_n}{h^n}$$
$$\begin{aligned} &\min_{a_0,\dots,a_p} \sum_{m=-k}^{k} \Big(y_{i+m} - \sum_{j=0}^{p} a_j m^j\Big)^2 \\ &y^{(n)}_i \approx \frac{n!\,a_n}{h^n} \end{aligned}$$

Math 4.

The derivative of noisy y2 from x = 0 to 10: a jagged red line for the central difference scattered around a gray dashed cos x, and a smoother blue line for Savitzky-Golay close to it

Chart 7. The derivative of $y_2$: central difference (Math 2) against Savitzky-Golay (Math 4), window 9, polyorder 3

Advantages

Disadvantages

Integrals

Trapezoid Rule

In plain words. Connect neighbouring samples with straight lines. The area under each segment is a trapezoid: its width multiplied by the average of the two endpoint values. The total integral is the sum of these trapezoid areas.

Control parameters. method = trapezoid. The range is set by from and to ($a$, $b$), the whole record by default. The basic calculator uses this method.

Basic idea. (Math 5):

$$\begin{aligned} \int_{x_0}^{x_N} y\,dx &\approx \sum_{i=0}^{N-1} \frac{x_{i+1} - x_i}{2}\,(y_i + y_{i+1}) \\ &= \frac{x_1 - x_0}{2}(y_0 + y_1) + \dots + \frac{x_N - x_{N-1}}{2}(y_{N-1} + y_N) \end{aligned}$$
$$\begin{aligned} \int_{x_0}^{x_N} y\,dx &\approx \sum_{i=0}^{N-1} \frac{x_{i+1} - x_i}{2}\,(y_i + y_{i+1}) \\ &= \frac{x_1 - x_0}{2}(y_0 + y_1) \\ &\quad + \dots \\ &\quad + \frac{x_N - x_{N-1}}{2}(y_{N-1} + y_N) \end{aligned}$$

Math 5.

panel$x_i$ to $x_{i+1}$$y_i$, $y_{i+1}$area
10 to 0.10.0000, 0.09980.004990
20.1 to 0.20.0998, 0.19870.014925
30.2 to 0.30.1987, 0.29550.024710
40.3 to 0.40.2955, 0.38940.034245
sum0 to 0.40.078870

Table 3. Math 5 on the first four panels of $y_1$

y1 from x = 0 to 1: eleven black dots joined by a blue line, the ten trapezoids under it shaded light blue with thin vertical edges, a gray dashed sine almost on the line

Chart 8. The trapezoid panels (Math 5) under $y_1$ from $x$ = 0 to 1

Advantages

Disadvantages

Simpson's Rule

In plain words. Replace the straight segments with parabolas, each passing through three neighbouring samples. A parabola follows the curve's bend more closely, generally giving a more accurate area estimate.

Control parameters. method = simpson, on the advanced calculator, with from and to as for the trapezoid.

Basic idea. On even spacing, with $h$ the constant distance between neighbouring samples ($h = x_{i+1} - x_i$ = $x_i - x_{i-1}$, the width of one panel), each pair of panels, from $x_{i-1}$ to $x_{i+1}$, contributes (Math 6):

$$\int_{x_{i-1}}^{x_{i+1}} y\,dx \approx \frac{h}{3}\,\big(y_{i-1} + 4y_i + y_{i+1}\big)$$

Math 6.

sin x sampled at x = 0, 1, 2, 3, 4 as five black dots; red straight lines between them cut under the gray dashed sine; two blue parabolas, 0 to 2 and 2 to 4, lie almost on the sine

Chart 9. $\sin x$ at $h$ = 1: trapezoid lines (Math 5) against Simpson's parabolas (Math 6)

Uneven spacing. When the two panels in a pair have different widths, with $h_- = x_i - x_{i-1}$ and $h_+ = x_{i+1} - x_i$, the parabola through the three samples gives Math 7; with $h_- = h_+ = h$ it turns back into Math 6:

$$\int_{x_{i-1}}^{x_{i+1}} y\,dx \approx \frac{h_- + h_+}{6}\Big[\Big(2 - \frac{h_+}{h_-}\Big) y_{i-1} + \frac{(h_- + h_+)^2}{h_- h_+}\, y_i + \Big(2 - \frac{h_-}{h_+}\Big) y_{i+1}\Big]$$
$$\begin{aligned} \int_{x_{i-1}}^{x_{i+1}} y\,dx \approx\; &\frac{h_- + h_+}{6}\Big[\Big(2 - \frac{h_+}{h_-}\Big) y_{i-1} \\ &+ \frac{(h_- + h_+)^2}{h_- h_+}\, y_i \\ &+ \Big(2 - \frac{h_-}{h_+}\Big) y_{i+1}\Big] \end{aligned}$$

Math 7.

sin x at x = 0, 0.6 and 2.0 as three black dots; two red straight lines cut well under the gray dashed sine; one blue parabola through the three dots follows it more closely

Chart 10. One uneven pair of panels, $h_-$ = 0.6 and $h_+$ = 1.4: trapezoid lines (Math 5) against Simpson's parabola (Math 7)

Advantages

Disadvantages

Cumulative Integral

In plain words. Rather than calculating only one total, maintain a running sum and report it at every sample. The result is a new curve whose value at each $x$ is the accumulated area from the first sample to that point. This is the curve shown in the calculators' output grid.

Control parameters. method (trapezoid or Simpson), order ($n$), and constants ($C$, 0 by default; advanced calculator).

Basic idea. Start at $C$ and add one panel at a time (Math 8):

$$F_0 = C, \qquad F_{i+1} = F_i + \frac{x_{i+1} - x_i}{2}\,(y_i + y_{i+1})$$
$$\begin{aligned} F_0 &= C \\ F_{i+1} &= F_i + \frac{x_{i+1} - x_i}{2}\,(y_i + y_{i+1}) \end{aligned}$$

Math 8.

From x = 0 to 10: y1 as a thin gray sine, and its cumulative trapezoid integral as blue dots rising from 0 to 2 and back twice, lying on a black dashed 1 − cos x curve

Chart 11. The cumulative trapezoid integral (Math 8) of $y_1$, against the true $1 - \cos x$

Advantages

Disadvantages

Which One Should You Choose?

For derivatives, Table 4 lists each method's RMS error relative to $\cos x$ for $y_1$ and $y_2$ from Chart 1. Chart 12 presents the same results graphically.

method$y_1$, clean$y_2$, noisyuse when
forward (Math 1)0.03440.2480data arriving in time, only the past known
backward (Math 1)0.03440.2454same
central (Math 2)0.00290.1259the default: any spacing, clean data
Savitzky-Golay (Math 4)0.00030.0621noisy data, even spacing

Table 4. Derivative methods: RMS error against $\cos x$ over all 101 samples, edges included

Bars per method, blue for y1 and red for y2: forward 0.034 and 0.248, backward 0.034 and 0.245, central 0.003 and 0.126, Savitzky-Golay 0.000 and 0.062

Chart 12. Table 4 as bars

For integrals, Table 5 lists the error in the total over the full record for $y_1$ (exact value $1 - \cos 10$ = 1.839072) and $y_3$ (exact value 250). Chart 13 presents these results graphically.

method$y_1$$y_3$use when
trapezoid (Math 5)0.00160.025the default: any data, never overshoots
Simpson (Math 6)0.0000450smooth, curved data

Table 5. Integral methods: error of the total

Bars per data set, red trapezoid and blue Simpson: on y1 0.0016 and 4.5e-05, on y3 0.025 and 0

Chart 13. Table 5 as bars

Start with the central difference for derivatives and the trapezoid rule for integrals; both handle uneven spacing. Use Savitzky-Golay when the derivative is noisy, and Simpson's rule when the data is smooth and greater accuracy in the total area is important.

Key Ideas

Step Size: The Smallest Is Not the Best

When working with a formula, you can choose the step $h$. A smaller step generally reduces the truncation error—the error caused by approximating the curve with a chord. With measured data, however, each sample also contains noise, represented here by $\varepsilon$, the typical error in an individual sample. Differentiation divides the difference between samples by $h$, amplifying this noise. The total error of the central difference has both parts (Math 9), where $|y'''|$ is the size of the third derivative near the point. Math 9 is an approximation. It assumes that $y'''$ varies little over the chord and that the noise has roughly the same magnitude at each sample.

$$E(h) \approx \frac{h^2}{6}\,|y'''| + \frac{\varepsilon}{h}, \qquad h^{*} = \left(\frac{3\,\varepsilon}{|y'''|}\right)^{1/3}$$
$$\begin{aligned} E(h) &\approx \frac{h^2}{6}\,|y'''| + \frac{\varepsilon}{h} \\ h^{*} &= \left(\frac{3\,\varepsilon}{|y'''|}\right)^{1/3} \end{aligned}$$

Math 9.

Chart 14 illustrates this behaviour using $\sin x$ sampled at 200 points, with step sizes ranging from 0.0001 to 1. Without noise (blue) the error falls with $h^2$ down to $9.4 \times 10^{-10}$. With noise of 0.001 (red) it falls only until $h$ ≈ 0.18, then rises again: at $h$ = 0.0001 it is 5.6, larger than the slope itself. Math 9 predicts $h^{*}$ ≈ 0.14 for $\varepsilon$ = 0.001 and $|y'''| \le 1$: the same size.

Log-log plot of central-difference error against step h from 0.0001 to 1: blue clean line falling straight down to the left to 1e-9; red noisy line falling from the right to a minimum near h = 0.18, then rising to the left to about 5

Chart 14. Central difference (Math 2) on $\sin x$: error against step $h$, clean and with noise 0.001 (Math 9)

Chart 14 chooses the step freely, as one can with a formula. With data, the sampling interval (0.1 in the example) fixes the smallest differentiation step: the central difference's chord spans two intervals. A wider step is still possible, by taking the chord to samples $k$ rows away, which trades truncation error for less noise exactly as Chart 14 shows. The other knob is to reduce $\varepsilon$ first: smooth, or use Savitzky-Golay.

Order of Accuracy

The order of a method is the power $p$ that describes how its error decreases as the step size is reduced (Math 10). Halving $h$ reduces the error by a factor of $2^p$.

$$E(h) \approx K\,h^p, \qquad \frac{E(h)}{E(h/2)} \approx 2^p$$

Math 10.

Chart 15 measures $p$ on clean $\sin x$ over 0 to $\pi$: the derivative at $x$ = 1, the integral over the whole range (true value 2). On log-log axes each error is a straight line whose slope is $p$: forward 1.01, central 2.00, trapezoid 2.00, Simpson 4.00. At $h$ = 0.0245 Simpson's error is $4.0 \times 10^{-9}$, the trapezoid's $1.0 \times 10^{-4}$.

Log-log plot of error against step h from 0.02 to 1: four straight lines, red forward with the shallowest slope, blue central and green dashed trapezoid parallel at twice that slope, purple dashed Simpson the steepest and lowest

Chart 15. Error against step $h$ on clean $\sin x$: the slope of each line is the order $p$ (Math 10)

This order-of-accuracy description applies to clean, smooth data. With noisy data, the noise term in Math 9 can dominate, so using a higher-order method does not necessarily improve the result.

Noise: Derivatives Amplify, Integrals Average

A derivative subtracts two nearby values. Much of the underlying signal cancels, but the noise generally does not; the remaining difference is then divided by a small step size. With independent, zero-mean noise of standard deviation $\sigma$ on each sample, the central difference carries (Math 11):

$$\sigma_{y'} \approx \frac{\sqrt{2}\,\sigma}{2h} = \frac{\sigma}{\sqrt{2}\,h}, \qquad \sigma_{\int} \approx \sigma\,h\,\sqrt{N}$$

Math 11.

An integral scales each sample's noise by approximately $h$ and accumulates the contributions. For independent, zero-mean errors, positive and negative contributions partly cancel, so the resulting standard deviation grows approximately with the square root of the number of panels, $N$. Chart 16 shows both on $y_2$ of Chart 1, whose noise is 0.0178 RMS: the derivative has an RMS error of 0.126, about seven times the noise level, as predicted by Math 11. The integral's RMS error is 0.027 and varies much more gradually.

$$\sigma_{y'} \approx \frac{\sigma}{\sqrt{2}\,h} = \frac{0.0178}{1.414 \times 0.1} = 0.126$$

Error of y2's derivative and integral from x = 0 to 10 on one axis: the red derivative error jumps between −0.32 and +0.32 from sample to sample; the blue integral error drifts slowly near zero, within ±0.04

Chart 16. $y_2$: error of the central derivative and of the cumulative trapezoid integral (Math 11)

Higher Orders

The advanced calculator can repeat each operation up to four times. The $n$-th derivative applies the first-derivative operation $n$ times. The $n$-th integral applies cumulative integration $n$ times, with the $k$-th pass starting from its own constant $C_k$ (Math 12):

$$y^{(n)} = \underbrace{D(D(\cdots D}_{n}(y))), \qquad I_0 = y, \quad I_k(x) = C_k + \int_{x_0}^{x} I_{k-1}\,dt$$
$$\begin{aligned} y^{(n)} &= \underbrace{D(D(\cdots D}_{n}(y))) \\ I_0 &= y \\ I_k(x) &= C_k + \int_{x_0}^{x} I_{k-1}\,dt \end{aligned}$$

Math 12.

The independent-noise estimate in Math 11 does not apply unchanged at every pass: each operation changes the correlations between errors in neighbouring samples. Chart 17 reports the measured RMS error for $y_2$ from Chart 1, for orders $n$ = 1 to 4, relative to the exact derivatives of $\sin x$ and its repeated integrals (with all $C_k$ = 0). The derivative error grows by roughly a factor of nine at each order: 0.13, 1.1, 9.8, and 92. The integral error also grows, but remains small relative to the values themselves: 0.027, 0.13, 0.40, and 0.93, compared with a fourth integral that reaches 156 at $x$ = 10.

Bars on a log scale for n = 1 to 4, red derivative and blue integral: derivative 0.13, 1.1, 9.8, 92; integral 0.027, 0.13, 0.4, 0.93

Chart 17. $y_2$: RMS error of the $n$-th derivative and the $n$-th integral (Math 12)

For measured data, even a second derivative can be unreliable without smoothing; a fourth derivative may be dominated by noise. Repeated integrals stay usable. The units follow the order: the $n$-th derivative is in $y$ per $x^n$, the $n$-th integral in $y$ times $x^n$. With $y$ a position in metres and $x$ a time in seconds, the derivative is a velocity in m/s and the second derivative an acceleration in m/s², while the integral is in m·s. With $y$ a power in watts and $x$ in hours, the integral is an energy in watt-hours (W·h), while the derivative is a rate of change of power in W/h.

Uneven x

Measured data is not always evenly spaced: a data logger may skip samples, or a measurement sweep may slow down. The trapezoid rule (Math 5) uses the actual width of each panel and requires no further assumptions about spacing. For unevenly spaced data, the central-difference formula accounts for the different distances to the two neighbouring samples ($h_-$ = $x_i - x_{i-1}$, $h_+$ = $x_{i+1} - x_i$), and is still second order (Math 13):

$$y'_i \approx \frac{h_-^2\,y_{i+1} - h_+^2\,y_{i-1} + (h_+^2 - h_-^2)\,y_i}{h_+\,h_-\,(h_+ + h_-)}$$
$$\begin{aligned} y'_i \approx\; &\big(h_-^2\,y_{i+1} - h_+^2\,y_{i-1} + (h_+^2 - h_-^2)\,y_i\big) \\ &/\; \big(h_+\,h_-\,(h_+ + h_-)\big) \end{aligned}$$

Math 13.

With $h_-$ = $h_+$ it is Math 2 again. Its truncation error is about $(h_- h_+/6)\,|y'''|$, so it stays second order in the larger step as the sampling is refined, as long as neighbouring steps stay comparable. A very uneven pair still converges, but its weights grow like $1/\min(h_-, h_+)$: noise on the sample beside the short step is amplified most. Every method needs $x$ strictly increasing: the calculators sort the rows by $x$ and refuse a repeated $x$, whose step of zero would divide by zero. Chart 18 samples $\sin x$ at 25 random points, with steps from 0.01 to 1.18: the central difference still lands on $\cos x$, 0.023 RMS inside the record. Savitzky-Golay and Simpson's even-spacing form (Math 4, Math 6) assume one step; the calculator refuses uneven $x$ for Savitzky-Golay and uses SciPy's uneven-spacing Simpson.

Central-difference derivative of sin x at 25 unevenly spaced x as blue dots on a gray dashed cos x curve; tick marks along the bottom show the irregular sample positions

Chart 18. $\sin x$ at 25 uneven points: central difference (Math 13) against $\cos x$

Edges

The first sample has no neighbour to its left, and the last has none to its right. The calculators do not invent missing samples. At either endpoint, every difference method uses the one-sided difference from Math 1, directed into the data range. This estimate is first-order accurate: its error is approximately $(h/2)\,|y''|$, compared with $(h^2/6)\,|y'''|$ for the central difference at interior points. In Chart 5, this appears as the jump at $x$ = 10. The error at the last sample is −0.0259, close to the estimate below and about thirteen times the 0.0020 RMS error of the central difference in the interior. (At $x$ = 0 the one-sided error happens to be small, −0.0020, because $\sin x$ has no curvature there.) Each additional differentiation pass extends the affected edge region by one sample: in Chart 6 the second derivative is off by 0.045 at $x$ = 0. Read the values near both ends with care, and more of them the higher the order.

$$\begin{aligned} &\frac{h}{2}\,|y''(10)| = \frac{0.1}{2} \times |{-\sin 10}| = 0.027 \\[0.9em] &\text{measured: } -0.0259 \end{aligned}$$

Integration Constant and Area

The cumulative integral (Math 8) starts from a constant $C$, which cannot be determined from the measured data alone. It may represent, for example, the initial position or the charge already stored. Chart 19 shows the cumulative integral of $y_1$ from Chart 1 for $C$ = 0 and $C$ = −1. The two curves have the same shape and differ only by a vertical shift. Between $x$ = 2 and 6 both change by exactly the same amount, −1.3752: from 1.4150 to 0.0398, and from 0.4150 to −0.9602.

The cumulative integral of y1 from x = 0 to 10 twice: blue with C = 0 between 0 and 2, green with C = −1 the same shape one unit lower; dashed verticals at x = 2 and 6 with black dots where each curve crosses them

Chart 19. The cumulative integral (Math 8) of $y_1$ with two constants: the change from $x$ = 2 to 6 is the same

This change is the area over the selected range; the integration constant cancels out. The calculators report it under the chart for any range you set. The calculators report two forms of area (Math 14): signed area, where regions below the axis subtract from those above it, and absolute area, where all regions contribute positively:

$$A_{\text{signed}} = \int_a^b y\,dx = F(b) - F(a), \qquad A_{\text{absolute}} = \int_a^b |y|\,dx$$
$$\begin{aligned} A_{\text{signed}} &= \int_a^b y\,dx \\ &= F(b) - F(a) \\ A_{\text{absolute}} &= \int_a^b |y|\,dx \end{aligned}$$

Math 14.

Chart 20 shades $y_1$ from 2 to 6: blue above the axis, red below. Signed, the red outweighs the blue: −1.3752 (true $\cos 2 - \cos 6$ = −1.3763). Absolute, both add: 2.5419 (true 2.5440). Net charge is signed; total distance travelled, whatever the direction, is absolute. For the absolute area the calculators split each panel where it crosses zero, so no panel cancels itself. A range end between two samples takes the straight line between them. On the advanced calculator at order $n$ > 1, the area is taken under the $(n-1)$-th integral, so that it equals the change of the $n$-th one.

y1 from x = 0 to 10 as a black line; between dashed verticals at x = 2 and 6, the part above the axis shaded blue with a plus sign and the larger part below shaded red with a minus sign

Chart 20. $y_1$ from $x$ = 2 to 6: area above the axis adds, below subtracts in the signed area (Math 14)

Example: One Noisy Record, Differentiated and Integrated

Consider again $y_2$ from Chart 1, the noisy sine wave. Table 6 lists the central-difference derivative and cumulative trapezoid integral at each integer value of $x$, alongside their exact values. Chart 21 shows all 101 samples for both results.

$x$$y_2$derivative$\cos x$integral$1 - \cos x$
00.00001.05801.00000.00000.0000
10.85130.63700.54030.45570.4597
20.8725−0.3100−0.41611.40041.4161
30.1105−1.0475−0.99001.96191.9900
4−0.7546−0.6350−0.65361.62071.6536
5−0.9437−0.03700.28370.68720.7163
6−0.27540.98000.96020.01490.0398
70.66990.63300.75390.21710.2461
80.9808−0.3280−0.14551.11591.1455
90.4116−0.9080−0.91111.88041.9111
10−0.5501−0.5190−0.83911.80271.8391
RMS error0.12590.0267

Table 6. $y_2$: central derivative (Math 2) and cumulative trapezoid integral (Math 8) against the truth

From x = 0 to 10: red dots of y2's derivative scattered around a red dashed cos x, and blue dots of y2's integral lying smoothly on a blue dashed 1 − cos x

Chart 21. $y_2$: its derivative and integral (dots) against the true curves (dashed)

At $x$ = 5, the estimated derivative even has the wrong sign: −0.037, compared with the true slope of +0.284. The integral at the same point is off by 0.029. The same noise level (RMS 0.0178) produces an RMS error of 0.126 in the derivative and 0.027 in the integral (Math 11). For $y_2$, Savitzky-Golay reduces the derivative error by about half (Table 4). To obtain an even cleaner derivative, smooth the data first, or fit a model and differentiate the model instead: smoother, but only as right as the model.

Differentiate with care, integrate with confidence. The derivative of measured data can be dominated by noise unless that noise is reduced first.

REFs

Wikipedia, Finite difference Wikipedia, Numerical differentiation Wikipedia, Trapezoidal rule Wikipedia, Simpson's rule Wikipedia, Savitzky–Golay filter NIST DLMF §3.4, Differentiation NIST DLMF §3.5, Quadrature NumPy numpy.gradient SciPy scipy.integrate.trapezoid SciPy scipy.integrate.simpson SciPy scipy.integrate.cumulative_trapezoid SciPy scipy.integrate.cumulative_simpson SciPy scipy.signal.savgol_filter Smoothing Interpolation Curve Fitting: Which Equation Fits My Data, and How Well?