完成/改进附件中最速下降法的MATLAB代码,可与其他算法结合求解30维Rosenbrock函数的最优解

function [F0,x0,FE] = LineSearchDemo()

nVar = 30; % Number of variables

x0 = 2*ones(1,nVar); % DO NOT CHANGE. Initial position.

% p0 = [1;1]; % Use your own search direction instead

tol = 1e-6; % Tolerance

FEmax = 2e6; % Maximum number of function evalution

alpha = 1; % Initial step length

F0 = Rosenbrock(x0); % Intial functino value

FE = 1; % Number of function evaluations

while F0 > tol && FE < FEmax % Step if reach tolerance or exceeds maximum function eval.

g = gradient(x0,F0); % The gradient on current x

FE = FE + nVar; % Finding gradient costs nVar FE

p = -g; % Search direction

xnew = x0 + alpha*p; % Next step

Fnew = Rosenbrock(xnew); % Function value of the next step

FE = FE + 1; % Update FE

if Fnew < F0 % If better solution found

    

    

    

else

    

end

end

function y = Rosenbrock(x) % The Rosenbrock function

nVar = size(x,2);

y = sum(100.*(x(:,1:nVar-1).^2-x(:,2:nVar)).^2+(x(:,1:nVar-1)-1).^2,2);

function g = gradient(x,F0) % Find the gradient of the current point

nVar = size(x,2);

d = sqrt(eps); % A small disturbance

X = repmat(x,nVar,1) + eye(nVar)*d; % Add disturbance to each element of x

g = (Rosenbrock(X)-F0)'/d; % You may use this line to debug your code for

% efficiency, but MUST NOT when you are using the Profiler

%--------------------------------------------------------------------------

% This part is only used when you need to submit the report. For debuging,

% comment this part.

% g = zeros(1,nVar);

% for ii = 1:nVar

% g(ii) = (Rosenbrock(X(ii,:))-F0)/d;

% end

%--------------------------------------------------------------------------大体框架是这样的,哪位大神会呀,只要改几行代码即可。要求是: 完成/改进附件中最速下降法的MATLAB代码,可与其他算法结合求解30维Rosenbrock函数的最优解;

2) 允许使用线搜索以外的算法,但必须手动实现。直接调用系统内置功能将被视为不合格

3) 无论进行何种更改,每次函数计算后都必须更新FE(函数求值,目标函数的调用次数);