# I cant seem to figure out how to plot my final answer theta-x axis and A,B,C in the y direction. all in 3 separate plots.

9 views (last 30 days)
felipe alvarado on 30 Sep 2022
Edited: dpb on 30 Sep 2022
I have tried different methods of figuring out how to plot theta x axis with the values from A B and C in 3 different plots. I already had trouble figuring out how to even get different thetas for A, B, and C and this was the only way I could make it work. But now I cant seem to plot them. The way I have it I can plot all my different thetas with all my different values of A, B, and C but it plots them in different graphs instead of plotting them all in 1 graph. If anyone could help out. This is a code for my composite class where we use engineeering constants to make a reduced compliance and stiffness matrix and then turn them from local to global and plot some data.
function [S,Q]= IsotropicComplianceStiffness(E1,E2,v12,G12)
% Inputs: E (Youngs modulus, GPa), v (Poisson's ratio),G Shear Moduli
E1=40*10^3;
E2=8*10^3;
v12=0.25;
G12=4*10^3;
sigxx=5 ; %MPa
sigyy=4 ;%MPa
txy=2 ; %MPa
% Outputs: C-stiffness matrix, S-compliance matrix
%Initialize the C and S matrix, by setting all the matrix elements to zero
S=zeros(3,3);
Q=zeros(3,3);
% Calculate terms of the compliance matrix.
% Remember: Compliance matrix is symmetric.
S(1,1)=1/E1;
S(2,2)=1/E2;
S(2,1)=-v12/E1;
S(3,3)=1/G12;
%terms of symmetry
S(1,2)=S(2,1);
% Calculate terms of the Stiffness matrix.
% Remember: Stiffness matrix is symmetric and inverse of C.
Q=inv(S);
for i=0:15:90 %making theta from [0-90 increments of 15]
theta=i
m=cos(theta);
n=sin(theta);
%Transformation Matrix
T=[m^2 n^2 2*m*n; n^2 m^2 -2*m*n; -m*n m*n ((m^2)-(n^2))];
%Inverse of Transformation Matrix
Tinv=inv(T);
R=[1 0 0; 0 1 0; 0 0 2];
Rinv=inv(R);
Sbar=R*Tinv*Rinv*S*T; % Transformed Reduced Compliance Matrix
Qbar=Tinv*Q*R*T*Rinv; % Transformed Reduced Stiffness Matrix
%Global Stress and strain calculation
Stress_G=[sigxx;sigyy;txy];% Given stress in global
Strain_G= S*Stress_G; % find strain in global(compliance matrix*stressglobal)
%Local Stress and Strain calcualtion (using matrix transformation)
StressL=T*Stress_G %stressL=stresslocal ( stress1,stress2,shear stress(6))
StrainL=T*Strain_G; %strainL=strainlocal (strain1,strain2, sigma(shear strain(6))
% Ratios to be ploted with angles for problem 2.3-- we use
A=StressL(1,1)/sigxx %Stresslocal(1 direction)/stress global (1direction)
%we use stressL(1,1) becuse thats where our stressL is on the 1 direction)
B=StressL(2,1)/sigyy %Stresslocal(2 direction)/stress global (2direction)
C=StressL(3,1)/txy %Shear stress local/shear stress global
figure
plot1=plot(theta,A)
dpb on 30 Sep 2022
Your code isn't complete through the for...end loop, but
...
figure
plot1=plot(theta,A)
will create a new figure and plot the one line. Not much else it can do with only theta, A in the argument.
To plot all three on the same plot, take the call to figure out of the loop and then use hold on to add additional data to the same plot.
...
hL=plot(theta,[A B C]); % I use hL as generic lines handle
NB: however, the above will only put a single set of points on the plot at each theta value, it will not draw a line between the points. And, as you've constructed the above, you're overwriting A,B,C each pass through the loop; you would need to allocate room for each as an array and store into that array with an index each pass through the loop to save them all.
That done, then also move the plot call out of the loop until after it's done and plot them all at once.

dpb on 30 Sep 2022
Edited: dpb on 30 Sep 2022
E1=40*10^3;
E2=8*10^3;
v12=0.25;
G12=4*10^3;
IsotropicComplianceStiffness(E1,E2,v12,G12);
function [S,Q]= IsotropicComplianceStiffness(E1,E2,v12,G12)
% Inputs: E (Youngs modulus, GPa), v (Poisson's ratio),G Shear Moduli
sigxx=5 ; %MPa
sigyy=4 ;%MPa
txy=2 ; %MPa
% Outputs: C-stiffness matrix, S-compliance matrix
%Initialize the C and S matrix, by setting all the matrix elements to zero
S=zeros(3,3);
Q=zeros(3,3);
% Calculate terms of the compliance matrix.
% Remember: Compliance matrix is symmetric.
S(1,1)=1/E1;
S(2,2)=1/E2;
S(2,1)=-v12/E1;
S(3,3)=1/G12;
%terms of symmetry
S(1,2)=S(2,1);
% Calculate terms of the Stiffness matrix.
% Remember: Stiffness matrix is symmetric and inverse of C.
Q=inv(S);
THETA=[0:15:90].'; % define desired angles
A=zeros(size(THETA)); B=A; C=A; % preallocate outputs
for i=1:numel(THETA) % iterate over the angles
theta=THETA(i); % set iterative theta value
m=cos(theta);
n=sin(theta);
%Transformation Matrix
T=[m^2 n^2 2*m*n; n^2 m^2 -2*m*n; -m*n m*n ((m^2)-(n^2))];
%Inverse of Transformation Matrix
Tinv=inv(T);
R=[1 0 0; 0 1 0; 0 0 2];
Rinv=inv(R);
Sbar=R*Tinv*Rinv*S*T; % Transformed Reduced Compliance Matrix
Qbar=Tinv*Q*R*T*Rinv; % Transformed Reduced Stiffness Matrix
%Global Stress and strain calculation
Stress_G=[sigxx;sigyy;txy];% Given stress in global
Strain_G= S*Stress_G; % find strain in global(compliance matrix*stressglobal)
%Local Stress and Strain calcualtion (using matrix transformation)
StressL=T*Stress_G; %stressL=stresslocal ( stress1,stress2,shear stress(6))
StrainL=T*Strain_G; %strainL=strainlocal (strain1,strain2, sigma(shear strain(6))
% Ratios to be ploted with angles for problem 2.3-- we use
A(i)=StressL(1,1)/sigxx; %Stresslocal(1 direction)/stress global (1direction)
%we use stressL(1,1) becuse thats where our stressL is on the 1 direction)
B(i)=StressL(2,1)/sigyy; %Stresslocal(2 direction)/stress global (2direction)
C(i)=StressL(3,1)/txy; %Shear stress local/shear stress global
end
hL=plot(THETA,[A B C],'x-');
legend('A','B','C','location','eastoutside')
end
Is one way to fix --
NOTA BENE: in the original the argument variables to the function were set as fixed values inside the function; there's no point in having them be arguments in that case. I made the minimal fix to correct that and then called the function with those values.
The function would be more useful generically if it also passed in the theta vector so could be changed without changing the code itself.
As well the return variables S,Q are not saved as arrays, either; I only fixed those needed to create the plots requested; the same fixup needs be done for those as well.
If returned the other stresses as well, then the function wouldn't need to do any plotting (be better factorization if it didn't) but would pass back its computations and let the caller decide what and how to plot.
Those cleanups and fixes are left as "exercise for student"

### Categories

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

R2020b

### Community Treasure Hunt

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

Start Hunting!