MATLAB: In an assignment A(I) = B, the number of elements in B and I must be the same

errorfsolveMATLAB

Hello, I am trying to solve the system below, but I get the error message "In an assignment A(I) = B, the number of elements in B and I must be the same.". I am not sure why this is happening since I am providing 2 equations to solve for 2 variables. I didn't put here the meanbl_i and covmatbl_i equations into the fcns(1) and fcns(2) equations just for you to better visualize the problem, but I get the error message above either way. Thank you very much for the help. Best wishes. Alexandre
function omeg()
guessomega_i=0;
guesslambda_i=4.2786;
guessom=[guessomega_i;guesslambda_i];
omresult=fsolve(@om,guessom);
omega_i=omresult(1)
lambda_i=omresult(2)
end
function fcns=om(x)
omega_i=x(1);
lambda_i=x(2);
meanbl_i=req+tau*covmat*P_i'*(tau*P_i*covmat*P_i'+omega_i)^(-1)*(v_i-P_i*req);
covmatbl_i=(1+tau)*covmat-tau^(2)*covmat*P_i'*(tau*P_i*covmat*P_i'+omega_i)^(-1)*P_i*covmat;
fcns(1)=lambda_i^(-1)*covmatbl_i^(-1)*(meanbl_i-(vec1'*covmatbl_i^(-1)*vec1)^(-1)*(vec1'*covmatbl_i^(-1)*meanbl_i-lambda_i)*vec1)-w_i;
fcns(2)=w_i'*meanbl_i+phimkt*sqrt(w_i'*covmatbl_i*w_i)-Hmkt;
end
WHERE:
req=[0.0332;0.0337;0.0312;0.0335;0.0298;0.0310;0.0286;0.0285;0.0270;0.0281;0.0281;0.0288];
tau=0.0167;
v_i=0;
P_i=[0,0,0,0,0,0,0,0,0,1,-1,0];
vec1=[1;1;1;1;1;1;1;1;1;1;1;1];
w_i=[0.3552;0.0521;0.0370;0.0538;0.0184;0.0094;0.0059;0.0072;0.0050;0.2372;0.1973;0.0215];
Hmkt=-0.0093;
phimkt=-1.2816;
covmat=[0.0026,0.0028,0.0019,0.0025,0.0012,0.0016,0.0007,0.0007,0.0001,0.0005,0.0005,0.0007; 0.0028,0.0033,0.0021,0.0028,0.0014,0.0017,0.0007,0.0008,0.0002,0.0005,0.0004,0.0007; 0.0019,0.0021,0.0022,0.0016,0.0010,0.0010,0.0005,0.0005,0.0001,0.0003,0.0002,0.0004; 0.0025,0.0028,0.0016,0.0030,0.0013,0.0019,0.0009,0.0008,0.0003,0.0006,0.0006,0.0009; 0.0012,0.0014,0.0010,0.0013,0.0010,0.0008,0.0005,0.0006,0.0002,0.0003,0.0002,0.0004; 0.0016,0.0017,0.0010,0.0019,0.0008,0.0015,0.0007,0.0006,0.0003,0.0005,0.0005,0.0006; 0.0007,0.0007,0.0005,0.0009,0.0005,0.0007,0.0005,0.0004,0.0002,0.0003,0.0003,0.0003; 0.0007,0.0008,0.0005,0.0008,0.0006,0.0006,0.0004,0.0004,0.0002,0.0002,0.0002,0.0003; 0.0001,0.0002,0.0001,0.0003,0.0002,0.0003,0.0002,0.0002,0.0002,0.0001,0.0001,0.0002; 0.0005,0.0005,0.0003,0.0006,0.0003,0.0005,0.0003,0.0002,0.0001,0.0002,0.0002,0.0003; 0.0005,0.0004,0.0002,0.0006,0.0002,0.0005,0.0003,0.0002,0.0001,0.0002,0.0004,0.0003; 0.0007,0.0007,0.0004,0.0009,0.0004,0.0006,0.0003,0.0003,0.0002,0.0003,0.0003,0.0005];

Best Answer

When you see an error like this, you should go back and look at the assignments that were done. The error message should even tell you WHERE the problem occurs, in which line it happened. You could have helped here by actually providing the complete error, so we would not need to guess at what you did wrong.
Anyway, I won't bother to run your code, as it is a bit of a mess, and won't even run without me making edits to correct some obvious flaws, so there are surely other problems. Besides, I refuse to run any code that will overwrite pi.
A quick scan however shows that there are only a few subscripted assignments in the code, so why not look at those lines? As quick of a scan then shows you are subtracting a vector from a mess before, and putting it into fcns(1), clearly a scalar assignment. Since the result must be at least a vector, I'll bet the error came from that line. So considering the error message, and considering what shape the right hand side of that expression will be, you might reconsider what you are trying to do.