Contents | Previous | Next | Subchapters |

Syntax |
`testgrad(function ` fval`, ` x0`, ` h`)` |

See Also | testder , testhess |

Given a routine

*f*(*x*)

,
`testgrad`

compares this calculation
with a central difference approximation.
(The gradient is the transpose of the derivative.)
The results of the comparison are printed in the command window.
The column vector *x*

at which to check the calculation of the gradient.
The column vector *x*

when
computing the central difference approximations to the
derivative of *f*(*x*)

.

`(`

`, `

`)`

`(`

`, `

`, `

`)`

The column vector *f*(*x*)

.
The input value of *f*(*x*1)

.
If

*d* *f* (*x*) |

*j* |

*gout* = -------- |

*i*,*j* *d* *x* | *x* = *x*1

*i*

For each component of *x*

,
the partials returned by The following program checks the gradient calculation for the function

*f*(*x*)

where

/ *x* \ __ |

*f*(*x*) = | | , \/ *f*(*x*) | = (1, 2)

\ *x*^2 / |*x* = 1

```
```

clear

function fval(x1, fout, gout) begin

fout = {x1, x1^2}

if arg(0) == 3 then ...

gout = [1, 2 * x1]

end

x0 = 1.

h = .001

testgrad(function fval, x0, h)