Maximize 3x+y with constraints in Matlab

后端 未结 2 1793
日久生厌
日久生厌 2021-01-14 16:07

I need to maximize the equation 3x+y in matlab with the following constraints:

2x+y<=6, x+3y<=9, and x,y>=0

I am having a really hard time figuring out

2条回答
  •  温柔的废话
    2021-01-14 16:38

    As @Franck mentioned, you can in general use fmincon to solve optimization problems. However, as your problem is simply a linear programming problem, the solution is much simpler (and guaranteed to be optimal) :

    f = -[3 1]; % Note the minus as we want maximization
    A = [2 1; 1 3];
    b = [6; 9];
    LB = [0 0];
    
    [X, FVAL] = linprog(f,A,b,[],[],LB)
    

    Will give:

    X =
    
        3.0000
        0.0000
    
    
    FVAL =
    
       -9.0000
    

    Hence the optimum is found at point (3,0) and the resulting value is 9.

    Try help linprog to read more about this very usefull function.

提交回复
热议问题