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.
Best Answer