MATLAB: “Unable to perform assignment because the left and right sides have a different number of elements”

loop

Hi, my code has the following error:
"Unable to perform assignment because the left and right sides have a different number of elements.
Error in sym/privsubsasgn (line 1126)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 963)
C = privsubsasgn(L,R,inds{:});
Error in main (line 49)
S(j) = solve(eqn, DegU, 'Real', true);"
My code is as follows:
x = ones(1,26);
alpha = (4*10^-3)/(4.8*10^-2);
[phos_rate] = (0.01:0.001:0.035);
for i = 1:26
inv_phos_rate(i) = x(i)/phos_rate(i);
inv_phos_rate;
end
for i = 1:length(phos_rate)
c1(i) = sqrt(7*(0.1/0.025)*(inv_phos_rate(i))^2);
c2(i) = (((4*10^-4)*((10^-2)/(4.8*(10^-2)*4*(10^-2))))*(1+phos_rate(i)))^-1;
c1;
c2;
end
for j = 1:26
syms DegU
eqn = ((alpha*((c1(j))^2) + DegU^2)/((c1(j))^2 + DegU^2)) - (DegU)/(c2(j)) ==0;
S(j) = solve(eqn, DegU, 'Real', true);
S;
end
plot(phos_rate, S)
Could anybody explain what is going wrong?

Best Answer

Hi Ellen,
The issue comes because for one of the equation, there are three roots, but till that iteration, only single root is present in S. To observe the different roots, you can make the following modifications
S{j} = solve(eqn, DegU, 'Real', true); % Replace S(j) with S{j}
S1(j) = S{j}(1);
Placing these lines in the code and then while plotting use S1 instead of S
plot(phos_rate, S1)
Look that S will have one of the roots as 3 x 1 and due to this the error.
Hope this helps.
Regards,
Sriram