Errors in objective function
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Torsten
il 13 Lug 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 Commenti
Stephan
il 17 Lug 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
Più risposte (1)
Stephan
il 13 Lug 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 Commenti
Stephan
il 13 Lug 2018
With every question here i also learn - thats why im here. The way Torsten solved the problem is interesting for me too.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!