Help with if statements
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Robert Fields
il 2 Apr 2023
Commentato: Robert Fields
il 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]
0 Commenti
Risposta accettata
Adam Drake
il 2 Apr 2023
Modificato: Adam Drake
il 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Visualization and Data Export in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!