1

I want to minimize the following minimization problem using fmincon in MATLAB

enter image description here

The code below (Strategy 1) that I have written solves the problem when I replace the logarithmic (nonlinear) constraint by the linear constraint sum(x) = 5. So how can I change it to introduce the logarithmic constraint? I am blocked here.

MATLAB CODE (Strategy 1):

x0 = zeros(3,1);
Sigma = rand(3,3); %take any matrix

Obj_func = @(x) sqrt(x' * Sigma * x);

options = optimoptions('fmincon','Algorithm','interior-point', 'MaxFunctionEvaluations', 1e100);

[x, FVAL, EXITFLAG, OUTPUT] = fmincon(Obj_func, x0, [], [], ones(1,3), 5, zeros(3,1), inf(3,1), [], options);

Another way to do it (see This reference) can be as below but I am sure if I am correct. (I need help).

MATLAB CODE (Strategy 2):

fun = @(x)sqrt(x' * Sigma * x);

nonlcon = @my_function;
x0 = [0 0 0]';
A = []; % No other constraints
b = [];
Aeq = [];
beq = [];
lb = zeros(3,1);
ub = inf(3,1);
w = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, nonlcon)


function [const,ceq] = my_function(x)
const(1) = 5 - sum(log(x));
ceq = [];
end

Any help will be very appreciated!

Christina
  • 903
  • 16
  • 39
  • If you're looking for a starting point, that starting point is a [web search](https://www.google.com/search?q=how+to+use+fmincon), not [asking on Stack Overflow](//meta.stackoverflow.com/a/261593/843953) – Pranav Hosangadi Feb 25 '21 at 23:14
  • @PranavHosangadi I edited my question. Hope you can help since at your profile you have written that you are very strong in matlab:) – Christina Feb 25 '21 at 23:47
  • What is the problem with your second strategy? Did you get an error? – Pranav Hosangadi Feb 26 '21 at 02:44
  • I added `rng(123);` before the definition of `Sigma` and ran Strategy 2 above and got `w = [5.1832; 5.2581; 5.4455]` (using R2019a). I'm also curious to know what the problem seems to be... – Vicky Feb 26 '21 at 04:15
  • @PranavHosangadi I don't get any error with the Strategy 2, but I just want to know if what I have written in Strategy 2 seems correct.. – Christina Feb 26 '21 at 08:01
  • @Vicky Thanks for your comment. I don't get any error but are you sure that what you obtained is really the right answer that should be? I am just asking to know if my code is written correctly (logically correct) or there is some mistakes... Thanks. – Christina Feb 26 '21 at 10:09
  • 1
    I guess your implementation looks fine. You could adapt your code to another nonlinear optimization problem whose solution you already know and check if the result is as expected, if you wanted to be sure. BTW: since SO tends to be somewhat problem-driven, in situations like this, where you have a program that seems to be working correctly, you may want to see what the people at [Code Review](https://codereview.stackexchange.com/help/on-topic) have to say about it. – Vicky Feb 26 '21 at 15:34
  • 1
    @Vicky Thank you for your reply. Ah great I didn't know about Code Review! Thanks – Christina Feb 26 '21 at 18:54

0 Answers0