"Matrix dimensions must agree"

Hello!
I have the error message "Matrix dimensions must agree" when I run the code below.  Equations A-Cxx are giving me this error. Please can any one kindly assist to resolve the mistake?
Thank you.
The following is the code:
Alpha = 0.971;
G = 2.38*10^9;
K = 1.5*10^9;
Sy = 0.4;
Sd = 0.6;
w = 100;
s = 1j*w;
rx = 1;
w_hat = [1.820e-01+4.538e+08i,7.334e-02+9.083e+07i,1.152+18.511i,0.000+0.109i];
M_r = [-2.508-0.000i,-0.000+0.000i,0.000+0.000i,0.000-0.000i];
R_s = [-7.780+0.000i,-0.311+0.000i,0.000-0.000i,-0.000+0.000i];
N_w = [ -Inf+0i,Inf+0i,-1.6103e-32+7.816e-33i,1.793e-36+2.242e-37i];
Ao = -2.644e+07+6.392e-02i;
s1 = 1.023e-06+9.552e+02i;
s2 = 3.686e+11+1.967e+20i;
s3 = 5.429e+09+2.377e+18i;
s4 = -1.685e-07+5.794e+02i;
s5 = 6.3294e+09+4.780e+18i;
s6 = 1.001e+08+5.778e+16i;
i = nelements;
j = 1:ndispbcelements;
p = sqrt((x(k)-xlayer(i))^2+(y(k)-ylayer(i))^2+(z(k)-zlayer(i))^2);
v1 = ((y2-y1)*(z3-z1)-(z2-z1)*(y3-y1))^2;
v2 = ((z2-z1)*(x3-x1)-(x2-x1)*(z3-z1))^2;
v3 = ((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))^2;
l = (v1+v2+v3)^0.5;
nx(i) = ((y2-y1)*(z3-z1)-(z2-z1)*(y3-y1))/l;
ri = (x(k)-xlayer(i))/p;
rj = (y(k)-ylayer(i))/p;
rk = (z(k)-zlayer(i))/p;
A = -(Ao*rj)/(4*pi*p^2)*sum((N_w.*((w_hat.^4).*(s1)+((w_hat.^2).*s2)+s3)).*((1+w_hat.*p)));
B = -(Ao*rk)/(4*pi*p^2)*sum((N_w.*((w_hat.^4).*(s4)+((w_hat.^2).*s5)+s6)).*((1+w_hat.*p)));
E = -(Ao*ri*nx(dispbcelements(j,1)))/(4*pi*p^2)*sum((N_w.*(1+(w_hat.*p)+(R_s+(w_hat.^2).*M_r))));
L = (Ao*ri*nx(dispbcelements(j,1))+rx/(4*pi*p^4)*sum((N_w.*(2*M_r.*(w_hat.^2).*(p^2))+((3*(w_hat.*p)+3)-((p^2).*(1+w_hat.*p)).*R_s))));
M = (Ao*ri*nx(dispbcelements(j,1))/(2*pi*p^4)*sum((N_w.*M_r).*(w_hat.^2).*(p^2))+((3*(w_hat.*p)+3)));
N = -(Ao*ri^2*rx)/(2*pi*p^4)*sum((N_w.*M_r.*((w_hat.^3).*(p^3))+(6*(w_hat.^2).*(p^2))+(15*(w_hat.*p)+15)));
% final equation
Cxx = (((K-(2/3*G))*E)+((alpha)*s*((Sy*A*nx(dispbcelements(j,1))))+(Sd*B*nx(dispbcelements(j,1))))+(G*(L+M+N)));

Answers (1)

Without knowing the geometry of all the undefined variables, nobody can do much but guess. It's helpful to provide sample code with at least some dummy variables to make the code run for someone else. Nobody can replicate the problem otherwise.
Alternatively, you can try troubleshooting. Start with the first expression which causes an error:
A = -(Ao*rj)/(4*pi*p^2)*sum((N_w.*((w_hat.^4).*(s1)+((w_hat.^2).*s2)+s3)).*((1+w_hat.*p)));
break it down into sub-expressions and observe the size of each
size(-(Ao*rj)/(4*pi*p^2))
size((N_w.*((w_hat.^4).*(s1) + ((w_hat.^2).*s2)+s3)))
size((1+w_hat.*p))
Look for a dimension mismatch. Maybe the expressions will need to be split further before it reveals itself. Once you find the dimension mismatch, find out why it occurs. Maybe it's an error in the expression itself. Maybe the data isn't oriented correctly prior to reaching this point.

Products

Release

R2019a

Asked:

on 24 Mar 2021

Answered:

DGM
on 24 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!