Minimization problem (FMINCON): I am unable to minimize my function: "Not enough input errors" and others

1 次查看(过去 30 天)
I have written a function mydifference(a,s) where a and s are two scalars. The function uses also two vectors of data which I have preferred to import on my script and not putting the as inputs in my function. The code is roughly:
function [ sum_err ]=mydifference(a,s)
err=[];
myimport;
sum_err=sum((1/45)*((p'-(exp((((s^2)/2*a^2)*t)- blablabla ... ;
where in myimport I import vectors of data p and t. The function works for a reasonable value of parameters a and s
When I try to minimize this function changing parameters a and s using the fmincon I do not get any useful result. My code for minimize it is:
myimport;
x=[];
X0=[0.085,0.0015]; %starting values
lb=[0.0001,0.00001]; % "a" not lower than 0.0001 and "s" 0.00001
ub=[10,5];% "a" not higher than ..ecc ecc
fun=@(a,s)mydifference(a,s);
[x,fval] = fmincon(fun,X0,[],[],[],[],lb,ub)
The errors I get are:
>> min_mydifference Not enough input arguments.
Error in min_mydifference>@(a,s)mydifference(a,s)
Error in fmincon (line 535) initVals.f = feval(funfcn{3},X,varargin{:});
Error in min_mydifference (line 7) [x,fval] = fmincon(fun,X0,[],[],[],[],lb,ub)
Caused by: Failure in initial objective function evaluation. FMINCON cannot continue.
I have computed the correct set of values by using another script which plots on a surfice the values of mydifference and adeguately choosing a and s I can find the minimum of mydifference. This is a very empirical way of doing it and I need to correctly use fmicon to get the same results.
the vector X0 uses good starting values for mydifference
What I think is that fmicon puts X0 directly in mydifference as a single input and this leads to some problems. What I mean is that fmincon performe mydifference(X0) instead of mydifference(X0(1),X0(2)). Same for my vector lb and ub.

采纳的回答

dbmn
dbmn 2017-4-3
I think the solution might be in the way you created your function fun.
if you check the documentation for fmincon it states that it accepts only one input
"fun is a function that accepts a vector or array x and returns a real scalar f, the objective function evaluated at x."
so you might be better of, if your function fun only accepts one array x with x(1) = a and x(2) = s
I would suggest you change your function to
mydifference(x)
a = x(1); s = x(2);
and change
fun=@(x)mydifference(x);
and
x0 = [a0, s0];
  1 个评论
enzo fiasco
enzo fiasco 2017-4-3
Thanks! following your suggestion my minimization code now run smoothly! I was thinking the same but I was not able to put my thoughts in practice; thanks again for your answer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by