Logo

NEWTON

Updated 2023-10-18 20:53:32.243000

Syntax

SELECT [westclintech].[wct].[NEWTON] (
   <@Func, nvarchar(max),>
  ,<@VarName, nvarchar(4000),>
  ,<@X, float,>
  ,<@DFunc, nvarchar(max),>
  ,<@MaxIter, int,>
  ,<@tol, float,>)

Description

Use the scalar function NEWTON to find the root of a univariate function.

Arguments

@tol

Absolute tolerance.

@VarName

The name of the variable.

@MaxIter

Maximum number of iterations.

@DFunc

A function to compute the derivative. If NULL, a numeric derivate will be computed.

@X

The starting value for the evaluation.

@Func

The function to be evaluated, as a string. The function must be in the form of a SELECT statement.

Return Type

float

Remarks

If Func or Dfunc returns a NULL then NULL Is returned.

If Func or Dfunc is not a valid SELECT statement then NULL is returned.

If the derivative of x evaluates to zero, then NULL Is returned.

If no solution is found then NULL is returned.

If X is NULL then X = 0.

If MaxIter is NULL then MaxIter = 100.

If tol is NULL then tol = 0.

If tol <= 0 then tol = 0.0000000149011612.

Examples

Example #1

Let’s find the root of the Legendre polynomial of degree 5.

SELECT wct.NEWTON('SELECT (63 * POWER(@x,5) - 70 * POWER(@x,3) + 15 * @x)/8e+00',

          '@x', 1.0, NULL, NULL, NULL) as newton;

This produces the following result.

{"columns":[{"field":"newton","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"newton":"0.906179845938504"}]}

Example #2

Same as the previous example, except that we supply the derivate function rather than have NEWTON use a finite difference calculation.

SELECT wct.NEWTON(

                     'SELECT (63 * POWER(@x,5) - 70 * POWER(@x,3) + 15 * @x)/8e+00',

                     '@x',

                     1.0,

                     'SELECT (315 * POWER(@x,4) - 210 * POWER(@x,2) + 15)/8e+00',

                     NULL,

                     NULL

                 ) as newton;

This produces the following result.

{"columns":[{"field":"newton","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"newton":"0.906179845938665"}]}

Example #3

In this example we call the PRICE function to calculate the yield on a bond using the NEWTON function and compare that with value returned by the YIELD function.

SELECT wct.NEWTON(

                     'SELECT wct.PRICE(

             ''2018-04-25''

            ,''2025-05-15''

            ,.04

            ,@Y

            ,100

            ,2

            ,0) - 98.25',

                     '@Y',

                     .04,

                     NULL,

                     NULL,

                     1E-08

                 ) as newton,

       wct.YIELD('2018-04-25', '2025-05-15', .04, 98.25, 100, 2, 0) as yield;

This produces the following result.

{"columns":[{"field":"newton","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"yield","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"newton":"0.0428973950775513","yield":"0.0428973950748617"}]}

See Also

BFGS - Broyden-Fletcher-Goldfarb-Shanno (BFGS) method to find the minimum of a functionBRENT - Find the root of a continuous function of one variable

FDERIV - Numerical function differentiation for orders n = 1 to 8 using finite difference approximations

HESSIAN - Numerically computer the Hessian matrix

JACOBIAN - Numerically compute the Jacobian matrix

SECANT - Find the root of single-variable continuous function.

BRENT - Find the root of a continuous function of on variable

GRAD - Numerically compute the gradient.