The following was my problem given by my teacher.
Find a minimum of the Rosenbrock’s (banana) function without constraints. Constants a and b should be unique for each person:
a,b = Int[4 * rand()]/2
where,
rand() - random number generator with the uniform distribution in the range <-1,1>.Int() - integer part of the real value.
Generate four starting points:
x = a + 2 * rand(); y = b + 2 * rand();
Create file with values (only) of a, b and all starting points x, y coordinates.
The following was my solution,
function i = Integer(x) i = fix(x); end function r = rand_interval(a, b) r = a + (b-a).*rand(1,1); end function c = gen_const() % Generate a real random number between -1 and 1.
r = 4 * rand_interval(-1.0, 1.0); % Returns the integer part of the real number 4*r/2.
i = Integer(r); c = int32(i)/ int32(2); end function m = gen_points(row_count) col_count = 2; a = gen_const();% integer
b = gen_const();% integer m(1:row_count, col_count) = 0; for i=1:row_count [x1, y1] = gen_start_point(a, b);% real
m(i, 1) = x1; m(i, 2) = y1; end end function [x, fval, eflag, iter, fcount] = Optimization_With_Analytic_Gradient(start_point) x0 = start_point; % inline function defitions
fun = @(x)(100*(x(2) - x(1)^2)^2 + (1 - x(1))^2); grad = @(x)[-400*(x(2) - x(1)^2)*x(1) - 2*(1 - x(1)); 200*(x(2) - x(1)^2)]; fungrad = @(x)deal(fun(x),grad(x)); % options setup
options = optimoptions( 'fminunc', ... 'Display','off',... 'OutputFcn',@bananaout,... 'Algorithm','trust-region', ... 'GradObj','on'); % calling fminunc
[x,fval,eflag,output] = fminunc(fungrad,x0,options); iter = output.iterations; fcount = output.funcCount; % plot window title
title 'Rosenbrock solution via fminunc with gradient' disp('Optimization_With_Analytic_Gradient...'); end
Then he mailed me,
The function is modified with randomly generated coefficients a & b. Therefore the location of global optima depends on these points. It's not (1, 1). So a, b must be listed in your report (obligatory!!!). Test results table must be recalculated. Function value (final) for clarity must be printed in exponential form. Contour and history plots should be created for each starting point separately
So, what I understand is, for different starting points, the optimization function would return different solutions.
But, in my case, no matter what I use as a starting point, the optimization always converges to (1, 1).
How can I solve this problem?
Best Answer