已知目标函数和约束条件,用MATLAB怎么求最大值

目标函数 1.08∧x(1)×2.06∧x(2)×1.17∧x(3)
约束条件 3×x(1)+6×x(2)+12×x(3)≤20
x(1),x(2),x(3)≥0
求目标函数最大值

已知目标函数和约束条件,求最大值,属于条件极值问题,可以用拉格朗日数乘法来做,下面给出拉格朗日数乘法的matlab代码:

clc;clear;
syms x y z t%定义自变量x,y,z,拉格朗日乘数t
f(x,y,z)=x+2*y+3*z;%设需要求最大值的表达式x+2*y+3*z
g=x^2+y^2+z^2-4;%设约束条件x^2+y^2+z^2-4=0
L=f-t*g;
sln=solve(diff(L,x)==0,diff(L,y)==0,diff(L,z)==0,g==0);%解拉格朗日数乘法的方程组
eval(f(sln.x,sln.y,sln.z))%把解带回f,求出条件极值

运行结果如下:
ans =

7.4833
-7.4833
即得到x+2*y+3*z在x^2+y^2+z^2-4=0条件下的最大值7.4833,最小值-7.4833。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-21
>> f = @(x) -(1.08.^x(1).*2.06.^x(2).*1.17.^x(3)); % 加了负号,求出的最小值的相反数即为要求的最大值
>> a = [-1 0 0;0 -1 0;0 0 -1;3 6 12]; 约束条件
>> b = [0 0 0 20];
>> x0 = [1 1 1]; % 迭代初始值
>> [x,val] = fmincon(f,x0,a,b,[],[],[],[],[],optimset('Algorithm','interior-point'))

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the default value of the function tolerance,
and constraints were satisfied to within the default value of the constraint tolerance.

<stopping criteria details>


x =

    0.0000    3.3333    0.0000


val =

  -11.1231

即x1=0,x2=3.333,x3=0时,原目标函数取得最大值11.1231

本回答被网友采纳