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.
y0 = f(x0);
while (delta_F > e) && (i < maxLoop)
x1 = x0+d;
y1 = f(x1);
if (y1 < y0) %# new starting point, so swap points
x0 = x1;
y0 = y1;
d = a*d;
else %# same starting point, refine step and see if we're done
d = -b*d;
delta_F = abs(x1-x0)/2;
end
i = i+1;
end
This removes half the calls to f
and the dynamic resizing of vF
, which is horrendously slow, especially as vF
gets big.