%Numerical Optimization
%HW 2
%Aamod Pethe
function c = countneval(n,x)    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This function counts the number of evaluations of function,its gradient
%and/or hessian. Declare inputs in MATLAB window.
%type counteval(n,x) to call the script
%Inputs: i)n : n is a 1X3 vector eg. [1 0 1] means we are counting function
%and hessian evaluation, but not gradient evaluation.
%n = [0 0 0] resets counters to 0
%Any value other than 0 or 1 in the vector will display 'wrong input'
%message.
%       ii)x : 1X2 vector for evaluating
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global c1 c2 c3     %Sets counter values
if isempty(c1)
    c1 = 0;
end
if isempty(c2)
    c2 = 0;
end
if isempty(c3)
    c3 = 0;
end
%counter = [c1,c2,c3];   %Counter vector
if n == [0 0 0]     %Resets counter values to 0
    c1=0;
    c2=0;
    c3=0; 
end
if ((n(1)==0)|(n(1)==1))&((n(2)==0)|(n(2)==1))&((n(3)==0)|(n(3)==1))    %Filters out non binary input
    if n(1)==1
    f=(exp(sin(50*x(1)))+sin(60*exp(x(2)))+sin(70*sin(x(1)))+sin(sin(80*x(2)))-sin(10*x(1)+10*x(2))+0.25*(x(1).^2 + x(2).^2)) %Function evaluation
    c1 = c1+1;   %increments function counter
    end
    if n(2)==1
     fx=50*cos(50*x(1))*exp(sin(50*x(1)))+70*cos(70*sin(x(1)))*cos(x(1))-10*cos(10*x(1)+10*x(2))+1/2*x(1);
     fy=60*cos(60*exp(x(2)))*exp(x(2))+80*cos(sin(80*x(2)))*cos(80*x(2))-10*cos(10*x(1)+10*x(2))+1/2*x(2);
     g=[fx fy]' %Gradient
    c2 = c2+1;   %increments gradient evaluation counter
    end
    if n(3)==1
    h = zeros(2);
    h(1)= -2500*sin(50*x(1))*exp(sin(50*x(1)))+2500*cos(50*x(1))^2*exp(sin(50*x(1)))-4900*sin(70*sin(x(1)))*cos(x(1))^2-70*cos(70*sin(x(1)))*sin(x(1))+100*sin(10*x(1)+10*x(2))+1/2;
    h(2)= 100*sin(10*x(1)+10*x(2));
    h(3)= 100*sin(10*x(1)+10*x(2));
    h(4)= -3600*sin(60*exp(x(2)))*exp(x(2))^2+60*cos(60*exp(x(2)))*exp(x(2))-6400*sin(sin(80*x(2)))*cos(80*x(2))^2-6400*cos(sin(80*x(2)))*sin(80*x(2))+100*sin(10*x(1)+10*x(2))+1/2;
    h           %Hessian
    c3 = c3+1;   %increments hessian evaluation counter
    end
   [c1,c2,c3]   %Displays the counter
else
    disp('wrong input')
end
