MATLAB: Not enough input arguments

fminuncfunction

I am minimizing a function that I created. Getting "Not enough input arguments".
This is the main code:
y=data;
y0=0;
global y y0;
theta0=[0.5, 0.5];
[x,fval,exitflag,output,grad,hessian] = fminunc(condlike,theta0)
This is the function file:
function [ f ] = condlike(x )
Conditional likelihood evaluation for AR(1)
global y y0
[m,n]=size(y);
g = normpdf(y(1,1),x(1)*y0,x(2));
for i=1:n-1
g=g+log(normpdf(y(1,i+1),x(1)*y(1,i),x(2)));
end
f=-g;
end

Best Answer

You forgot to make the first input a function handle, using an @ character. The code below ran without errors for me.
data=rand(10,1);
global y y0;
y=data;
y0=0;
theta0=[0.5, 0.5];
[x,fval,exitflag,output,grad,hessian] = fminunc(@condlike,theta0);
function [ f ] = condlike(x )
%Conditional likelihood evaluation for AR(1)
global y y0
[~,n]=size(y);
g = normpdf(y(1,1),x(1)*y0,x(2));
for i=1:n-1
g=g+log(normpdf(y(1,i+1),x(1)*y(1,i),x(2)));
end
f=-g;
end