MATLAB: Errors in objective function

fmincon

data=[0;
0;
0;
37248;
58649;
85974;
132620;
200698;
266406;
325423;
383975;
443234;
498503;
558777;
612881;
659367;
710580;
749772;
794515;
847288;
910312;
963746;
1016473;
1060359;
1097173;
1135293;
1176867;
1212260;
1239208;
1267657]
function res= Bass(x,v,data )
%objective function
F=[ -data+ (x(1)*x(2)*(1 - exp( x(2) + (( X(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ ( 1+ (( x(3)*X(4))/ x(3)-x(4))*exp(-x(5)*v)))]
res=sum(F.^2)
%constriant
function [c ceq]= nonlinc(x,v,data)
c=[];
ceq=[ -data +( x(1)*x(2)*(1 - exp( x(2) + (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ ( 1+ (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))]
fun=@Bass;
v=100;
nl=@nonlinc;
lb=[20,0,0,0,0]
ub=[inf,1,1,1,inf]
x0=[ 0,0,0,0,0]
j=fmincon(fun,x0,[],[],[],[],lb,ub,nl)
my aim was to find the parameters of a system on non_linear equations. I used the Res as my objective function and the constraints were the equations. I didn't use fsolve because I had boundaries.data is a vector . I have corrected this several times but results are always : Error using Bass (line 3) Not enough input arguments.
Error in fmincon (line 564) initVals.f = feval(funfcn{3},X,varargin{:});
Error in Untitled2 (line 41) x= fmincon(fun,x0,A,b,Aeq,beq,lb,ub);
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
>> I would be glad for a little assistance.Thank you 🙂

Best Answer

function main
xdata=1:30;
ydata=[0,0,0,37248,58649,85974,132620,200698,266406,325423,383975,443234,498503,558777,612881,659367,710580,749772,794515,847288,910312,963746,1016473,1060359,1097173,1135293,1176867,1212260,1239208,1267657];
v=100;
fun=@(x,xdata)Bass(x,xdata,v);
lb=[20,0,0,0,0]
ub=[inf,1,1,1,inf]
x0=[0,0,0,0,0]
j=lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
end
%objective function
function F = Bass(x,xdata,v)
F=x(1)*x(2)*(1-exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v)))*xdata))./...
(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))*exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v)))*xdata));
end
You can't specify every single equation as a constraint. Minimizing the objective function does the job.
Best wishes
Torsten.