matlab中rosenbrock函数的代码??

来源:百度知道 编辑:UC知道 时间:2024/05/26 19:40:33
matlab上能用的

能用分全给你

1378分都给你
1楼您所说的代码该如何使用呢??具体把哪些打进去?不好意思我一点不会matlab

首先申明,这个程序不是我写的,也是我以前在网上找到的(本科的时候看过,没太看懂,但是测试过了,可以运行的)。

这是完整的rosenbrock函数的代码,绝对可用:
function [f, df, ddf] = rosenbrock(x);

% rosenbrock.m This function returns the function value, partial derivatives
% and Hessian of the (general dimension) rosenbrock function, given by:
%
% f(x) = sum_{i=1:D-1} 100*(x(i+1) - x(i)^2)^2 + (1-x(i))^2
%
% where D is the dimension of x. The true minimum is 0 at x = (1 1 ... 1).
%
% Carl Edward Rasmussen, 2001-07-21.

D = length(x);
f = sum(100*(x(2:D)-x(1:D-1).^2).^2 + (1-x(1:D-1)).^2);

if nargout > 1
df = zeros(D, 1);
df(1:D-1) = - 400*x(1:D-1).*(x(2:D)-x(1:D-1).^2) - 2*(1-x(1:D-1));
df(2:D) = df(2:D) + 200*(x(2:D)-x(1:D-1).^2);
end

if nargout > 2
ddf = zeros(D,D);
ddf(1:D-1,1:D-1) = diag(-400*x(2:D) + 1200*x(1:D-1).^2 + 2);
ddf(2:D,2:D) = ddf(2:D,2:D) + 200*eye(D-1);
ddf = ddf - diag(400*x