How can I fix this error?

%Compute stresses through the thickness of a laminate
%Read in number of layers
num_layers=input('Enter the number of layers: ');
%Read in material properties
E_L=input('Enter longitudinal modulus E_L [GPa]: ');
E_T=input('Enter transverse modulus E_T [GPa]: ');
G_LT=input('Enter shear modulus G_LT [GPa]: ');
v_LT=input('Enter Poisson"s Ratio v_LT: ');
thickness=input('Enter lamina thickness: ');
%Compute Q
Q=calculate_Q(E_L,E_T,G_LT,v_LT);
%Givin mid-plane strains and curvatures
%Format: [εx,εy,γxy, κx, κy]
epsilon_x=input('Enter εx: ');
epsilon_y=input('Enter εy: ');
gamma_xy=input('Enter γxy: ');
kappa_x=input('Enter κx: ');
kappa_y=input('Enter κy: ');
mid_plane_data=[epsilon_x, epsilon_y, gamma_xy, kappa_x, kappa_y];
%Compute stresses for each layer
top_bottom_stresses = cell(num_layers, 2);
for i = 1:size(mid_plane_data, 1)
data = mid_plane_data(i,:);
strain = data(1:3)';
curvature = data(4:5)';
% Compute S matrix
S = (Q)^-1;
% Compute strain due to curvature
strain_due_to_curvature = S .* curvature .* thickness / 2;
% Compute total strain at top and bottom
total_strain_top = strain + strain_due_to_curvature;
total_strain_bottom = strain - strain_due_to_curvature;
% Compute stress at top and bottom
stress_top = Q .* total_strain_top;
stress_bottom = Q .* total_strain_bottom;
top_bottom_stresses{i,1} = stress_top;
top_bottom_stresses{i,2} = stress_bottom;
end
%Display Results
fprintf('\nStress Values (in MPa):\n');
fprintf('%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n', 'Layer', ...
'σx_top', 'σy_top', 'τxy_top', 'σx_bottom', 'σy_bottom', ...
'τxy_bottom');
for i=1:num_layers
stress_top=top_bottom_stresses{i,1};
stress_bottom=top_bottom_stresses{i,2};
fprintf('%-10d %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f\n',i,stress_top(1), ...
stress_top(2),stress_top(3),stress_bottom(1),stress_bottom(2), ...
stress_bottom(3));
end
%Plot stress variations through thickness
layer_thickness=0:thickness:(num_layers*thickness);
figure;
hold on;
for i=1:num_layers
stress_top=top_bottom_stresses{i,1};
stress_bottom=top_bottom_stresses{i,2};
plot([stress_top(1),stress_bottom(1)], [layer_thickness(i), layer_thickness(i)],'r-');
plot([stress_top(2),stress_bottom(2)], [layer_thickness(i), layer_thickness(i)],'b-');
plot([stress_top(3),stress_bottom(3)], [layer_thickness(i), layer_thickness(i)],'g-');
end
hold off;
xlabel('Stress [MPa]');
ylabel('Thickness [m]');
title('Stress Variation through Thickness of Laminate');
legend('σx', 'σy', 'τxy');
grid on;
%Function to compute Q matrix
function Q=calculate_Q(E_L,E_T,G_LT,v_LT)
Q=zeros(3,3);
Q(1,1)=E_L/(1-v_LT^2);
Q(1,2)=v_LT*E_T/(1-v_LT^2);
Q(2,1)=Q(1,2);
Q(2,2)=E_T/(1-v_LT^2);
Q(3,3)=G_LT;
end
OUTPUT:
Enter the number of layers: 4
Enter longitudinal modulus E_L [GPa]: 38.60
Enter transverse modulus E_T [GPa]: 8.27
Enter shear modulus G_LT [GPa]: 4.14
Enter Poisson"s Ratio v_LT: 0.26
Enter lamina thickness: 0.002
Enter εx: 0.05
Enter εy: 0.00
Enter γxy: 0.00
Enter κx: 0
Enter κy: 0
Arrays have incompatible sizes for this operation.
Error in Project1 (line 40)
strain_due_to_curvature = S .* curvature .* thickness / 2;
Related documentation

5 Comments

Q is a 3x3 matrix, so S is also a 3x3 matrix, but curvature is a 2x1 vector.
Are those the sizes you expect?
You cannot multiply a 3x3 matrix with a 2x1 vector, neither by element-wise multiplication (.*, which is what you are using) nor by matrix multiplication (*).
I missed a variable, it works now! Thank you for pointing that out. However, I am now getting this error,
Index exceeds the number of array elements. Index must not exceed 0.
Error in Project1 (line 63)
fprintf('%-10d %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f %-10.2f\n',i,stress_top(1),stress_top(2),stress_top(3),stress_bottom(1),stress_bottom(2),stress_bottom(3));
You're welcome!
Check the sizes of stress_top and stress_bottom. Looks like at least one of them is empty and it shouldn't be.
If that doesn't help, please share the updated code so I can run it myself and check.
I've got it figured out. Thank you for your help!
You're welcome!

Sign in to comment.

Answers (0)

Categories

Find more on Stress and Strain in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 3 Apr 2024

Commented:

on 4 Apr 2024

Community Treasure Hunt

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

Start Hunting!