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

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?

1 Comment

Global variables are bad way of passing fixed parameters to functions. They are bad for most things, in general. See here for better options,

Sign in to comment.

Answers (1)

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

Thanks. x and n are initialised in my main code before the first line. Is the problem with me including these two lines again within the function code?
Did you remember to declare the variables as "global" in your main code, before initializing them?
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.
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

Asked:

GC
on 18 Nov 2013

Commented:

GC
on 18 Nov 2013

Community Treasure Hunt

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

Start Hunting!