Plotting Results of a Matrix from a For Loop Help

2 visualizzazioni (ultimi 30 giorni)
Hey guys, this is a snippet of my code I can't seem to get to graph. S is a 3x3 matrix and I need to plot points (1,1), (2,2) and (3,3) of the transformed 3x3 Sxy matrix for values of theta from 0:90. How to I get the figures to work and not just be blank? I would very much appreciate help with this. So far I've tried using the 'hold on' and moving the plot functions inside the for loop.
I would do this without the for loop but the matrix S does not change with each iteration so without the for loop, the inner matrix indices do not match.
for theta=0:90
% Angles
c=cosd(theta);
s=sind(theta);
% Transformation Matrix
T=[c.^2 s.^2 2.*c.*s; s.^2 c.^2 -2.*c.*s; -c.*s c.*s c.^2-s.^2];
Tinv=[c.^2 s.^2 -2.*c.*s; s.^2 c.^2 2.*c.*s; c.*s -c.*s c.^2-s.^2];
% Coordinate Transformations
Sxy=Tinv*S*T;
Exx=1/Sxy(1,1);
Eyy=1/Sxy(2,2);
Gxy=(1/Sxy(3,3))/2;
end
figure(1)
plot(theta,Exx)
xlabel('Theta in Degrees')
ylabel('Exx')
figure(2)
plot(theta,Eyy)
xlabel('Theta in Degrees')
ylabel('Eyy')
figure(3)
plot(theta,Gxy)
xlabel('Theta in Degrees')
ylabel('Gxy')

Risposta accettata

Luke Horstman
Luke Horstman il 6 Mar 2019
Your Exx Eyy and Gxy variables are only saving the last value of the loop. So, you are only plotting a single point. You need to index your variables in the loop so those are arrays instead of single values.
%initialize variables
theta = 0:1:90;
Exx = zeros(1,length(theta));
Eyy = zeros(1,length(theta));
Gxy = zeros(1,length(theta));
%Loop
for 1=1:length(theta)
% Angles
c=cosd(theta(i));
s=sind(theta(i));
% Transformation Matrix
T=[c.^2 s.^2 2.*c.*s; s.^2 c.^2 -2.*c.*s; -c.*s c.*s c.^2-s.^2];
Tinv=[c.^2 s.^2 -2.*c.*s; s.^2 c.^2 2.*c.*s; c.*s -c.*s c.^2-s.^2];
% Coordinate Transformations
Sxy=Tinv*S*T;
Exx(i)=1/Sxy(1,1);
Eyy(i)=1/Sxy(2,2);
Gxy(i)=(1/Sxy(3,3))/2;
end
I haven't tested the code, but I'm pretty sure it should work.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by