Problem using solve() and root muliplicity.
1 次查看(过去 30 天)
显示 更早的评论
Why is this not working with the Rosenbrock ? It works with the test function. But how can I get it to work with Rosenb?
The error refers to the solutions of h with are not unique for this function. How can I go about this?
Error: "Unable to perform assignment because the left and right sides have a different number of elements."
clc
clear
format long
syms X Y;
%f = X - Y + 2*X^2 + 2*X*Y + Y^2; % Test function
f= 100*(Y-X^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
0 个评论
回答(1 个)
Walter Roberson
2019-11-29
f= 100*(Y-X^2)^2+ (1-X)^2; %ROSENBROCK FUNCTION !!!!!!!!!!!!!!!!!!!!!!!
(Y-X^2) involves X to degree 2, and then you square that, (Y-X^2)^2, so your f is going to have X to degree 4. You substitute something+h in for X in f, so you are going to end up with h^4 . You differentiate with respect to h, getting a cubic in h. solve() of that for h is going to have three solutions.
Your test function ends up having only X^2 so the subs() gives you something in h^2, differentiate and you get something in h^1 which has only a single solution.
What can you do? Well, you can do the double-differentiate test to determine which values are local minima or maxima. However eventually at some input function you would run into there being multiple local minima or maxima, and then you would be stuck.
You should be reviewing the entire algorithm to figure out what the algorithm says to do if there are multiple solutions for h.
6 个评论
Walter Roberson
2019-11-29
Note that you are running the solve() with respect to [x(i)+S(1)*h,y(i)+h*S(2)]) . With the diff() you are doing at that point and solve() for the 0, you are using calculus to find the critical points with respect to [x(i)+S(1)*h,y(i)+h*S(2)]) . That is not necessarily going to be the same as minimization with respect to the location [x(i), y(i)]
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!