Clear Filters
Clear Filters

Help with if statements

1 view (last 30 days)
Robert Fields
Robert Fields on 2 Apr 2023
Commented: Robert Fields on 2 Apr 2023
Hello I am getting errors for a varaible I am defining in a if loop any help would be great.
The error is stopping it at no QbarA Variable defined
close all, clear all,clc
E1 = 40;
E2 = 1.8;
V12 = 0.25;
V21 = 0.01125;
G12 = 0.7;
t = 0.005;
z0 = -t*2;
z1 = -t;
z2 = 0;
z3 = t;
z4 = t*2;
theta = [0,15,90,-30];
Q11 = E1/(1-V12*V21);
Q12 = (V21*E2)/(1-V12*V21);
Q22 = E2/(1-V12*V21);
Q66 = G12;
for i = 1:4
m = cosd(theta(i));
n = sind(theta(i));
end
Qbar11 = Q11.*m^4+2.*(Q12+2*Q66).*m^2.*n^2+Q22*n^4;
Qbar12 = (Q11+Q22-4*Q66)*m^2*n^2+Q12*(m^4+n^4);
Qbar16 = (Q11-Q12-2*Q66)*m^3*n+(Q12-Q22+2*Q66)*m*n^3;
Qbar22 = Q11.*n^4+2.*(Q12+2*Q66).*m^2.*n^2+Q22*m^4;
Qbar26 = (Q11-Q12-2*Q66)*m*n^3+(Q12-Q22+2*Q66)*m^3*n;
Qbar66 = (Q11+Q22-2*Q12-2*Q66)*m^2*n^2+Q66*(m^4+n^4);
Qbar = [Qbar11 Qbar12 Qbar16
Qbar12 Qbar22 Qbar26
Qbar16 Qbar26 Qbar66]
if i == 1
QbarA = Qbar;
end
if i == 2
QbarB = Qbar;
end
if i == 3
QbarC = Qbar;
end
if i == 4
QbarD = Qbar;
end
A = QbarA*t+QbarB*t+QbarC*t+QbarD*t
B = 1/2*(QbarA*(z1^2-z0^2)+QbarB*(z2^2-z1^2)+QbarC*(z3^2-z2^2)+QbarD*(z4^2-z3^2))
D = 1/3*(QbarA*(z1^3-z0^3)+QbarB*(z2^3-z1^3)+QbarC*(z3^3-z2^3)+QbarD*(z4^3-z3^3))
Nx = 1200*4*t;
Ny = -600*4*t;
Nxy = 600*4*t;
My = 36/12;
Mx = -18/18;
Mxy = 0;
N = [Nx;Ny;Nxy]
M = [Mx;My;Mxy]
Output = [A,B;B,D]^-1*[N,M]

Accepted Answer

Adam Drake
Adam Drake on 2 Apr 2023
Edited: Adam Drake on 2 Apr 2023
"i" will always equal 4 because your for loop has no condition that will stop it from going to the end. Because "i" will always be 4 at the end of the loop, QbarA, QbarB, QbarC will never get set and therefore, when you get to the line where "A" is being set, you will get an error.
The following code is probably closer to what you want, but your output function will return an array mismatch error because A, B, and D are 3x3 arrays, N and M are 1x3 arrays. Would need background on your algorithm to help further.
clc, close all, clear variables
E1 = 40;
E2 = 1.8;
V12 = 0.25;
V21 = 0.01125;
G12 = 0.7;
t = 0.005;
z0 = -t*2;
z1 = -t;
z2 = 0;
z3 = t;
z4 = t*2;
theta = [0,15,90,-30];
Q11 = E1/(1-V12*V21);
Q12 = (V21*E2)/(1-V12*V21);
Q22 = E2/(1-V12*V21);
Q66 = G12;
Nx = 1200*4*t;
Ny = -600*4*t;
Nxy = 600*4*t;
My = 36/12;
Mx = -18/18;
Mxy = 0;
N = [Nx;Ny;Nxy];
M = [Mx;My;Mxy];
for i = 1:4
m = cosd(theta(i));
n = sind(theta(i));
Qbar11 = Q11.*m^4+2.*(Q12+2*Q66).*m^2.*n^2+Q22*n^4;
Qbar12 = (Q11+Q22-4*Q66)*m^2*n^2+Q12*(m^4+n^4);
Qbar16 = (Q11-Q12-2*Q66)*m^3*n+(Q12-Q22+2*Q66)*m*n^3;
Qbar22 = Q11.*n^4+2.*(Q12+2*Q66).*m^2.*n^2+Q22*m^4;
Qbar26 = (Q11-Q12-2*Q66)*m*n^3+(Q12-Q22+2*Q66)*m^3*n;
Qbar66 = (Q11+Q22-2*Q12-2*Q66)*m^2*n^2+Q66*(m^4+n^4);
Qbar = [Qbar11 Qbar12 Qbar16
Qbar12 Qbar22 Qbar26
Qbar16 Qbar26 Qbar66];
A = Qbar*t+Qbar*t+Qbar*t+Qbar*t;
B = 1/2*(Qbar*(z1^2-z0^2)+Qbar*(z2^2-z1^2)+Qbar*(z3^2-z2^2)+Qbar*(z4^2-z3^2));
D = 1/3*(Qbar*(z1^3-z0^3)+Qbar*(z2^3-z1^3)+Qbar*(z3^3-z2^3)+Qbar*(z4^3-z3^3));
Output = [A,B;B,D]^-1.*[N,M]
end
  2 Comments
Robert Fields
Robert Fields on 2 Apr 2023
The code is for a composite Laminate Analysis. I am trying to calculate the midplane stresses. The layers of the composits are 0,15,90,-30 So I need the Qbar of each of those angles added up for A B and D
Robert Fields
Robert Fields on 2 Apr 2023
You led me in the right direction with changing the for loop to contain everything thank you my new code looks like this and is doing how I hope.
close all, clear all, clc
E1 = 40*10^6; %Msi
E2 = 1.8*10^6; %Msi
V12 = 0.25;
V21 = E2/E1*V12;
G12 = 0.7*10^6; %Msi
t = 0.005; %in/ply
Xt = 230; %ksi
Xc = -180; %ksi
Yt = 7; %ksi
Yc = -325; %ksi
S = 10; %ksi
z0 = -t*2;
z1 = -t;
z2 = 0;
z3 = t;
z4 = t*2;
% After performing the calculation with to find out the build up it was
% determined it would be build up 3.
theta = [0,15,90,-30];
Q11 = E1/(1-V12*V21);
Q12 = (V21*E2)/(1-V12*V21);
Q22 = E2/(1-V12*V21);
Q66 = G12;
for i = 1:4
m = cosd(theta(i));
n = sind(theta(i));
Qbar11 = Q11.*m^4+2.*(Q12+2*Q66).*m^2.*n^2+Q22*n^4;
Qbar12 = (Q11+Q22-4*Q66)*m^2*n^2+Q12*(m^4+n^4);
Qbar16 = (Q11-Q12-2*Q66)*m^3*n+(Q12-Q22+2*Q66)*m*n^3;
Qbar22 = Q11.*n^4+2.*(Q12+2*Q66).*m^2.*n^2+Q22*m^4;
Qbar26 = (Q11-Q12-2*Q66)*m*n^3+(Q12-Q22+2*Q66)*m^3*n;
Qbar66 = (Q11+Q22-2*Q12-2*Q66)*m^2*n^2+Q66*(m^4+n^4);
Qbar = [Qbar11 Qbar12 Qbar16
Qbar12 Qbar22 Qbar26
Qbar16 Qbar26 Qbar66]
%Displayed in this order Qbar0, Qbar15, Qbar90, Qbar-30
if i == 1
QbarA = Qbar;
elseif i == 2
QbarB = Qbar;
elseif i == 3
QbarC = Qbar;
elseif i == 4
QbarD = Qbar;
end
end
%Question 1
A = QbarA*t+QbarB*t+QbarC*t+QbarD*t %lb/in
B = 1/2*(QbarA*(z1^2-z0^2)+QbarB*(z2^2-z1^2)+QbarC*(z3^2-z2^2)+QbarD*(z4^2-z3^2))%lb
D = 1/3*(QbarA*(z1^3-z0^3)+QbarB*(z2^3-z1^3)+QbarC*(z3^3-z2^3)+QbarD*(z4^3-z3^3))%lbin
%Question 2
Nx = 1200*4*t;
Ny = -600*4*t;
Nxy = 600*4*t;
My = 36/12;
Mx = -18/18;
Mxy = 0;
N = [Nx;Ny;Nxy] %lb/in
M = [Mx;My;Mxy] %lbin/in
Output = [A,B;B,D]^-1*[N;M]

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!