在Matlab中使用约束最大化3x + y [英] Maximize 3x+y with constraints in Matlab
问题描述
我需要在matlab中将方程3x + y最大化,并具有以下约束:
I need to maximize the equation 3x+y in matlab with the following constraints:
2x + y <= 6, x + 3y< = 9,并且x,y> = 0
2x+y<=6, x+3y<=9, and x,y>=0
我很难弄清楚如何以将约束条件与原始方程式联系起来的方式来放置约束条件.我是Matlab的新手,在解决这个问题时遇到了麻烦.
I am having a really hard time figuring out how to put in the constraints in a way that I can relate them back to the original equation. I am new to matlab and am having trouble figuring this out.
提前谢谢!
推荐答案
如@Franck所述,通常可以使用fmincon
解决优化问题.但是,由于您的问题只是线性规划问题,所以解决方案要简单得多(并保证是最佳的):
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)
会给:
X =
3.0000
0.0000
FVAL =
-9.0000
因此,在(3,0)点找到了最优值,结果值为9.
Hence the optimum is found at point (3,0) and the resulting value is 9.
尝试help linprog
阅读有关此非常有用的功能的更多信息.
Try help linprog
to read more about this very usefull function.
这篇关于在Matlab中使用约束最大化3x + y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!