function y = objective (x, ord)
%OBJECTIVE  Values about the objective function
%
%   y = objective(x, ord) = d^ord y / d x^ord at point x.

%   Ling Li, Caltech
%   05/04/2001

A = [ 5  1  0  .5; ...
      1  4  .5 0; ...
      0  .5 3  0; ...
      .5 0  0  2];
s = 10;

A = (A + A') / 2;

if nargin < 2,
    ord = 0;
end

if ord == 0,
    %     1  T     s   T   2
    % y = - x  x + - (x Ax)
    %     2        4
    y = x' * x / 2 + s * (x' * A * x)^2 / 4;

elseif ord == 1,
    %             T
    % y' = x + s (x Ax) Ax
    y = x + s * (x' * A * x) .* (A * x);

elseif ord == 2,
    %                       T       T
    % y'' = I + 2s (Ax) (Ax)  + s (x Ax) A
    % For y'', we do not accept a list of x.
    tmp = A * x;
    y = eye(size(A)) + s .* (2 .* (tmp * tmp') + (x' * tmp) .* A);

else
    error('ord should be 0, 1, or 2');
end
