# SQL Server GRADIENT function

Updated: 16 April 2018

Use the scalar function GRAD to numerically compute the gradient of a function

using central finite difference.

Syntax
<@Func, nvarchar(max),>
,<@VarNames, nvarchar(4000),>
,<@X, nvarchar(4000),>
,<@H, float,>)
Arguments
 Input Name Description @Func The function to be evaluated, as a string. The function must be in the form of a SELECT statement. @VarName The name of the variable @X The starting point for the minimization @H Step size
Return Type
[nvarchar](max)
Remarks
• If @Func returns a NULL then NULL Is returned.
• If @Func is not a valid SELECT statement then NULL is returned.
• If no solution is found then NULL is returned.
• If @X is NULL then @X = ''.
• If @H is NULL then @H = 0.
• If @H <= 0 then @H = .
• Available in XLeratorDB / math 2008 only
Examples
Example #1

Calculate the gradient of

at the point (1,1,1).

Since the result is returned as a string, we will use the MATRIX function to unpack the results into a vector format.

SELECT
RowNum,
ItemValue
FROM wct.MATRIX(
'SELECT POWER(@x,3) / POWER(@y,2) * POWER(@z,2) + 12*@x*@y / 2*@z'
,'@x,@y,@z'
,'1,1,1'
,NULL
)
)

This produces the following result.

Example #2

Using the same function from the previous formula we use the FDERIV function to demonstrate the calculation of the gradient.

SELECT
wct.FDERIV(REPLACE(REPLACE('SELECT POWER(@x,3) / POWER(@y,2) * POWER(@z,2) + 12*@x*@y / 2*@z','@y',1),'@z',1),'@x',1,1,NULL,'C') as [dF/dx]
,wct.FDERIV(REPLACE(REPLACE('SELECT POWER(@x,3) / POWER(@y,2) * POWER(@z,2) + 12*@x*@y / 2*@z','@x',1),'@z',1),'@y',1,1,NULL,'C') as [dF/dy]
,wct.FDERIV(REPLACE(REPLACE('SELECT POWER(@x,3) / POWER(@y,2) * POWER(@z,2) + 12*@x*@y / 2*@z','@x',1),'@y',1),'@z',1,1,NULL,'C') as [dF/dz]

This produces the following result.