你那有最速下降法的MATLAB程序吗?给我一份,谢谢
1个回答
展开全部
function y = minSpeed2(functions,var,x0,a,stepNum)
%functions is a function of var,for example
%x = [x1,x2,x3,x4] y = x*x.';
%var is a variable array like [x1,x2,x3,...]
%you shouldn't like [a,b,c,d,...] or other kinds.
%x0:起始迭代值 a:迭代步长
VF = jacobian(functions,var);
x = zeros([1001,length(x0)]);
x(1,:) = x0;
for k=1:stepNum
x(k+1,:) = x(k,:) - a*double( vpa( subs(VF,var(1:end),x(k,:)) ) );
end;
if length(x0)==2
plot(x(1:k,1),x(1:k,2),'o-');grid on;
end;
if length(x0)==3
plot3(x(1:k,1),x(1:k,2),x(1:k,3),'o-');grid on;
end;
disp(x(stepNum,:));
y = x(stepNum,:);
调用格式:
clc,clear all;
syms x1 x2;
fg = (x1-5).^2+(x2+4).^2;
minSpeed2(fg,[x1 x2],[1 -2],0.01,300)
%functions is a function of var,for example
%x = [x1,x2,x3,x4] y = x*x.';
%var is a variable array like [x1,x2,x3,...]
%you shouldn't like [a,b,c,d,...] or other kinds.
%x0:起始迭代值 a:迭代步长
VF = jacobian(functions,var);
x = zeros([1001,length(x0)]);
x(1,:) = x0;
for k=1:stepNum
x(k+1,:) = x(k,:) - a*double( vpa( subs(VF,var(1:end),x(k,:)) ) );
end;
if length(x0)==2
plot(x(1:k,1),x(1:k,2),'o-');grid on;
end;
if length(x0)==3
plot3(x(1:k,1),x(1:k,2),x(1:k,3),'o-');grid on;
end;
disp(x(stepNum,:));
y = x(stepNum,:);
调用格式:
clc,clear all;
syms x1 x2;
fg = (x1-5).^2+(x2+4).^2;
minSpeed2(fg,[x1 x2],[1 -2],0.01,300)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询