0

I am currently practicing a simple function optimization in Matlab and hope you could provide with little assistance / explanation on the following error:

%quadramin.m
function z=quadramin(param,data);
z=data.*(param(1).^2 - param(2).^3)+3;

%quadramin_lik.m
function quadlik = quadramin_lik(param,data);
%pseudo/ad-hoc log-likelihood function
quadlik = quadramin(param,data)- 10;

%script.m
data=trnd(5,6,1);
param0=[2,3];
[param_eq,exitflag,output,grad,hessian] = ... 
fminunc(@(param) quadramin_lik(param,data),param0)

Output after executing %script.m: Error using fminunc (line 333) User supplied objective function must return a scalar value.

ps: It looks paradoxical as the user-defined functions quadramin && quadramin_lik do return values.

Thanks

4
  • 2
    Both your functions return a vector of values whereas fminunc requires that the function returns a scalar / single value. The error is pretty clear. The function fminunc is trying to find the best solution that minimizes a cost function, so what you need to supply is a cost function. Therefore, perhaps try summing the results in each function before returning them.... but doing that doesn't guarantee a global minimum because fminunc assumes that your cost function is convex. Commented Jun 2, 2015 at 21:36
  • summing the pseudo log-likehood function to return a scalar/single value works indeed. Thanks for the comments. Finally if i'd like to use @fmincon, am I correct to set lb=[0,2], ub=[0,+inf] if 0<=x(1)<=2 and x(2) belongs to R+? in a nutshell param_eq = fmincon(@(param) quadramin_lik(param,data),param0,[],[],[],[],[0,2],[0, inf])? Cheers Commented Jun 2, 2015 at 21:54
  • I have no idea how your linear program is set up so I can't comment on what you have coded is correct. Commented Jun 2, 2015 at 21:55
  • It should not , a priori, matter too much, but I am very grateful to the main scalar issue raised and solved, as i could not guess the cause of this error. Cheers Commented Jun 2, 2015 at 22:05

1 Answer 1

2

Both your functions return a vector of values whereas fminunc requires that the function returns a scalar / single value. The error is pretty clear. The function fminunc is trying to find the best solution that minimizes a cost function, so what you need to supply is a cost function.

Therefore, perhaps try summing the results in each function before returning them.... but doing that doesn't guarantee a global minimum because fminunc assumes that your cost function is convex. However, judging from your comments as you are computing log likelihoods, then summing is what you should be doing any way!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.