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)
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/25 10:33:55
![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)](/uploads/image/z/8654731-43-1.jpg?t=matlab%E6%97%A0%E6%B3%95%E8%BF%90%E8%A1%8C%E6%A2%AF%E5%BA%A6%E7%AE%97%E6%B3%95%E7%BC%96%E5%86%99M%E6%96%87%E4%BB%B6fun9.m%E5%AE%9A%E4%B9%89%E7%9B%AE%E6%A0%87%E5%87%BD%E6%95%B0%E5%8F%8A%E6%A2%AF%E5%BA%A6%E5%87%BD%E6%95%B0%EF%BC%9Afunction+%5Bf%2Cdf%5D%3Dfun9%28x%29%3B+f%3Dexp%28x%281%29%29%2A%284%2Ax%281%29%5E2%2B2%2Ax%282%29%5E2%2B4%2Ax%281%29%2Ax%282%29%2B2%2Ax%282%29%2B1%29%3B+df%3D%5Bexp%28x%281%29%29%2A%284%2Ax%281%29%5E2%2B2%2Ax%282%29%5E2%2B4%2Ax%281%29%2Ax%282%29%2B8%2Ax%281%29%2B6%2Ax%282%29%2B1%29%3Bexp%28x%281%29%29%2A%284%2Ax%282%29%2B4%2Ax%281%29)
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)
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)
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)
%运行成功,但是结果不稳定.可能主要还是函数本身的影响.
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=[];