Subscripted assignment dimension mismatch. Error in fminsearch (line 191) fv(:,1) = funfcn(x,varargin{:});

1 view (last 30 days)
This is my code:
Starting=[10.0;1.0];
[~, Paretofval]=fminsearch(@pareto, Starting);
BIC(1,1) = -2*(-Paretofval) + 2 * log(n);
with function:
function fn = pareto(init)
global x;
global n;
fn = - (n*log(init(1)) + n*init(1)*log(init(2))-(1+init(1))*sum(log(init(2)+x)));
end
where x = n by 1 vector; n = length(x).
Could someone advise what's wrong with my code pls?

Answers (1)

Walter Roberson
Walter Roberson on 18 Nov 2013
You do not show x or n being initialized. Without initialization they will be empty matrices (because of the global statement), and empty matrices in conjunction with arithmetic calculations is going to end up with empty matrices.
  4 Comments
Matt J
Matt J on 18 Nov 2013
Not only must they be initialized, they must be declared global in every workspace where they are used. If that's not the problem, then the problem is not in the code you've shown. I get no errors when running the code.
GC
GC on 18 Nov 2013
Thanks all. The replies are very helpful. I realised that I did not declare them global in another workspace / function.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!