MATLAB: Fminsearchbnd morphs the matrix

colonfile exchangefminsearchbndMATLAB

I'm trying to run fminsearchbnd while passing these variables: Plats (4 value vector) Pcn (7,4 matrix) Mm (constant) Avail (7,10 matrix) Percs (10,4 matrix)
I'm trying to give fminsearchbnd "Percs" (percentages) as the argument to use, but it keeps morphing the size and going out of bounds.
function x = solver(Percs, Plats, Pcn, Mm, Avail)
%Easy way to round everything to 2 decimal places
format bank;
Req = zeros(7,4);
for i=1:7
for j=1:4
Req(i,j) = Plats(j)*Pcn(i,j)*Mm;
end
end
Pulled = zeros(7,10);
Rem = zeros(7,10);
for i=1:10
for j=1:7
%-*-*-*-*-Line 47 below-*-*-*-*-
Pulled(j,i) = (Percs(i,1)*Req(j,1)*.01) + ...
(Percs(i,2)*Req(j,2)*.01) + ...
(Percs(i,3)*Req(j,3)*.01) + ...
(Percs(i,4)*Req(j,4)*.01);
Rem(j,i) = Avail(j,i) - Pulled(j,i);
end
end
x = sum(Req,2) - sum(Pulled,2);
end
The idea is to get x as close to zero as possible while finding the optimal percentages (Perc) and keeping the remaining (Rem) numbers at or above a certain threshold (could be zero or -70 for example). I haven't figured out how to incorporate the threshold on Rem yet, but I'm already running into problems.
I'm running the below command:
xsol = fminsearchbnd(@(Percs) solver(Percs, Plats, Pcn, Mm, Avail), Percs, LB, UB)
LB and UB are 10,4 matrices of 0's and 100's respectively.
I'm receiving the following error:
Attempted to access Percs(2,1); index out of bounds because size(Percs)=[1,40].
Error in ==> solver at 47
When I type size(Percs) immediately at the prompt, it says ans = 10.00 4.00.
This is the first time I've ever used MATLAB, if you couldn't tell. Please be gentle 😉
What am I doing wrong?

Best Answer

I've just uploaded a new version of fminsearchbnd, which should solve the problem, and sent Zack a copy via direct e-mail.