Keywords - Function groups - @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Library: nummath
See also: nmgraditer nmjacobian

Quantlet: nmgraddiff
Description: Computes the gradient of a function func at a point x0 using the symmetric difference with a step h: graddiff(f,x,h) = [f(x+h) - f(x-h) / (2*h)]

Usage: grad = nmgraddiff(func, x0{, h})
Input:
func name of function (string) whose gradient is to be computed. The function should take just one parameter x, which is n x k matrix (k >= 1) x = (x1,x2,...,xk); xi (n x 1 vector; i=1..k) represent points in which the function should be evaluated. As a result, the function should return k real numbers in a form of 1 x k vector.
x0 n x 1 vector, the point at which gradient is to be computed
h optional n x 1 vector or scalar (in the latter case, h <- h * matrix(n) will be used); vector of stepsizes for partial derivatives; default value is 0.00001
Output:
grad n x 1 vector, computed gradient

Example:
library("nummath")
;
; definition of function f(x) = 1 - x1 + x2^2
;
proc(r)=f(x)
  r=1-x[1,]+x[2,]^2
endp
;
grad = nmgraddiff("f",#(1,2))
grad

Result:
Contents of grad
[1,]       -1
[2,]        4
Example:
library("nummath")
grad = nmgraddiff("sin",0)
grad

Result:
Contents of grad
[1,]        1



Author: L. Cizkova, 20010505 license MD*Tech
(C) MD*TECH Method and Data Technologies, 05.02.2006