Hi i just can't seem to figure out how to plot a graph using fmincon. I am doing an optimization with my function:

fun =@(x) (R_f(Cm,Cwp,Abt,V,x(4),x(1),x(2),x(3)) * k_1(Cm,x(3),x(4),Cstern,x(1),x(2))) + R_w(Cwp,x(4),Abt,hb,Cm,At,x(1),x(2),x(3)) + R_b(Abt,hb,V,x(1),x(2),x(3)) + R_tr(Cwp,At,V,x(1),x(3)) + R_a(Cm,Cwp,Abt,V,hb,x(4),x(1),x(2),x(3));
lb=[2.5,1.5,150,30000];
ub=[5,3.5,250,50000];
A=[];
bt=[];
Aeq=[];
beq=[];
nonlincon = [];
options = optimoptions(@fmincon,'Algorithm','interior-point',...
 'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,'PlotFcn',{@optimplotx,...
    @optimplotfval,@optimplotfirstorderopt});
[x,fval] = fmincon(fun,x0,A,bt,Aeq,beq,lb,ub,nonlincon,options);

can i please get some help here.

6 Comments

Please post enough to reproduce -- e.g., R_f and Cm, Cwp, etc.
Hi Walter Roberson, i am sorry i do not get what you mean. i am new to MatLab. My R_f is a function that i create in another script. i am able to run the optimization with the following result.
Total Resistance: 933233.490 kN
Total Initial Resistance: 1492251.065 kN
Percentage difference: 21.734
L/B Ratio: 5.000 B/T Ratio: 3.500
Length Per Perpendicular: 188.781 m
Submerged Volume: 30000.000 m^3
Numbers of iteration: 17
We need your complete code, along with any data files you use.
I have attached my data file as below. Thank you so much
How do we know if we've fixed it? What result do you see now? What should you see?
Undefined function or variable 'x0'.
Error in Main (line 60)
[x,fval,exitflag] = fmincon(fun,x0,A,bt,Aeq,beq,lb,ub,nonlincon,options); 

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2018

I enclose altered versions of the code that do something

I had to adjust a couple of undefined variables.

After that it turned out that a lot of your permitted positions generate imaginary numbers. I explored that for a while; some of the changes I made to the code were to provide a smooth way to do a more detailed analysis. Your lower bounds turn out to be not suitable. You mostly have to start near your upper bounds and work your way back.

I adjusted the k_1 function to detect imaginary results and substitute a real result. Without that it was not practical to get anywhere because fmincon could not calculate the gradients. With that change, you can use x0 = ub-.1 to get somewhere, relying on the out-of-bounds detection.

I also managed to find a starting point that does not need to invoke the repair of imaginaries; that is what I coded in the above file. It gives a better solution than the ub-0.1 that I mentioned above. It would not astonish me at all if there are better solutions available.

6 Comments

I created a vectorized version of the code and used it to explore the parameter space. The minimum appears to be quite close to 4.9999999998506 1.92225818733023 154.647050628047 30000 .

That is essentially pegged at the upper bound for the first parameter, and pegged at the lower bound for the 4th parameter. The other two parameters are closer to their lower bound than their upper bound, but are not at their lower bounds. The first and last parameters have significant influences on the "cost", with the second and third having less influence.

Hi Sir,
is there a way where when i plot my graph it is my objective function against the iterations. as in order to prove the reliability of this optimization there must be a stopping criteria for the iterations.
can i also ask how did you manage to get the starting bound like this: x0 = [ 4.5, 509/198, 20150/99, 5594737196890505/137438953472];
Thank you so much for your time to help me solve this part.
Plotting can be done through the options structure like you have commented out. I am not at my desk to experiment with the various plots.
That particular starting point was arrived at with some symbolic analysis. If you look at the modified code that I posted you might notice that I check whether a particular variable is numeric and if so go through the if statements you had, but if it is not numeric then the code creates a piecewise(). With that modification it is possible to pass in a mix of symbolic variable names and specific numeric values, generating out a symbolic formula for the combination. For example you can pass in a symbolic variable for x(1) and ub(2:4) for the other x, which would give you a symbolic formula in x1 for the case the other variables are at the top end of their range. Then you can plot the imaginary part of of that symbolic formula over the entire valid range of x(1): the places where the results are nonzero are the places where your function would generate complex values. This kind of analysis permits you to explore how the combination of values interact to give out unusable results. The hope would be that you could find better lb and ub or find linear constraints that would permit the calculation to be entirely valid, after which you could optimize in just that region.
That particular vector of x0 values was found by parameterizing x2, x3, x4 as moving together linearly, each being portion "t" through their valid ranges, t varying over 0 to 1, but with x1 being unconstrained. When the resulting formula is examined it turns out to involve a simple polynomial involving t, raised to an exponent. Solving for 0 gave approximately 54/99 as the boundary for t. Substitute that boundary in to the symbolic formula gives a function in x1 that is more complicated. But you can plot the imaginary part to find the boundary. The boundary turned out to be approximately 4.384582, so I called it 4.5, plugged everything in, and the starting point turned out not to need the protection against complex values. It hits a genuine local minima with that starting point.
Unfortunately with your choice of using fmincon, a local minima is the best you can do from a single starting point.
Hi Sir,
Thank you for the explanation it helps me to understand it better now. Right now i tried to plot the objective function against the number of iteration. I tried couple of methods however i am still unable to do the coding. my current graph only gave me 1 point. May i ask from you to advise me on the direction to be able to plot a converging graph with my objective function against the iteration.
You help and time is appreciated. Thank you
    options = optimset('MaxFunEvals',Inf,'MaxIter',5000,...
        'Algorithm','interior-point','Display','iter', ...
        'PlotFcn', {@optimplotfval});
Thank you so much Mr Walter Roberson.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!