I would like to know if there is some way to vectorize this code. I tried so hard to do it... but failed.
while (delta_F > e) && (i < maxLoop)
x1 = x0+d;
y0 = f(x0);
y1 = f(x1);
if y1 < y0
x0= x1;
d = a*d;
else
vF = [vF;x1];
d = -b*d;
end
i = i + 1;
if length(vF) > 1
ultm = vF(end);
pultm = vF(end-1);
delta_F = abs(ultm+pultm)/2;
end
end
It's a simple implementation of the Rosenbrock method for finding the min of a function.
In general, Matlab's vectorization works over fixed-size arrays/matrices. If you want to speed up this code in some other ways, the single biggest thing you could do is reuse your previous results in each iteration of the loop and get rid of the extraneous variables.
This removes half the calls to
f
and the dynamic resizing ofvF
, which is horrendously slow, especially asvF
gets big.