Can't get tiled graph to show two lines inside of a loop, can anyone help?

6 views (last 30 days)
Darshan Patel on 28 Jan 2020
Edited: Jeremy Marcyoniak on 28 Jan 2020
Here is my code, I want it to plot both values from the first for loop, but it is only plotting the second value. Am I doing something wrong with my holds?
clear
clc
hold on
E1s = [162 38.6] ;
E2s = [8.34 8.27];
G12s = [2.07 4.14];
v12s = [.34 .26];
vfs = [.6 .45];
thetas = -90:10:90;
for i = 1:2;
E1 = E1s(i);
E2 = E2s(i);
G12 = G12s(i);
v12 = v12s(i);
v21 = (v12/E1)*E2;
vf = vfs(i);
Q11 = E1/(1-v12*v21);
Q12 = (v12*E2)/(1-v12*v21);
Q21 = Q12;
Q22 = E2/(1-v12*v21);
Q66 = G12;
Qbar = zeros(3);
Results = zeros(19,6);
for i1 = 2:20;
theta = thetas(i1-1);
c = cosd(theta);
s = sind(theta);
Qbar(1,1) = Q11*c^4 + Q22*s^4+2*(Q12+2*Q66)*(s^2*c^2);
Qbar(1,2) = (Q11+Q22-4*Q66)*s^2*c^2+Q12*(c^4+s^4);
Qbar(2,2) = Q11*s^4+Q22*c^4+2*(Q12+2*Q66)*(s^2*c^2);
Qbar(1,3) = (Q11-Q12-2*Q66)*(c^3*s)-(Q22-Q12-2*Q66)*(c*s^3);
Qbar(2,3) = (Q11-Q12-2*Q66)*(c*s^3)-(Q22-Q12-2*Q66)*(c^3*s);
Qbar(3,3) = (Q11+Q22-2*Q12-2*Q66)*(s^2*c^2)+Q66*(s^4 + c^4);
Qbar(2,1) = Qbar(1,2);
Qbar(3,1) = Qbar(1,3);
Qbar(3,2) = Qbar(2,3);
Sbar = inv(Qbar);
Ex = 1/Sbar(1,1);
Results(i1-1,1)= Ex;
Ey = 1/Sbar(2,2);
Results(i1-1,2)= Ey;
Gxy = 1/Sbar(3,3);
Results(i1-1,3)= Gxy;
Vxy = -Sbar(1,2)*Ex;
Results(i1-1,4)= Vxy;
Nxxy = Sbar(2,3)/Sbar(1,1);
Results(i1-1,5)= Nxxy;
Nxyx = Sbar(2,3)/Sbar(3,3);
Results(i1-1,6)= Nxyx;
tiledlayout(2,3);
nexttile
plot(thetas,Results(:,1));
title('Ex over theta');
hold on
nexttile;
plot(thetas,Results(:,2));
title('Ey over theta');
hold on
nexttile;
plot(thetas,Results(:,3));
title('Gxy over theta');
hold on
nexttile;
plot(thetas,Results(:,4));
title('Vxy over theta');
hold on
nexttile;
plot(thetas,Results(:,5));
title('Nx,xy over theta');
hold on
nexttile;
plot(thetas,Results(:,6));
title('Nxy,x over theta');
hold on
end
end
legend('Carbon Fiber','Scotch Ply')
hold off

1 Comment

Geoff Hayes on 28 Jan 2020
Darshan - your inner for loop seems to iterate 19 times. Are 19 different figures created or is the same one re-used 19 times? If re-used, what happens to the data from the previous iteration?
From nexttile, nexttile creates an axes object and places it into the next empty tile of the tiled chart layout that is in the current figure. It seems to me that each time you create a new layout with tiledlayout then you will lose the plots that you had before (or a new figure will be created). If this is true, then it doesn't really matter if you have a hold on or not since you will be creating new tiles on each iteration. (I'm guessing here since I don't have the version of MATLAB that supports this function.)

Jeremy Marcyoniak on 28 Jan 2020
Edited: Jeremy Marcyoniak on 28 Jan 2020
I found two issues. One is, your plot commands are inside the nested FOR loop and they need to be outside the first END statement so that they'll be in the outer loop but not inside the inner loop. (Essentially, you are plotting your results array 38 times.)
Second, I found that subplot works better here than nexttile. You can probably plot using nexttile by simply storing the results as a 3D array instead of throwing away each iteration after plotting it and then plotting outside of both loops.
Thirdly, I added a linestyle array to differentiate the plots.
Here are the modifications I made:
clearvars; close all; clc
E1s = [162 38.6] ;
E2s = [8.34 8.27];
G12s = [2.07 4.14];
v12s = [.34 .26];
vfs = [.6 .45];
thetas = -90:10:90;
subplot(2,3,1); axh = axes;
fmt = {'b-'; 'r--'};
for i = 1:2
E1 = E1s(i);
E2 = E2s(i);
G12 = G12s(i);
v12 = v12s(i);
v21 = (v12/E1)*E2;
vf = vfs(i);
Q11 = E1/(1-v12*v21);
Q12 = (v12*E2)/(1-v12*v21);
Q21 = Q12;
Q22 = E2/(1-v12*v21);
Q66 = G12;
Qbar = zeros(3);
Results = zeros(19,6);
for i1 = 2:20
theta = thetas(i1-1);
c = cosd(theta);
s = sind(theta);
Qbar(1,1) = Q11*c^4 + Q22*s^4+2*(Q12+2*Q66)*(s^2*c^2);
Qbar(1,2) = (Q11+Q22-4*Q66)*s^2*c^2+Q12*(c^4+s^4);
Qbar(2,2) = Q11*s^4+Q22*c^4+2*(Q12+2*Q66)*(s^2*c^2);
Qbar(1,3) = (Q11-Q12-2*Q66)*(c^3*s)-(Q22-Q12-2*Q66)*(c*s^3);
Qbar(2,3) = (Q11-Q12-2*Q66)*(c*s^3)-(Q22-Q12-2*Q66)*(c^3*s);
Qbar(3,3) = (Q11+Q22-2*Q12-2*Q66)*(s^2*c^2)+Q66*(s^4 + c^4);
Qbar(2,1) = Qbar(1,2);
Qbar(3,1) = Qbar(1,3);
Qbar(3,2) = Qbar(2,3);
Sbar = inv(Qbar);
Ex = 1/Sbar(1,1);
Results(i1-1,1)= Ex;
Ey = 1/Sbar(2,2);
Results(i1-1,2)= Ey;
Gxy = 1/Sbar(3,3);
Results(i1-1,3)= Gxy;
Vxy = -Sbar(1,2)*Ex;
Results(i1-1,4)= Vxy;
Nxxy = Sbar(2,3)/Sbar(1,1);
Results(i1-1,5)= Nxxy;
Nxyx = Sbar(2,3)/Sbar(3,3);
Results(i1-1,6)= Nxyx;
end
subplot(2,3,1), hold on
plot(thetas,Results(:,1),fmt{i});
title('Ex over theta');
hold on
subplot(2,3,2)
plot(thetas,Results(:,2),fmt{i});
title('Ey over theta');
hold on
subplot(2,3,3)
plot(thetas,Results(:,3),fmt{i});
title('Gxy over theta');
hold on
subplot(2,3,4)
plot(thetas,Results(:,4),fmt{i});
title('Vxy over theta');
hold on
subplot(2,3,5)
plot(thetas,Results(:,5),fmt{i});
title('Nx,xy over theta');
hold on
subplot(2,3,6)
plot(thetas,Results(:,6),fmt{i});
title('Nxy,x over theta');
hold on
end
legend('Carbon Fiber','Scotch Ply')
hold off