matlab算最大值用matlab计算y=x1+x1*x2+x3-x2*x3+x3*x4+134,其中x1到x4范围分别为(1到5),(2到7)(3-11),(4-10),当y最大时的x1,x2,x3,x4,请写出代码,
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/15 02:05:22
![matlab算最大值用matlab计算y=x1+x1*x2+x3-x2*x3+x3*x4+134,其中x1到x4范围分别为(1到5),(2到7)(3-11),(4-10),当y最大时的x1,x2,x3,x4,请写出代码,](/uploads/image/z/12520185-33-5.jpg?t=matlab%E7%AE%97%E6%9C%80%E5%A4%A7%E5%80%BC%E7%94%A8matlab%E8%AE%A1%E7%AE%97y%3Dx1%2Bx1%2Ax2%2Bx3-x2%2Ax3%2Bx3%2Ax4%2B134%2C%E5%85%B6%E4%B8%ADx1%E5%88%B0x4%E8%8C%83%E5%9B%B4%E5%88%86%E5%88%AB%E4%B8%BA%EF%BC%881%E5%88%B05%EF%BC%89%2C%EF%BC%882%E5%88%B07%EF%BC%89%EF%BC%883-11%EF%BC%89%2C%EF%BC%884-10%EF%BC%89%2C%E5%BD%93y%E6%9C%80%E5%A4%A7%E6%97%B6%E7%9A%84x1%2Cx2%2Cx3%2Cx4%2C%E8%AF%B7%E5%86%99%E5%87%BA%E4%BB%A3%E7%A0%81%2C)
matlab算最大值用matlab计算y=x1+x1*x2+x3-x2*x3+x3*x4+134,其中x1到x4范围分别为(1到5),(2到7)(3-11),(4-10),当y最大时的x1,x2,x3,x4,请写出代码,
matlab算最大值
用matlab计算y=x1+x1*x2+x3-x2*x3+x3*x4+134,其中x1到x4范围分别为(1到5),(2到7)(3-11),(4-10),当y最大时的x1,x2,x3,x4,请写出代码,
matlab算最大值用matlab计算y=x1+x1*x2+x3-x2*x3+x3*x4+134,其中x1到x4范围分别为(1到5),(2到7)(3-11),(4-10),当y最大时的x1,x2,x3,x4,请写出代码,
显然y>0;
用fmincon,求最小值
取y=-(x(1)+x(1)*x(2)+x(3)-x(2)*x(3)+x(3)*x(4)+134);
那么求出来的就是y的最大值
function y=OptimFun(x)
y=-(x(1)+x(1)*x(2)+x(3)-x(2)*x(3)+x(3)*x(4)+134);
%%%%%%%%%%%%%%%%%%%%%%
clc;clear;
[x,fval]=fmincon(@OptimFun,[1 2 3 4],[],[],[],[],[1 2 3 4],[5 7 11 10])
x =
5 2 11 10
fval =
-248
那么
y=-fval=248
%%%%%%%%%%%%%
可以用循环验证一下
clc;clear;
y=zeros(4*5*8*6+1);
y(1)=0;
for j=1:4*5*8*6;
for x1=1:1:5
for x2=2:1:7
for x3=3:1:11
for x4=4:1:10
y(j+1)=x1+x1*x2+x3-x2*x3+x3*x4+134;
if y(j+1)>y(j)
yy=y(j+1);
a=x1;b=x2;c=x3;d=x4;
j=j+1;
end
end
end
end
end
end
yy
yy =
248
显然y>0;
用fmincon,求最小值
取y=-(x(1)+x(1)*x(2)+x(3)-x(2)*x(3)+x(3)*x(4)+134);
那么求出来的就是y的最大值
function y=OptimFun(x)
y=-(x(1)+x(1)*x(2)+x(3)-x(2)*x(3)+x(3)*x(4)+134);
%%%%%%%%%%%...
全部展开
显然y>0;
用fmincon,求最小值
取y=-(x(1)+x(1)*x(2)+x(3)-x(2)*x(3)+x(3)*x(4)+134);
那么求出来的就是y的最大值
function y=OptimFun(x)
y=-(x(1)+x(1)*x(2)+x(3)-x(2)*x(3)+x(3)*x(4)+134);
%%%%%%%%%%%%%%%%%%%%%%
clc;clear;
[x,fval]=fmincon(@OptimFun,[1 2 3 4],[],[],[],[],[1 2 3 4],[5 7 11 10])
x =
5 2 11 10
fval =
-248
那么
y=-fval=248
%%%%%%%%%%%%%
可以用循环验证一下
clc;clear;
y=zeros(4*5*8*6+1);
y(1)=0;
for j=1:4*5*8*6;
for x1=1:1:5
for x2=2:1:7
for x3=3:1:11
for x4=4:1:10
y(j+1)=x1+x1*x2+x3-x2*x3+x3*x4+134;
if y(j+1)>y(j)
yy=y(j+1);
a=x1;b=x2;c=x3;d=x4;
j=j+1;
end
end
end
end
end
end
yy
yy =
248
收起