![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175742/image.png)
fminsearch not working, a few errors
6 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm a novice when it comes to this and I was wondering if someone could help me. I have the following function:
function [prob]=prob(alpha,beta,L32,L33,L42,L43,L44)
%Defining global variables thar are also used in this function
global char price choice N
%L matrix in a Choleski factor
L = [0 0 0 0 ; 0 1 0 0 ; 0 L32 L33 0 ; 0 L42 L43 L44];
%Var-Cov matrix calculated from the L matrix
omega = L*L.';
%Var-Cov matrix of the differences between the errors
M = [1 0 -1 0 ;
0 1 -1 0 ;
0 0 -1 1];
omega_tilda = M*omega*M.';
%Random draws from MVN(0,omega)
error = mvnrnd([0 0 0 0],omega,N);
%Calculate 4 utilities for each individual i; the resulting matrix is Nx4
U=zeros(N,4);
for i=1:N
U(i,1)=error(i,1);
for j=1:3
U(i,j+1) = char(i,j)*beta - price(i,j)*alpha + error(i,j);
end
end
%Checking if the simulation matches with the actual result
%individual i will get a 1 if there is a match, 0 if no match
C=zeros(N,1);
TF=zeros(N,1);
for i=1:N
if U(i,1)>U(i,2) && U(i,1)>U(i,3) && U(i,1)>U(i,4)
C(i,1)=0;
elseif U(i,2)>U(i,1) && U(i,2)>U(i,3) && U(i,1)>U(i,4)
C(i,1)=1;
elseif U(i,3)>U(i,1) && U(i,3)>U(i,2) && U(i,3)>U(i,4)
C(i,1)=2;
elseif U(i,4)>U(i,1) && U(i,4)>U(i,2) && U(i,4)>U(i,3)
C(i,1)=3;
end
if C(i,1)==choice(i,1)
TF(i,1)=1;
else
TF(i,1)=0;
end
end
%Choice Probability
prob=-log(sum(TF)/N);
end
Then I have the following main file:
%Defining global variables
global char price choice N
%Loading the data into MATLAB
pset1=importdata('consumptiondata.txt');
%Number of individuals in one market
N=50;
%Submatrix for the characteristic data
char=pset1(:,1:3);
%Submatrix for the price data
price=pset1(:,4:6);
%Submatrix for the alternative that individual i chooses
choice=pset1(:,7);
prob(-7,-12,13,2,3,2,6)
%Search for the optimal parameters
options = optimset('Display','iter');
[x,fval] = fminsearch(@prob,[-7 -12 1 1 1 1 1])
I get the following errors:
Error using prob (line 7)
Not enough input arguments.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Error in PSet1_IO (line 27)
[x,fval] = fminsearch(@prob,[-7 -12 1 1 1 1 1])
The function works fine when I test it with prob(-7,-12,13,2,3,2,6). Any help would be great, thanks!
2 个评论
Matt J
2013-10-12
Please apply the
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175742/image.png)
formatting button to your code to make it distinguishable from your text.
回答(1 个)
Matt J
2013-10-12
编辑:Matt J
2013-10-12
The input syntax of prob should not be prob(-7,-12,13,2,3,2,6). You need to rewrite it to accept a vector of unknowns prob([-7,-12,13,2,3,2,6]).
Also, there are better ways to pass constant data to an objective function than using global variables, see http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html
Finally, your loglikelihood function should not be the place that random data gets generated. All of the following stuff should be done, I'm guessing, outside of prob with "error" then passed to prob() as constant data along with char, price, and choice
%L matrix in a Choleski factor
L = [0 0 0 0 ; 0 1 0 0 ; 0 L32 L33 0 ; 0 L42 L43 L44];
%Var-Cov matrix calculated from the L matrix
omega = L*L.';
%Var-Cov matrix of the differences between the errors
M = [1 0 -1 0 ;
0 1 -1 0 ;
0 0 -1 1];
omega_tilda = M*omega*M.';
%Random draws from MVN(0,omega)
error = mvnrnd([0 0 0 0],omega,N);
9 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!