MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

"Subscripted assignment dimension mismatch"

Asked by Pavlin on 15 Nov 2012

Hello,

Here is part of the code in which I can't avoid the following error: "Subscripted assignment dimension mismatch" at line 115. I do not have this error and the code runs fine, when I take away the main loop for the incremental load and instead have just one predefined value. If anyone can help, I will highly appreciate! If necessary I will send the whole code by mail.

Regards, Pavlin

CODE

for increment = 1:incmax

```    q_total=q_total+q_incr;
K(beam_DOFs,beam_DOFs)=0;
F(beam_DOFs,1)=0;
for el_y=1:beam_el_y
y1=(W/beam_el_y)*(el_y-1);
y3=y1+W/beam_el_y;
for el_x=1:beam_el_x
x1=(L/beam_el_x)*(el_x-1);
x3=x1+L/beam_el_x;
i1=(beam_el_y+1)*(el_x-1)+el_y;
i2=i1+beam_el_y+1;
el_DOFs=[2*i1-1; 2*i1 ; 2*i2-1 ; 2*i2; 2*i2+1; 2*i2+2; 2*i1+1; 2*i1+2];
Em=(E_el(el_x,el_y)/(1-nu^2))*[1  nu 0
nu 1  0
0  0  (1-nu)/2];
Ke=Q4_stiffness(x1,x3,y1,y3,Em,T);
K(el_DOFs,el_DOFs)=K(el_DOFs,el_DOFs)+Ke;
end
end```
```    all_DOFs=1:beam_DOFs;
fixed_DOFs_y=beam_DOFs;
fixed_DOFs_x=1:2:2*(beam_el_y+1);
fixed_DOFs=union(fixed_DOFs_x,fixed_DOFs_y);
free_DOFs=setdiff(all_DOFs,fixed_DOFs);
F(2,1)=-q_total;
U(free_DOFs,:)=K(free_DOFs,free_DOFs)\F(free_DOFs,:);
U(fixed_DOFs,:)=0;
ver_DOFs_top_side=2*(beam_el_y+1):2*(beam_el_y+1):beam_DOFs;
U(ver_DOFs_top_side,:);
ver_DOFs_top_side2=ver_DOFs_top_side;
ver_DOFs_top_side2(:,end)=[];
U(2,1);
U_tot=U_tot+U(2,1);```
```    for el_y=1:beam_el_y
y1=(W/beam_el_y)*(el_y-1);
y3=y1+W/beam_el_y;
for el_x=1:beam_el_x
x1=(L/beam_el_x)*(el_x-1);
x3=x1+L/beam_el_x;
i1=(beam_el_y+1)*(el_x-1)+el_y;
i2=i1+beam_el_y+1; % i2=(DCB_y+1)*el_x+el_y;
el_DOFs=[2*i1-1; 2*i1 ; 2*i2-1 ; 2*i2; 2*i2+1; 2*i2+2; 2*i1+1; 2*i1+2];
Em=(E_el(el_x,el_y)/(1-nu^2))*[1  nu 0
nu 1  0
0  0  (1-nu)/2];
el_displ=U(el_DOFs);
el_stress=zeros(3,8);
el_strain=zeros(3,8);
[el_stress el_strain]=Q4_stress(x1,x3,y1,y3,Em,el_displ);
Sigma_xx(el_x,el_y)=el_stress(1,1);
Sigma_yy(el_x,el_y)=el_stress(2,1);
Sigma_xy(el_x,el_y)=el_stress(3,1);
x(el_x,el_y)=x1; % For the mesh plot
y(el_x,el_y)=y1; % For the mesh plot
Sigma_xx_tot(el_x,el_y) = Sigma_xx_tot(el_x,el_y) + Sigma_xx(el_x,el_y);
Sigma_yy_tot(el_x,el_y) = Sigma_yy_tot(el_x,el_y) + Sigma_yy(el_x,el_y);
Sigma_xy_tot(el_x,el_y) = Sigma_xy_tot(el_x,el_y) + Sigma_xy(el_x,el_y);
S11 = 2*Sigma_xx_tot(el_x,el_y)/3 - Sigma_yy_tot(el_x,el_y)/3;
S22 = 2*Sigma_yy_tot(el_x,el_y)/3 - Sigma_xx_tot(el_x,el_y)/3;
S12 = Sigma_xy_tot(el_x,el_y);
Sigma_von_Mises = sqrt(3*(S11^2+S22^2+3*S12^2)/2);
if  Sigma_von_Mises > Sigma_y;
E_el(el_x,el_y)=E/10;
else
E_el(el_x,el_y)=E;
end
end
end```

last_x=zeros(1,beam_el_y);

LINE 115 last_x(1,:)=x(end,:)+L/beam_el_x;

x=[x;last_x]; x=[x,x(:,end)]; last_y=zeros(1,beam_el_y); last_y(1,:)=y(end,:); y=[y;last_y]; y=[y,y(:,end)+W/beam_el_y];

Products

No products are associated with this question.

Answer by Matt Fig on 15 Nov 2012
Edited by Matt Fig on 15 Nov 2012

This error occurs, much like the message tells you, when you try to put the wrong number (or shape) of items into an array. Here is a simple example:

```H = zeros(6);
try
H(:,1) = rand(2,3);  % Wrong shape.
catch
fprintf('%s\n',lasterr)
end
```
```try
H(:,1) = rand(7,1);  % Wrong number.
catch
fprintf('%s\n',lasterr)
end
```
```H(:,1) = rand(6,1)  % Correct shape and number!
```

1 Comment

Pavlin on 15 Nov 2012

Thanks,

I knew how it works, but what I had not taken into consideration is that x changes dimension after some convergence is executed by another file. Then, this function is called again and I have to clear the x value before it. The function will regenerate it again in with the correct form.