Errors in objective function
Show older comments
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 :)
6 Comments
Torsten
on 13 Jul 2018
You want to force "fmincon" find parameters x(1),...,x(5) such that one single number (namely your expression
(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)))
)
is simultaneously equal to all your data points.
That doesn't make sense.
Best wishes
Torsten.
Honey Adams
on 13 Jul 2018
Edited: Honey Adams
on 13 Jul 2018
Torsten
on 13 Jul 2018
For simplicity, let's assume that
data=[3 4 5 6]
and your expression is
x(1)+3*x(2).
Then your vector of equations is
3 = x(1)+3*x(2)
4 = x(1)+3*x(2)
5 = x(1)+3*x(2)
6 = x(1)+3*x(2)
Thus one single number (x(1)+3*x(2)) shall approximate 3,4,5 and 6 simultaneously.
This doesn't make sense.
Best wishes
Torsten.
Honey Adams
on 13 Jul 2018
Honey Adams
on 13 Jul 2018
Honey Adams
on 13 Jul 2018
Accepted Answer
More Answers (1)
Stephan
on 13 Jul 2018
Hi,
in addition to the issues that Torsten noted in his comments, note the following errors:
- you have X(i) instead of x(i) in your objective function, which will cause problems. I found 2 locations in your Bass function where this error appears.
- your functions should end with an end - this will also cause errors, when executing the code
- functions in a script have to be at the end of the script - this will also cause Errors, when executing the code
- the problem regarding the input arguments is described here. The way you do it, will not pass the required arguments to the function calls of nonlinc and Bass, when fmincon calls the functions to evaluate the actual values of vector x in every iteration. In the result you will get the error not enough input arguments. To resolve this you need a kind of outer function, like described in the link above.
Here is a corrected version that considered all the issues that i could find:
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];
v=100;
x = outer_function(v, data);
disp(x)
function j = outer_function(v, data)
fun=@Bass;
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);
function res= Bass(x)
%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);
end
function [c, ceq]= nonlinc(x)
%constraint
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)));
end
end
If you run this script, it will work, but converges to an infeasible point, which may be because of what Torsten noted in his commentary:
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the default value of the step size tolerance but constraints are not
satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
28.4354 0.0000 0.5313 0.5313 0.4404
So what you have to do is check what Torsten told you.
Best regards
Stephan
3 Comments
Honey Adams
on 13 Jul 2018
Honey Adams
on 13 Jul 2018
Edited: Honey Adams
on 13 Jul 2018
Stephan
on 13 Jul 2018
With every question here i also learn - thats why im here. The way Torsten solved the problem is interesting for me too.
Categories
Find more on Nonlinear Least Squares (Curve Fitting) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!