Clear Filters
Clear Filters

Errors in objective function

4 views (last 30 days)
Honey Adams
Honey Adams on 12 Jul 2018
Commented: Honey Adams on 17 Jul 2018
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
Honey Adams
Honey Adams on 13 Jul 2018
I found the error in the original equation.its is quite complex
P*m*((1-e^(-(p+(q_m q_0)/(q_(0+) (q_m-q_0)e^(-vw) ))*t)))/(P+ ((q_m q_0)/(q_(0+) (q_m-q_0)e^(-vw) ))*e^(-(P+ ((q_m q_0)/(q_(0+) (q_m-q_0 ) e^(-vw) ))*t) )
I then tried to define the function using the general equation. When I attempted to test my function I was told Error using exp Not enough input arguments.
Error in Bass (line 5) F=[-data + (x(1)*x(2)*(1- exp*(-x(1)- ((x(3)*x(4))/(x(4)+(x(3)-x(4))*exp(-x(5)*v))))*t)/...
Honey Adams
Honey Adams on 13 Jul 2018
t was omitted in the first case and it is a vector from 1 to 30.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 13 Jul 2018
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.
  32 Comments
Stephan
Stephan on 17 Jul 2018
It was a kind of fun to help. I will now unfollow this question, since you seem to be happy. If you have further problems, come back with a new question.
Best regards
Stephan
Honey Adams
Honey Adams on 17 Jul 2018
Yes, thank you.

Sign in to comment.

More Answers (1)

Stephan
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
Honey Adams on 13 Jul 2018
Edited: Honey Adams on 13 Jul 2018
Great. I understand now how the fmincon works even tough from Torsten, I have come to realize that using the fmincon isn't the right optimizer to use for my problem. Thank you for bearing with me and using part of ur precious time to go through the code and point out all the errors. I am grateful. I accepted Tortsen answers since I will be using the lsqcurvefit. But I gained a lot of insight from your short tutorial.
Stephan
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!