-1
$\begingroup$

I am trying to find the minimum of this function. But I receive the following error when I run the script. What am I doing wrong:

            % pre allocating
            x=zeros(10,4);
            mu=zeros(10,1);% process parameter
            T=zeros(10,1);% spike times
            y=zeros(10,1);%number of spikes
            eta=zeros(10,1);% linear predictor
            for i=1:10 % on trials
                for j=1:4 % on pixels
                    x(i,j)=randi(2,1)-1;
                end
                mu(i)=randi(20,1)/100;
                [T,y(i)]=ppsample(mu(i),20);
                eta(i)=log(mu(i));
                syms b0;syms b1;syms b2;syms b3;syms b4;syms objfun;
                o(i)=((b0+b1*x(i,1)+b2*x(i,2)+b3*x(i,3)+b4*x(i,4))*y(i))-exp(b0+b1*x(i,1)+b2*x(i,2)+b3*x(i,3)+b4*x(i,4));
            end
            %%finding optimum weights

            objfun=sum(o);
            ofun= matlabFunction(objfun);
            x0 = ones(1,5);
            [x,fval] = fminunc(ofun,x0);

This gives the following:

Error using makeFhandle/@(b0,b1,b2,b3,b4)b0.*1.9e1+b1.*9.0+b2.*1.2e1+b3.*7.0+b4.*7.0-exp(b0+b1+b2+b3)-exp(b0+b2+b3+b4)-exp(b0+b1).*2.0-exp(b0+b2)-exp(b0)-exp(b0+b1+b2)-exp(b0+b1+b3)-exp(b0+b2+b4)-exp(b0+b3+b4)
Not enough input arguments.

Error in fminunc (line 254)
    f = feval(funfcn{3},x,varargin{:});

Error in GLM (line 30)
[x,fval] = fminunc(ofun,x0);

Caused by:
    Failure in initial user-supplied objective function evaluation.
    FMINUNC cannot continue.
$\endgroup$
1
  • 1
    $\begingroup$ This is off topic here since it's code debugging and can probably be best solved by reading the manual (help fminunc). I'm guessing this is because functions supplied to fminunc must take a single (possibly vector) input and it looks like you function expects multiple inputs. I don't know what ppsample is so I can't test your code. $\endgroup$ – Doug Lipinski Jul 5 '15 at 18:18
1
$\begingroup$

Your syntax for the objective function evaluation is wrong.

The code below should work. The 1 by 5 vector b is your optimization variable. It inherits its dimensions from the dimensions of x0 in the fminunc call. I got rid of all the syms stuff. Note that you need @ before your objective function name in the call to fminunc so that it becomes a function handle.

                % Put this in the file ofun.m somewhere in your MATLAB path                
                function [objective] = ofun(b)
                % The line below is so that your expression for o(i) is left unchanged
                b0 = b(1); b1 = b(2); b2 = b(3); b3 = b(4); b4 = b(5);
                % pre allocating
                x=zeros(10,4);
                mu=zeros(10,1);% process parameter
                T=zeros(10,1);% spike times
                y=zeros(10,1);%number of spikes
                eta=zeros(10,1);% linear predictor
                for i=1:10 % on trials
                    for j=1:4 % on pixels
                        x(i,j)=randi(2,1)-1;
                    end
                    mu(i)=randi(20,1)/100;
                    [T,y(i)]=ppsample(mu(i),20);
                    eta(i)=log(mu(i));
                    o(i)=((b0+b1*x(i,1)+b2*x(i,2)+b3*x(i,3)+b4*x(i,4))*y(i))-exp(b0+b1*x(i,1)+b2*x(i,2)+b3*x(i,3)+b4*x(i,4));
                end
                objective = sum(o);
                end
                % end of what goes in ofun.m

                %%finding optimum weights

                x0 = ones(1,5);
                [x,fval] = fminunc(@ofun,x0);
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.