# Using for loop to build and plot data from a matrix

1 view (last 30 days)
Rodrigo Pena on 15 Nov 2021
Commented: Rodrigo Pena on 15 Nov 2021
Hello ! how are you?
I wrote a code that calculates strains by using matrices, and the answer that I receive from matlab is an array of (6x1) like this: Now, I would like to perform the exactly same calculations but for different temperatures (T) from -30 to 280 degrees celcius in steps of 10 and store these results in a variable called Epsilon. In the end of calculations I should obtain a matrix of (6x32). The approach that I used MATLAB returns me a message saying that right and left hand side have different numbers of arrays.
Please find the code below:
% Material Mechanical Properties
Ex = 41*10^3; % [MPa]
Ey = 10.4*10^3; % [MPa]
Ez = 10.4*10^3; % [MPa]
Gxy = 4.3*10^3; % [MPa]
Gyz = 3.5*10^3; % [MPa]
Gxz = 4.3*10^3; % [MPa]
Vxy = 0.28;
Vyz = 0.50;
Vxz = 0.28;
% Material Thermal Properties
alphax = 7.0*10^-6; % [1/°C]
alphay = 26*10^-6; % [1/°C]
alphaz = 26*10^-6; % [1/°C]
% Holding true Matrix symmetry
Vyx = (Vxy * Ex)/Ey; % [MPa]
Vzx = (Vxz * Ex)/Ez; % [MPa]
Vzy = (Vyz * Ey)/Ez; % [MPa]
% Creation of the compliance matrix
E = [1/Ex,-Vxy/Ey,-Vxz/Ez,0,0,0 ; -Vyx/Ex,1/Ey,-Vyz/Ez,0,0,0 ; -Vzx/Ex,-Vzy/Ey,1/Ez,0,0,0 ; 0,0,0,1/(2*Gxy),0,0 ; 0,0,0,0,1/(2*Gyz),0 ; 0,0,0,0,0,1/(2*Gxz)];
% Creation of thermal matrix
alpha = [alphax ; alphay ; alphaz ; 0 ; 0 ; 0];
% Calculation of the stiffness matrix
K = inv(E);
% Calculation of Thermal Moduli (β)
Beta = -K * alpha;
% Point material stress
Sigma_xx = 12; % [MPa]
Sigma_yy = 10; % [MPa]
Sigma_zz = 14; % [MPa]
Tau_xy = 6; % [MPa]
Tau_yz = 3; % [MPa]
Tau_zx = 9; % [Mpa]
% Point material ΔT
T = [-30:10:280]; % [°C]
% Formation of stress vector
Sigma = [Sigma_xx ; Sigma_yy ; Sigma_zz ; Tau_xy ; Tau_yz ; Tau_zx];
Epsilon = zeros(6,length(T));
for i = 1:length(T)
% Calculation of strains
Epsilon(i) = E * Sigma + T(i) * alpha;
end
Unable to perform assignment because the left and right sides have a different number of elements.
Thank you in advance !

Cris LaPierre on 15 Nov 2021
The calculation in your for loop returns more than 1 value, so your assignment must be to a number of rows and/or columns of the same size. Here, that means specifying rows and column.
Try this.
% Material Mechanical Properties
Ex = 41*10^3; % [MPa]
Ey = 10.4*10^3; % [MPa]
Ez = 10.4*10^3; % [MPa]
Gxy = 4.3*10^3; % [MPa]
Gyz = 3.5*10^3; % [MPa]
Gxz = 4.3*10^3; % [MPa]
Vxy = 0.28;
Vyz = 0.50;
Vxz = 0.28;
% Material Thermal Properties
alphax = 7.0*10^-6; % [1/°C]
alphay = 26*10^-6; % [1/°C]
alphaz = 26*10^-6; % [1/°C]
% Holding true Matrix symmetry
Vyx = (Vxy * Ex)/Ey; % [MPa]
Vzx = (Vxz * Ex)/Ez; % [MPa]
Vzy = (Vyz * Ey)/Ez; % [MPa]
% Creation of the compliance matrix
E = [1/Ex,-Vxy/Ey,-Vxz/Ez,0,0,0 ; -Vyx/Ex,1/Ey,-Vyz/Ez,0,0,0 ; -Vzx/Ex,-Vzy/Ey,1/Ez,0,0,0 ; 0,0,0,1/(2*Gxy),0,0 ; 0,0,0,0,1/(2*Gyz),0 ; 0,0,0,0,0,1/(2*Gxz)];
% Creation of thermal matrix
alpha = [alphax ; alphay ; alphaz ; 0 ; 0 ; 0];
% Calculation of the stiffness matrix
K = inv(E);
% Calculation of Thermal Moduli (β)
Beta = -K * alpha;
% Point material stress
Sigma_xx = 12; % [MPa]
Sigma_yy = 10; % [MPa]
Sigma_zz = 14; % [MPa]
Tau_xy = 6; % [MPa]
Tau_yz = 3; % [MPa]
Tau_zx = 9; % [Mpa]
% Point material ΔT
T = [-30:10:280]; % [°C]
% Formation of stress vector
Sigma = [Sigma_xx ; Sigma_yy ; Sigma_zz ; Tau_xy ; Tau_yz ; Tau_zx];
Epsilon = zeros(6,length(T));
for i = 1:length(T)
% Calculation of strains
Epsilon(:,i) = E * Sigma + T(i) * alpha;
% ^ Specify 'all rows'
end
plot(Epsilon) ##### 1 CommentShowHide None
Rodrigo Pena on 15 Nov 2021
Thank you ! Problem solved.

R2020b

### Community Treasure Hunt

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

Start Hunting!