matlab无法运行梯度算法编写M文件fun9.m定义目标函数及梯度函数:function [f,df]=fun9(x)

matlab无法运行梯度算法
编写M文件fun9.m定义目标函数及梯度函数:
function [f,df]=fun9(x);
f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
df=[exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+8*x(1)+6*x(2)+1);exp(x(1))*(4*x(2)
+4*x(1)+2)];
(2)编写M文件fun10.m定义约束条件及约束条件的梯度函数:
function [c,ceq,dc,dceq]=fun10(x);
c=[x(1)*x(2)-x(1)-x(2)+1.5;-x(1)*x(2)-10];
dc=[x(2)-1,-x(2);x(1)-1,-x(1)];
ceq=[];dceq=[];
(3)调用函数fmincon,编写主函数文件example13.m如下:
%采用标准算法
options=optimset('largescale','off');
%采用梯度
options=optimset(options,'GradObj','on','GradConstr','on');
[x,y]=fmincon(@fun9,rand(2,1),[],[],[],[],[],[],@fun10,options)
0lgg 1年前 已收到1个回答 举报

tangtang217 幼苗

共回答了19个问题采纳率:94.7% 举报

%运行成功,但是结果不稳定.可能主要还是函数本身的影响.
function [x,y]=solutionFind
% 调用函数fmincon,编写主函数文件example13.m如下:
%采用标准算法
options=optimset('largescale','off');
%采用梯度
options=optimset(options,'GradObj','on','GradConstr','on');
[x,y]=fmincon(@fun9,rand(2,1),[],[],[],[],[],[],@fun10,options);
function [f,df]=fun9(x)
f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
if nargout > 1 % gradient required
df=[exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+8*x(1)+6*x(2)+1);exp(x(1))*(4*x(2)+4*x(1)+2)];
end
% (2)编写M文件fun10.m定义约束条件及约束条件的梯度函数:
function [c,ceq,dc,dceq]=fun10(x)
c=[x(1)*x(2)-x(1)-x(2)+1.5;-x(1)*x(2)-10];
dc=[x(2)-1,-x(2);x(1)-1,-x(1)];
ceq=[];dceq=[];

1年前

1
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.038 s. - webmaster@yulucn.com