poster
2019-11-29, 07:37
为什么这不适用于Rosenbrock?它与测试功能一起使用。但是,如何使它与Rosenb一起使用?该错误是指h的解对于该函数不是唯一的。我该怎么办?
错误:“由于左侧和右侧具有不同数量的元素,因此无法执行分配。”
clc clear format long syms XY; %f = X - Y + 2*X^2 + 2*X*Y + Y^2; % Test function f= 100*(YX^2)^2+ (1-X)^2; %ROSENBROCK FUNCTION !!!!!!!!!!!!!!!!!!!!!!! x(1) = -1; y(1) = 4; e = 10^(-8); i = 1; % Gradient set up: df_dx = diff(f, X); df_dy = diff(f, Y); J = [subs(df_dx,[X,Y], [x(1),y(1)]) subs(df_dy, [X,Y], [x(1),y(1)])]; S = -(J); % Search Direction %Minimization Algorithm: while norm(J) > e I = [x(i),y(i)]'; syms h; g = subs(f, [X,Y], [x(i)+S(1)*h,y(i)+h*S(2)]); dg_dh = diff(g,h); h = solve(dg_dh, h); %Problem here!!!!!!!!!!!!!!!!!!!!! x(i+1) = I(1)+h*S(1); y(i+1) = I(2)+h*S(2); i = i+1; J = [subs(df_dx,[X,Y], [x(i),y(i)]) subs(df_dy, [X,Y], [x(i),y(i)])]; % Updated Gradient S = -(J); end
更多&回答... (https://stackoverflow.com/q/59097071)
错误:“由于左侧和右侧具有不同数量的元素,因此无法执行分配。”
clc clear format long syms XY; %f = X - Y + 2*X^2 + 2*X*Y + Y^2; % Test function f= 100*(YX^2)^2+ (1-X)^2; %ROSENBROCK FUNCTION !!!!!!!!!!!!!!!!!!!!!!! x(1) = -1; y(1) = 4; e = 10^(-8); i = 1; % Gradient set up: df_dx = diff(f, X); df_dy = diff(f, Y); J = [subs(df_dx,[X,Y], [x(1),y(1)]) subs(df_dy, [X,Y], [x(1),y(1)])]; S = -(J); % Search Direction %Minimization Algorithm: while norm(J) > e I = [x(i),y(i)]'; syms h; g = subs(f, [X,Y], [x(i)+S(1)*h,y(i)+h*S(2)]); dg_dh = diff(g,h); h = solve(dg_dh, h); %Problem here!!!!!!!!!!!!!!!!!!!!! x(i+1) = I(1)+h*S(1); y(i+1) = I(2)+h*S(2); i = i+1; J = [subs(df_dx,[X,Y], [x(i),y(i)]) subs(df_dy, [X,Y], [x(i),y(i)])]; % Updated Gradient S = -(J); end
更多&回答... (https://stackoverflow.com/q/59097071)