function min_demo()
    %use matlab fminunc (fmin unconstrained)
    
    x0= [10;10];
    
    
    % BFGS Quasi-Newton method
    options = optimset('GradObj','off');
    tic
    [x,fval,exitflag,output] = fminunc(@myobj,x0,options);
    toc
    x
    fval
    
    
    % CG
    options = optimset('GradObj','on');
    tic
    [x,fval,exitflag,output] = fminunc(@myobjGrad,x0,options);
    toc
    x
    fval
    
    
    
    
    %uses newton's method
    options = optimset('GradObj','on');
    tic
    [x,fval,exitflag,output] = fminunc(@myobjHess,x0,options);
    toc
    x
    fval
    output
    
end



function f_val = myobj(x)
   % evaluates f = 0.5*(x'Ax) -b'x; x is a 2*1 vector  
    A = [3 2; 2 6];
    b = [2;-8];
    f_val = 0.5*(x'*A*x) -b'*x;
end    
    
function [f_val,g_val] = myobjGrad(x)
    % evaluates f = 0.5*(x'Ax) -b'x; x is a 2*1 vector  
    % also computes the corresponding gradient
    A = [3 2; 2 6];
    b = [2;-8];
    f_val = 0.5*(x'*A*x) -b'*x;
    g_val = A*x-b;   
end

function [f_val,g_val,h_val] = myobjHess(x)
    % evaluates f = 0.5*(x'Ax) -b'x; x is a 2*1 vector  
    % also computes the corresponding gradient
    %also computes the corresponding hessian
    A = [3 2; 2 6];
    b = [2;-8];
    f_val = 0.5*(x'*A*x) -b'*x;
    g_val = A*x-b;   
    h_val = A;
end

