<< Diffcode Overview diffcode diffcode_der >>

diffcode >> diffcode > diffcode_CDcost

diffcode_CDcost

Objective function for optim.

Calling Sequence

[f,g,ind]=diffcode_CDcost(x,ind,fun,varargin)

Parameters

x

real vector or matrix

ind

integer parameter (see optim)

fun

function with calling sequence F=fun(x,varargin) varargin may be used to pass parameters p1,...pn

f

objective value at point x

g

gradient value at point x

Description

This function can be used in combination with optim in order to solve optimization problems without providing the gradient. In this case, the gradient computed by diffcode_CDcost is based on automatic differentiation. This may be used in the situations where the gradient is too complicated to program. This may also be used in the situations where the gradient computed by finite differences is not accurate enough. Only the function fun which computes the criterion is required.

This function should be used as follow:

      [f,xopt,gopt]=optim(list(diffcode_CDcost,fun,p1,...pn),x0,...)
    

Examples

// The function to minimize
function f=rosenbrock(x)
  f=1+sum( 100*(x(2:$)-x(1:$-1).^2).^2 + (1-x(2:$)).^2)
endfunction

x0=[1;2;3;4];
ind = 1;
[f,g,ind]=diffcode_CDcost(x,ind,rosenbrock)

[fopt,xopt,gopt]=optim(list(diffcode_CDcost,rosenbrock),[1;2;3;4])

// Function to minimize
// p is an extra-argument
function f=rosenbrockP(x, p)
  f=1+sum( p*(x(2:$)-x(1:$-1).^2).^2 + (1-x(2:$)).^2)
endfunction

x0=[1;2;3;4];
ind = 1
[f,g,ind]=diffcode_CDcost(x,ind,rosenbrockP)
[f,xopt,gopt]=optim(list(diffcode_CDcost,rosenbrockP,200),x0)

See Also

Authors


Report an issue
<< Diffcode Overview diffcode diffcode_der >>