Contents
- derivative of exp(x), at x == 0
- DERIVEST can also use an inline function
- Higher order derivatives (second derivative)
- Higher order derivatives (third derivative)
- Higher order derivatives (up to the fourth derivative)
- Evaluate the indicated (default = first) derivative at multiple points
- Specify the step size (default stepsize = 0.1)
- Provide other parameters via an anonymous function
- The second derivative should be positive at a minimizer.
- Compute the numerical gradient vector of a 2-d function
- Compute the numerical Laplacian function of a 2-d function
- Compute the derivative of a function using a central difference scheme
- Compute the derivative of a function using a forward difference scheme
- Compute the derivative of a function using a backward difference scheme
- Although a central rule may put some samples in the wrong places, it may still succeed
- But forcing the use of a one-sided rule may be smart anyway
- Control the behavior of DERIVEST - forward 2nd order method, with only 1 Romberg term
- Functions should be vectorized for speed, but its not always easy to do.
% DERIVEST demo script % This script file is designed to be used in cell mode % from the matlab editor, or best of all, use the publish % to HTML feature from the matlab editor. Older versions % of matlab can copy and paste entire blocks of code into % the Matlab command window. % DERIVEST is property/value is driven for its arguments. % Properties can be shortened to the
derivative of exp(x), at x == 0
[deriv,err] = derivest(@(x) exp(x),0)
deriv = 1.0000 err = 1.5088e-014
DERIVEST can also use an inline function
[deriv,err] = derivest(inline('exp(x)'),0)
deriv = 1.0000 err = 1.5088e-014
Higher order derivatives (second derivative)
Truth: 0
[deriv,err] = derivest(@(x) sin(x),pi,'deriv',2)
deriv = -5.5372e-019 err = 1.8650e-018
Higher order derivatives (third derivative)
Truth: 1
[deriv,err] = derivest(@(x) cos(x),pi/2,'der',3)
deriv = 1.0000 err = 2.0718e-012
Higher order derivatives (up to the fourth derivative)
Truth: sqrt(2)/2 = 0.707106781186548
[deriv,err] = derivest(@(x) sin(x),pi/4,'d',4)
deriv = 0.7071 err = 1.9122e-005
Evaluate the indicated (default = first) derivative at multiple points
[deriv,err] = derivest(@(x) sin(x),linspace(0,2*pi,13))
deriv = 1.0000 0.8660 0.5000 0 -0.5000 -0.8660 -1.0000 -0.8660 -0.5000 0 0.5000 0.8660 1.0000 err = 1.0e-012 * 0.0033 0.0039 0.0251 0 0.0126 0.0290 0.0031 0.0298 0.0488 0 0.0035 0.1291 0.0043
Specify the step size (default stepsize = 0.1)
deriv = derivest(@(x) polyval(1:5,x),1,'deriv',4,'FixedStep',1)
deriv = 24.0000
Provide other parameters via an anonymous function
At a minimizer of a function, its derivative should be essentially zero. So, first, find a local minima of a first kind bessel function of order nu.
nu = 0; fun = @(t) besselj(nu,t); fplot(fun,[0,10]) x0 = fminbnd(fun,0,10,optimset('TolX',1.e-15)) hold on plot(x0,fun(x0),'ro') hold off deriv = derivest(fun,x0,'d',1)
x0 = 3.8317 deriv = -2.3301e-009

The second derivative should be positive at a minimizer.
deriv = derivest(fun,x0,'d',2)
deriv = 0.4028
Compute the numerical gradient vector of a 2-d function
Note: the gradient at this point should be [4 6]
fun = @(x,y) x.^2 + y.^2; xy = [2 3]; gradvec = [derivest(@(x) fun(x,xy(2)),xy(1),'d',1), ... derivest(@(y) fun(xy(1),y),xy(2),'d',1)]
gradvec = 4.0000 6.0000
Compute the numerical Laplacian function of a 2-d function
Note: The Laplacian of this function should be everywhere == 4
fun = @(x,y) x.^2 + y.^2; xy = [2 3]; lapval = derivest(@(x) fun(x,xy(2)),xy(1),'d',2) + ... derivest(@(y) fun(xy(1),y),xy(2),'d',2)
lapval = 4
Compute the derivative of a function using a central difference scheme
Sometimes you may not want your function to be evaluated above or below a given point. A 'central' difference scheme will look in both directions equally.
[deriv,err] = derivest(@(x) sinh(x),0,'Style','central')
deriv = 1 err = 2.0824e-015
Compute the derivative of a function using a forward difference scheme
But a forward scheme will only look above x0.
[deriv,err] = derivest(@(x) sinh(x),0,'Style','forward')
deriv = 1.0000 err = 4.3314e-015
Compute the derivative of a function using a backward difference scheme
And a backward scheme will only look below x0.
[deriv,err] = derivest(@(x) sinh(x),0,'Style','backward')
deriv = 1.0000 err = 4.3314e-015
Although a central rule may put some samples in the wrong places, it may still succeed
[d,e,del]=derivest(@(x) log(x),.001,'style','central')
d = 1.0000e+003 e = 1.7156e-010 del = 3.0518e-005
But forcing the use of a one-sided rule may be smart anyway
[d,e,del]=derivest(@(x) log(x),.001,'style','forward')
d = 1000.0000 e = 6.5546e-008 del = 1.2207e-004
Control the behavior of DERIVEST - forward 2nd order method, with only 1 Romberg term
Compute the first derivative, also return the final stepsize chosen
[deriv,err,fdelta] = derivest(@(x) tan(x),pi,'deriv',1,'Style','for','MethodOrder',2,'RombergTerms',1)
deriv = 1.0000 err = 2.8379e-013 fdelta = 0.0012
Functions should be vectorized for speed, but its not always easy to do.
[deriv,err] = derivest(@(x) x.^2,0:5,'deriv',1) [deriv,err] = derivest(@(x) x^2,0:5,'deriv',1,'vectorized','no')
deriv = 0 2.0000 4.0000 6.0000 8.0000 10.0000 err = 1.0e-013 * 0 0.0807 0.1613 0.3226 0.3226 0.4712 deriv = 0 2.0000 4.0000 6.0000 8.0000 10.0000 err = 1.0e-013 * 0 0.0807 0.1613 0.3226 0.3226 0.4712