Contents Previous Next Subchapters Current Chapters-> interp interp1 lagrange polyfit smospl cubespl cubeval interp2 mlmode_interp2 snewton brent linlsq linlsqb nlsq dnlsq nlsqbox dnlsqb conjdir neldermead conjgrad minline lemke Qpos Qbox Qpro sqp relative fordiff cendiff autodiff testder testgrad testhess Parent Chapters-> Omatrix6 fit testhess Search Tools-> contents reference index search

Testing Calculation of The Hessian of a Function
 Syntax `testhess(function `fval`, `x0`, `h`)` See Also testgrad , testder

Description
Given a routine fval that calculates the gradient and Hessian of a scalar valued function `f(x)`, `testhess` compares these calculations with a central difference approximation. (The Hessian if the second derivative.) The column vector x0 specifies the value of `x` at which to check the calculations. The column vector h has the same type and dimension as x0. It specifies the step size to use for each component when computing the central difference approximations. ``` ```fval`(`x1`, `fout```) ```fval`(`x1`, `fout`, `gout```) ```fval`(`x1`, `fout`, `gout`, `Hout```) ```The column vector x1 has the same type and dimension as x0 and specifies the point at which to calculate the value of `f(x)`. The input value of fout does not matter. Its output value is a scalar and equal to the value `f(x1)`. If gout is present, its input value does not matter. Its output value is a column vector with the same type and dimension as x1 and ```                d f(x)  |          gout   =  ------- |          i      d x    | x = x1                    i ```If Hout is present, its input value does not matter. Its output value is a square matrix with the same type and row dimension as x1 and ```                  d    d f(x) |          Hout    =  ----- ------ |          i,j     d x   d x   | x = x1                     i     j ```First the gradient calculation is compared with central differences of the function values. Then the Hessian calculation is compared with central differences of the gradient values. This is done for each component of `x` by printing the values returned by fval, the central difference approximation, and the corresponding relative error in the command window. (Note that in the output that checks the Hessian, gradient refers to the gradient of the derivative which is the Hessian.)

Example
The following program checks the gradient and Hessian calculation for the function `f(x)` where ```                                  / x  \               / 0   1 \                        __        |  2 |     __2       |       |      f(x) = x  * x  ,  \/ f(x) = |    |  ,  \/ f(x) = |       |              1    2              | x  |               |       |                                  \  1 /               \ 1   0 / `````` clear function fval(x1, fout, gout, Hout) begin      fout = x1(1) * x1(2)      if arg(0) >= 3 then ...           gout = {x1(2), x1(1)}      if arg(0) >= 4 then ...           Hout = {[0., 1.], [1., 0.]} end x0 = {1., 1.} h  = {.001, .001} testhess(function fval, x0, h) ```