Calculus

Calculus#

Problem: Calculate derivative and integral of a curve, given the vectors x and y.

Test data:

⎕IO0
x0.25×⍳100
f1+x  g10-(x*0.5)+x÷2  h10×1x÷5
]plot (f g h h)x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels -10 -8 -6 -4 -2 0 2 4 6 8 10 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Points follow ... Line Points follow ... Line Points follow ... Line Reset to original origin

Derivative#

The derivative will be calculated as a simple ratio of the difference between two consequtive values. The differences will be calculated using the n-wise reduction operator (dyadic /).

dy2-/f  dx2-/x  ddy÷dx
]plot d x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Reset to original origin

We better define a function:

D÷(2-/)
]plot ((f D x)(g D x)(h D x)) x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Points follow ... Line Points follow ... Line Reset to original origin

Noise#

The problem of calculating the derivative is more complicated if the function is not smooth. For example:

nf+0.1-0.2×?0f
]plot (n (n D x)) x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Points follow ... Line Reset to original origin

In such a case, it would be convenient to first perform some smoothing of the function, for example calculating a running average:

ns xs10(+/÷⊣)¨n x
]plot (ns (ns D xs)) xs
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels -0.5 0 0.5 1 1.5 2 2.5 3 3.5 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Points follow ... Line Reset to original origin

Or defining a smoothing function:

S(+/÷⊣)¨
]plot 10 D/⍤S n x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels -0.05 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 for X-axis labels 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Reset to original origin

Integral#

To calculate the value of the integral we need to add, each step, the increment of the x value multiplied by the current value of the function at that point. Several options can be used to estimate the value of the function (previous point, next point, average, …). Here, the average of the value at the previous and next point is used.

∆x¯2-/x  y2(+/÷⊣)f  i+\∆x×y
]plot i x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels 0 5 10 15 20 25 30 35 40 45 50 55 60 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Reset to original origin

Again, we define a function to facilitate calculating the integral for any curve:

I+\(¯2-/)×2(+/÷⊣)
]plot ((f I x)(g I x)(h I x))x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels -60 -40 -20 0 20 40 60 80 100 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Points follow ... Line Points follow ... Line Reset to original origin

If we are only interested in the total value of the integral, we simply take the last value:

(f I x)(g I x)(h I x)
58.89219624 ¯12.29837376 38.22296274

Notice that no special action is needed to calculate the integral of noisy functions, although using a smoothed function is also possible:

]plot ((n I x)(0,⊃10 I/⍤S n x)) x
Created by Causeway SVG engine - SharpPlot v3.71.0 Paint the paper ===== Border ===== Region ===== X-Axis Ticks ===== X-Axis tickmarks Y-Axis Ticks ===== Y-Axis tickmarks Axes ===== Y-axis labels 0 5 10 15 20 25 30 35 40 45 50 55 60 for X-axis labels 0 2 4 6 8 10 12 14 16 18 20 22 24 26 Heading, subheading and footnotes ===== Start of Line Chart =========== Points follow ... Line Points follow ... Line Reset to original origin