Error using fmincon function
Show older comments
*Hi everyone,
This is my first time using fmincon so I am sorry if the error is "stupid". Here is my code:*
r1= sym('r1'); r2= sym('r2'); r3= sym('r3'); r4= sym('r4'); r5= sym('r5');
syms A; syms B; syms C; syms alpha; syms beta; syms gamma;
syms temp1; syms x;
A = (r1^2)-(r2^2)-(r3^2)-(r4^2)+(2*r2*r4*sin(x/r5));
B = (2*r2*r3*sin(x/r5))-(2*r2*r4);
C = -2*r2*r3*cos(x/r5);
alpha= (1+((B/C)^2));
beta= (-2*A*B)/(C^2);
gamma= ((A/C)^2)-1;
temp1 = (-beta + (((beta^2)-(4*alpha*gamma))^(1/2)))/(2*alpha);
fun = matlabFunction(temp1);
rmax = 1;
x0 = [0.5 0.5 0.5 0.5 0.5 0.5]';
Matrix = [1 0 0 0 0 0;-1 0 0 0 0 0;0 1 0 0 0 0;0 -1 0 0 0 0;0 0 1 0 0 0;0 0 -1 0 0 0;0 0 0 1 0 0;0 0 0 -1 0 0;0 0 0 0 1 0;0 0 0 0 -1 0;0 0 0 0 0 1;0 0 0 0 0 -1];
b = [rmax 0 0 0 0 0 0 0 0 0 0 0]';
var = fmincon(fun,x0,Matrix,b);
Anyway, I am getting this error:
Warning: The default trust-region-reflective algorithm does not solve problems with the constraints you have specified. FMINCON will use the active-set algorithm instead. For information on applicable algorithms, see Choosing the Algorithm in the documentation. > In fmincon at 501 In Linkage_Optimization at 26 Warning: Your current settings will run a different algorithm (interior-point) in a future release.
> In fmincon at 506
In Linkage_Optimization at 26
Error using
symengine>makeFhandle/@(r1,r2,r3,r4,r5,x)(sqrt(-(1.0./r2.^2.*1.0./r3.^2.*1.0./cos(x./r5).^2.*(r2.*r4.*2.0-r2.*r3.*sin(x./r5).*2.0).^2+4.0).*(1.0./r2.^2.*1.0./r3.^2.*1.0./cos(x./r5).^2.*(-r1.^2+r2.^2+r3.^2+r4.^2-r2.*r4.*sin(x./r5).*2.0).^2.*(1.0./4.0)-1.0)+1.0./r2.^4.*1.0./r3.^4.*1.0./cos(x./r5).^4.*(r2.*r4.*2.0-r2.*r3.*sin(x./r5).*2.0).^2.*(r1.^2.*-2.0+r2.^2.*2.0+r3.^2.*2.0+r4.^2.*2.0-r2.*r4.*sin(x./r5).*4.0).^2.*(1.0./1.6e1))+1.0./r2.^2.*1.0./r3.^2.*1.0./cos(x./r5).^2.*(r2.*r4.*2.0-r2.*r3.*sin(x./r5).*2.0).*(r1.^2.*-2.0+r2.^2.*2.0+r3.^2.*2.0+r4.^2.*2.0-r2.*r4.*sin(x./r5).*4.0).*(1.0./4.0))./(1.0./r2.^2.*1.0./r3.^2.*1.0./cos(x./r5).^2.*(r2.*r4.*2.0-r2.*r3.*sin(x./r5).*2.0).^2.*(1.0./2.0)+2.0) Not enough input arguments.
Error in fmincon (line 640) initVals.f = feval(funfcn{3},X,varargin{:});
Error in Linkage_Optimization (line 26) var = fmincon(fun,x0,Matrix,b);
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
2 Comments
Walter Roberson
on 12 Dec 2013
When you say
x0 = [0.5 0.5 0.5 0.5 0.5 0.5]'
which element of x0 corresponds to x, which to r1, r2, r3, r4, r5 ?
Answers (1)
Walter Roberson
on 12 Dec 2013
fun = matlabFunction(temp1, 'vars', [r1, r2, r3, r4, r5, x]);
var = fmincon(@(X) fun(X(1),X(2),X(3),X(4),X(5),X(6)),x0,Matrix,b);
Or alternately, leave your fmincon function call alone and use
fun = matlabFunction(temp1, 'vars', {[r1 r2 r3 r4 r5 x]});
The syntax for this last version is not self-explanatory; look carefully at the Examples area of http://www.mathworks.com/help/symbolic/matlabfunction.html
19 Comments
Jonah
on 12 Dec 2013
Walter Roberson
on 12 Dec 2013
Edited: Walter Roberson
on 12 Dec 2013
fun = matlabFunction(temp1, 'vars', [r1, r2, r3, r4, r5, x]);
options.MaxunEvals = 3000; %more more more
options.Algorithm = 'active-set';
var = fmincon(@(X) fun(X(1),X(2),X(3),X(4),X(5),X(6)),x0,Matrix,b, options);
Jonah
on 12 Dec 2013
Walter Roberson
on 12 Dec 2013
var = fmincon(@(X) fun(X(1),X(2),X(3),X(4),X(5),X(6)), x0, Matrix, b, [], [], options);
Jonah
on 12 Dec 2013
Walter Roberson
on 12 Dec 2013
Style note that would not affect anything: instead of the way you are using sym and syms now, you could use
syms r1 r2 r3 r4 r5 x
You do not need to mark A or B or alpha or beta or gamma or temp1 as syms: it will do that automatically because you are assigning symbolic expressions to those variables.
You are not currently using temp2 or y1 or y2
Walter Roberson
on 12 Dec 2013
Your A, b bounds appear to be simple ones. I suggest that instead of using the A, b form, that you use lb and ub
Matrix = [];
b = [];
Aeq = [];
Beq = [];
lb = [0; 0; 0; 0; 0; 0];
ub = [rmax; 0; 0; 0; 0; 0];
Notice that you have constrained r2 to r5 and x to all be exactly 0 in your Matrix, b setup. If -1*x <= 0 and +1*x <= 0 then the only resolution is x = 0 exactly. Which would certainly go a ways towards explaining your difficulties. If variables are going to have inequalities strictly related to constants and not other variables, the lb / ub form is easier to read.
Jonah
on 12 Dec 2013
Walter Roberson
on 12 Dec 2013
One thing I would point out is that x ad r5 are only used in the pair x/r5 with neither of them used individually. You can therefor drop down a dimension in your search by using a single variable for the ratio.
With the constraints you have given just above for r5 and x, the ratio must be between (0.1/2) = 0.05 and (5/0.1) = 50.
However: You indicate "the input is x"; does that mean that in practice you will be given a particular x value, and need to optimize for that? If so then you really do need to drop down a dimension:
x = input('value for x?');
syms r1 r2 r3 r4 r5
A = (r1^2)-(r2^2)-(r3^2)-(r4^2)+(2*r2*r4*sin(x/r5));
B = (2*r2*r3*sin(x/r5))-(2*r2*r4);
C = -2*r2*r3*cos(x/r5);
alpha= (1+((B/C)^2));
beta= (-2*A*B)/(C^2);
gamma= ((A/C)^2)-1;
temp1 = (-beta + (((beta^2)-(4*alpha*gamma))^(1/2)))/(2*alpha);
fun = matlabFunction(temp1, 'vars', [r1, r2, r3, r4, r5]);
x0 = [1 2.5 2.5 2 0.5]';
options.MaxFunEvals = 100;
Matrix = [];
b = [];
Aeq = [];
beq = [];
lb = [0.1; 0.1; 0.1; 0.1; 0.1];
ub = [2; 4; 4; 4; 2];
var = fmincon(@(R) fun(R(1),R(2),R(3),R(4),R(5)), x0, Matrix, b, Aeq, beq, lb, ub, nonlcon, options);
Jonah
on 13 Dec 2013
Walter Roberson
on 13 Dec 2013
Have you considered finding an approximate solution first? For example,
fragments = 16;
samples2 = linspace(0.1, 2, fragments+1);
samples4 = linspace(0.1, 4, fragments+1);
[R1, R2, R3, R4, R5] = ndgrid(samples2, samples4, samples4, samples4, samples2);
funvals = fun(R1, R2, R3, R4, R5); %all of them at once
[minval, minidx] = min(funvals(:));
r1val = R1(minidx);
r2val = R2(minidx);
r3val = R3(minidx);
r4val = R4(minidx);
r5val = R5(minidx);
fprintf('minimum was %g at (%g, %g, %g, %g, %g)\n', minval, r1val, r2val, r3val, r4val, r5val);
After that you could take projections along various axes or do contour plots of iso slices, and so on, to figure out where the smaller values are clustered, and perhaps there are multiple such locations. Once the region of the lower solutions are found, you could zoom into that area by changing the ranges used in the ndgrid(), and then iterate again to get closer; and then take the ranges that you can extract from that and plop them into fmincon() to head for the minimum.
Jonah
on 13 Dec 2013
Walter Roberson
on 13 Dec 2013
Remember, fmincon() always minimizes. If you have a function f() that you want to maximize, then minimize -1*f() .
Jonah
on 13 Dec 2013
Walter Roberson
on 13 Dec 2013
The limit is indefinitely high.
r1 = smallest possible
r5 = 1
x = Pi/2
r3 = nearly largest possible
r4 same as r2
r2 = just slightly larger than r3, but not equal to r3
The closer r2 gets to r3, the higher the maximum
x/r5 = Pi/2 + n*2*Pi, n an integer
is a value at which singularities start to form. At that angle, the limit of f goes to (r1^2-(r2-r4)^2-r3^2) / (2*r2*(r3-r4))
you can drive this indefinitely high in more than one way; one of the ways involves maximizing r1 and ensuring that (r3-r4) is one side of 0, and the other involves minimizing r1 and ensuring that (r3-r4) is the other side of 0. And you can see from the numerator that you subtract off least if r4 approaches r2.
A different singularity forms along x/r5 = 3*Pi/2 + n*2*Pi, n integer. It can give you local maxima that do not depend upon the r3 vs r2 singularity. The form is very similar to the other:
-(r1^2-(r2+r4)^2-r3^2)/(2*r2*(r3+r4))
On the other hand, this can be driven indefinitely high by making r2 and r4 small, r1 small, and r3 high.
Srikanth Gummadavalli
on 13 Apr 2017
Hi
in the solution that you had provided
fun = matlabFunction(temp1, 'vars', [r1, r2, r3, r4, r5, x]);
var = fmincon(@(X) fun(X(1),X(2),X(3),X(4),X(5),X(6)),x0,Matrix,b);
what if we have r1 r2... up to r40. how to solve the above issue
Thankyou
Walter Roberson
on 13 Apr 2017
r = sym('r', [1 40]);
fun = matlabFunction(temp1, 'vars', {r.'});
var = fmincon(fun, x0, Matrix, b)
Categories
Find more on Common Operations 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!