MATLAB: Help with a MuPAD error please

errorfunctioniterationmupad

Hi, I'm trying to run a program I put together, but I keep encountering a MuPAD error partway through. The program is exactly the same as a previous program I created, but with a few more substitutions than the last, and a few more bits of data entered too. Functionally it should be the same though, and that's why I'm confused? So, what's causing the MuPAD error? How do I debug this? I didn't understand the literature online, maybe someone with some experience might be able to help. I can also post the code if it's more useful.
Thanks
[Code merged from comment]
data = csvread('data.csv');
assert (mod(size(data, 1), 1) == 0, ...
'Input data must have an integer multiple of 1 rows');
assert (size(data, 2) == 8, ...
'Input data must have exactly eight columns.');
syms d1;
nsys = size(data, 1);
for k = 1 : nsys,
F = solve_d_two_layers_eq(data((k-1) + (1:1), 1:end));
d(k, :) = solve(F);
end
dd = double(d);
fid=fopen('d_values.csv','w');
fprintf(fid,'%+5.5f, %+5.5fi\n',[real(dd(:)), imag(dd(:))].');
fclose(fid);
and the function file:
function F = solve_d_two_layers_eq(p)
assert (ndims(p) ==2, ...
'System parameters ''p'' must be 2D matrix.');
assert (all(size(p) ==[1,8]), ...
'System parameters must be 1-by-8 matrix.');
syms d1
n0 = 1;
y = p(:,1);
R = p(:,2);
n1 = p(:,3);
k1 = p(:,4);
n2 = p(:,5);
k2 = p(:,6);
n3 = p(:,7);
k3 = p(:,8);
d2 = 300;
g1 = ((n0.^2 - n1.^2 - k1.^2)./((n1 + n2).^2 + k1.^2));
g2 = ((n1.^2 - n2.^2 + k1.^2 - k2.^2)./((n1 + n2).^2 + (k1 + k2).^2));
h1 = ((2.*n0.*k1)./((n0 + n1).^2 + k1.^2));
h2 = ((2.*(n1.*k2 - n2.*k1))./((n1 + n2).^2 + (k1 + k2).^2));
a1 = ((2.*pi().*k1.*d1)./y);
b1 = ((2.*pi().*n1.*d1)./y);
p2 = ((exp(a1)).*cos(b1));
q2 = ((exp(a1)).*sin(b1));
t2 = ((exp(-1.*a1)).*(g2.*(cos(b1)) + h2.*(sin(b1))));
u2 = ((exp(-1.*a1)).*(h2.*(cos(b1)) - g2.*(sin(b1))));
p12 = (p2 + g1.*t2 - h1.*u2);
q12 = (q2 + h1.*t2 + g1.*u2);
t12 = (t2 + g1.*p2 - h1.*q2);
u12 = (u2 + h1.*p2 + g1.*q2);
g3 = ((n2.^2 - n3.^2 + k2.^2 - k3.^2)./((n2 + n3).^2 + (k2 + k3).^2));
h3 = ((2.*(n2.*k3 - n3.*k2))./((n2 + n3).^2 + (k2 + k3).^2));
a2 = ((2.*pi().*k2.*d2)./y);
b2 = ((2.*pi().*n2.*d2)./y);
p3 = ((exp(a2)).*(cos(b2)));
q3 = ((exp(a2)).*(sin(b2)));
t3 = ((exp(-1.*a2)).*(g3.*(cos(b2)) + h3.*(sin(b2))));
u3 = ((exp(-1.*a2)).*(h3.*(cos(b2)) - g3.*(sin(b2))));
r2 = ((exp(a1)).*(g2.*(cos(b1)) - h2.*(sin(b1))));
s2 = ((exp(a1)).*(h2.*(cos(b1)) + g2.*(sin(b1))));
v2 = ((exp(-1.*a1)).*(cos(b1)));
w2 = ((-1.*(exp(-1.*a1))).*(sin(b1)));
r12 = (r2 + g1.*v2 - h1.*w2);
s12 = (s2 + h1.*v2 + g1.*w2);
v12 = (v2 + g1.*r2 - h1.*s2);
w12 = (w2 + h1.*r2 + g1.*s2);
p13 = (p12.*p3 - q12.*q3 + r12.*t3 - s12.*u3);
q13 = (q12.*p3 + p12.*q3 + s12.*t3 + r12.*u3);
t13 = (t12.*p3 - u12.*q3 + v12.*t3 - w12.*u3);
u13 = (u12.*p3 + t12.*q3 + w12.*t3 + v12.*u3);
F = (((t13.^2 + u13.^2)./(p13.^2 + q13.^2)) - R);
Error message:
>> run solve_d_two_layers
Warning: Explicit solution could not be found.
> In solve at 83
In solve_d_two_layers at 58
In run at 74
??? Error using ==> mupadmex
Error in MuPAD command: Subscripted assignment dimension
mismatch
Error in ==> sym.sym>sym.subsasgn at 1435
C = mupadmex('symobj::subsasgn',A.s,B.s,inds{:});
Error in ==> solve_d_two_layers at 58
d(k, :) = solve(F);
Error in ==> run at 74
evalin('caller',[script ';']);

Best Answer

When solve() is not able to find solutions for specific parameters, it is not likely to return exactly the same number of results as when it is able to find one or more solutions. If the number of elements from solve(F) at a particular iteration does not match the number of columns already allocated to "d" then you would get the error you see.
Instead of solve() and assigning to d(k,:) in one step, assign the results of solve() to a temporary variable and check the variable for valid answers before you assign into d(k,:)