# Difference between revisions of "Integral"

 Z←{L} f∫ R returns the definite Integral of the function f between the points L and R.
L is an optional Real numeric singleton which represents the lower bound of the definite Integral. If it is omitted, 0 is used.
R is a Real numeric singleton which represents the upper bound of the definite Integral.
f is an arbitrary monadic function whose argument and result are both Real numeric singletons.

## Introduction

According to Wikipedia, "In analysis, numerical integration comprises a broad family of algorithms for calculating the numerical value of a definite integral".

This implementation uses several different algorithms to achieve this as well as extends these algorithms to Infinite and Left and Right semi-infinite intervals.

## Notation

The symbol chosen for this operator is the Integral Sign (), entered from the keyboard as Alt-’S’ or Alt-Shift-'s' (U+222B), and used in mathematics for Integration.

## Applications

There are many, many applications of Numerical Integration. Here are but a few taken from Whitman College's Mathematics Department's Calculus Online course:

and more taken from the website Interactive Mathematics:

## Variants

There are several different algorithms that may be used for Numerical Integration; the main (and default) one is based upon the Tanh-Sinh Quadrature code in David H. Bailey's ARPREC package. Other algorithms include Gauss-Legendre and Newton-Cotes taken from Laurent Fousse's CRQ (Correctly Rounded Quadrature) Numerical Integration code written in MPFR as described in this paper. These algorithm may be selected via the Variant operator as in

```      ⎕FPC←128
{1+1○⍵}∫⍠'g' ○2x  ⍝ Gauss-Legendre
6.28318530717958647692528676655900576839
{1+1○⍵}∫⍠'n' ○2x  ⍝ Newton-Cotes
6.28318530717958647692528676655900571385
6.28318530717958647692528676655900576839
```

The Order of the Numerical Integration (the number of rectangles used to approximate the result) is, by default, 128 for the Gauss-Legendre algorithm, and 32 for Newton-Cotes. That number may be changed via the Variant operator as in

```      N←17x
⎕PP←60 ⋄ ⎕FPC←512
{÷1+⍵*2}∫⍠'g' N
1.51204050407917392632913838918797965662193428158710826434397
{÷1+⍵*2}∫⍠'n' N
1.51204050407917392632913834207540700696171248671126465236073
{÷1+⍵*2}∫⍠('n' 64) N
1.51204050407917392632913838918797965662193428158722152948792
{÷1+⍵*2}∫⍠('n' 96) N
1.51204050407917392632913838918797965662193428158710826434397
```

There are additional choices for algorithms such as

Algorithm Syntax Source
Gauss-Legendre ⍠'G' David H. Bailey's ARPREC ARPREC C++ code
Error Function ⍠'E' David H. Bailey's ARPREC ARPREC C++ code
Tanh-Sinh ⍠'T' David H. Bailey's ARPREC ARPREC C++ code
Tanh-Sinh ⍠'t' and
as it is the default algorithm
David H. Bailey's ARPREC ARPREC C++ code translated to C
Gauss-Legendre ⍠'g' Laurent Fousse's CRQ code
Newton-Cotes ⍠'n' Laurent Fousse's CRQ code
Tanh-Sinh ⍠'d' Graeme Dennes Excel Spreadsheet code

When the Variant operator is applied to the default algorithm, the first number is used to replace the exponent N=5 in the loop limit of 12×2*N.

Not all of these additional algorithm's may be supported in future versions.

## Examples

The formula for the Unit Parabola is {⍵*2} whose Integral is {1r3×⍵*3}, and so the Integral of the Unit Parabola from -a to a is -/{1r3×⍵*3}a,-a ←→ 2r3×a*3:

```      ⎕FPC←128
a←2 ⋄ (-a){⍵*2}∫a
5.33333333333333333333333333333333333334
```

This may be used to calculate the Area Enclosed Between a Parabola and a Chord, the formula for which is two-thirds of the area of the surrounding rectangle of the chord, the line tangent to the Vertex, and the two sides x=-a and x=a.

Because the Unit Parabola curves upward and away from the X-axis, the above Integral returns the area under (i.e. below and outside) the Parabola. In this case, because we're Integrating from -a to a, the chord is the line y={⍵*2} a ←→ y=a*2. The top of the rectangle (chord) is of length a--a ←→ 2×a and of height y ←→ a*2, whose area is (2×a)×a*2 ←→ 2×a*3. Subtracting the result of the Integral from the rectangle's area and multiplying this by two-thirds yields the area enclosed between a chord and a Parabola.

```      2r3×(2×a*3)-(-a){⍵*2}∫a
7.1111111111111111111111111111111111111
```

The Integral of the 1+Sine function from 0 to ○2 is ○2:

```      {1+1○⍵}∫○2x ⋄ ○2x
6.28318530717958647692528676655900576834
6.28318530717958647692528676655900576839
```

and the Integral of the Sine function from 0 to ○2 is (essentially) 0

```      {1○⍵}∫○2x
6.69453502284549420282962009846618284241E¯42
```

A Normal Distribution is defined as nd←{(*¯0.5×⍵*2)÷√○2x}. Because this instance of the algorithm is scaled properly, integrating it over the entire width from ¯∞ to yields an answer of 1 (the area under the curve). However, to achieve this result, we need to increase the default loop limit exponent:

```      ¯∞ nd∫ ∞
1.000000000000002522695227400560226259006
¯∞ nd∫⍠10 ∞
1
```

Integrating this same function for one, two, and three standard deviations on either side yields the 3-sigma rule of :

```      ⍪¯1 ¯2 ¯3 nd∫¨ 1 2 3
0.682689492137085897170465091264075844958    ⍝ 68%
0.954499736103641585599434725666933125059    ⍝ 95%
0.997300203936739810946696370464810045244    ⍝ 99.7%
```

which describes about how many of the values in a normal distribution lie within one, two, and three standard deviations from the mean.

## Numerical Differentiation

Note that Derivative, the inverse of this operator, has also been implemented. These two features may be combined in various ways, for example, to calculate Arc Length.

The formula for Arc Length is simply arclen←{4○⍺⍺∂⍵} which can be used on any given function. For example, the upper half of a unit circle is represented by the function {0○⍵}. The interval between [-√0.5, √0.5] is then a quarter of a circle, whose length is

```      ⎕FPC←512 ⋄ ⎕PP←60
(-√0.5x){0○⍵}arclen∫√0.5x
1.57079632679489661923132169163975144209858469968755291048747
○0.5x     ⍝ Exact length
1.57079632679489661923132169163975144209858469968755291048747```