QUADGK
Updated 2024-03-07 15:01:42.627000
Syntax
SELECT [westclintech].[wct].[QUADGK](
<@Func, nvarchar(max),>
,<@VarName, nvarchar(4000),>
,<@A, float,>
,<@B, float,>)
Description
Use the scalar function QUADGK to evaluate a finite integral. QUADGK uses 21-point Gauss-Kronrod quadrature.
Arguments
@A
The lower limit of integration. @A must be of a type float or of a type that implicitly converts to float.
@B
The upper limit of integration. @B must be of a type float or of a type that implicitly converts to float.
@VarName
the TSQL variable name. The variable name must start with '@'. @VarName must be of a type nvarchar or of a type which implicitly converts to nvarchar.
@Func
the function to be integrated. @Func is a string containing any valid TSQL statement which includes a single variable that is the object of the integration. The variable name is defined in @VarName. @Func is of a type nvarchar or of any type which implicitly converts to nvarchar.
Return Type
float
Remarks
If @A not less than @B an error will be returned.
If @Func contains an undeclared SQL variable and it is not defined in @VarName a NULL will be returned.
Examples
In this example we want to evaluate the integral:
\int_0^1\frac{1}{\sqrt{x}}
SELECT wct.QUADGK( 'SELECT 1/SQRT(@x)', --@Func
'@x', --@VarName
0, --@A
1 --@B
) as Integral;
This produces the following result.
{"columns":[{"field":"Integral","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Integral":"2"}]}
In this example we want to evaluate the integral:
\int_0^2\sqrt{4-x^2}
SELECT wct.QUADGK( 'SELECT SQRT(4-POWER(@x,2))', --@Func
'@x', --@VarName
0, --@A
2 --@B
) as Integral;
This produces the following result.
{"columns":[{"field":"Integral","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Integral":"3.14159265358979"}]}
In this example we want to evaluate the integral:
\int_{-2}^2\frac{\pi}{2}\cosh{x}\sin{e^{\frac{\pi}{2}\sinh{x}}}
Note that COSH, the hyperbolic cosine function, and SINH, the hyperbolic sine function, are not a built-in SQL Server functions but are part of the XLeratorDB library.
SELECT wct.QUADGK( 'SELECT PI()/2e+00*wct.COSH(@x)*SIN(EXP(PI()/2e+00*wct.SINH(@x)))', --@Func
'@x', --@VarName
-2, --@A
2 --@B
) as Integral;
This produces the following result.
{"columns":[{"field":"Integral","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"Integral":"1.57044006384932"}]}
See Also
QUAD - Gauss-Kronrod 15-point quadrature
QUADDE - Double Exponential quadrature for non-periodic functions
QUADOSC - Double exponential quadrature for periodic functions