Asked by catarina
on 16 May 2013

Hello,

I would like to plot a graph which shows the results of a for loop that derived a matrix Y(20,1001). I would like all the results of the iterations of Y to be plotted as one continuous line graph instead of all the iterations separately. However my code plots a graph with several lines of different colour, which I'm assuming its plotting the result of each iteration separately in the same graph. Hope I'm explaining my self clearly. My loop is underneath. Any tips would be appreciated. Thanks.

f=0:1000; w=2*pi*f; F=1;

%plate dimensions ad exciter location

Lx=0.286;

Lz=0.198;

x0=0.16;

z0=0.083;

%Plate data - Fibre board

h=3.25e-3 ; %thickness

E=1.4e9 ; %Young?s modulus

M=265*h; %Mass per unit area

mu=0.3; %Poisson?s ratio

eta=4e-2; %Damping Factor

p=265;

D=(E*(h^3))/(12*(1-mu)); %flexural rigidity

% calculate the eigenfrequencies and modes

i = 0;

for m = 1:5;

for n = 1:5;

if i<=19

i = i + 1;

wmn(i)=(sqrt(D/(p*h)))*((((n*pi)/Lx)^2)+(((m*pi)/Lz)^2)); %modal angular frequency

mode(i,:) = [m n];

end end end

%sort the eigenfrequencies and the mode numbers

[wmn, I] = sort(wmn); wmn = wmn';

modesort(1:i,:)=mode(I,:);

T = [wmn, modesort];

i=0;

for q = 1:length(T(:,1))

i=i+1; Y(i,:)=((4./(Lx.*Lz)).*(1./p))... .*(((sin((T(q,2).*pi.*x0)./Lx).*sin((T(q,3).*pi.*z0)./Lz))... .*(sin((T(q,2).*pi.*x0)./Lx).*sin((T(q,3).*pi.*z0)./Lz)))... ./((-w.^2)+(((T(q,1)).^2)*(1+(1j*(2*eta/(T(q,1))))))));

figure(1)

plot(f,20*log10(abs(Y)).') end

Answer by Jing
on 17 May 2013

Hi Catarina, Y is a 20*1001 matrix! When you use plot, there'll be 20 lines for each row. If Y should be one line, you should not make it a 20*2001 matrix at first, or reshape it into a vector:

Y=Y'; Y=Y(:);

And you don't need to put PLOT into for loop if you don't want the plot of each iteration.

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Jing (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75942-how-to-plot-results-from-for-loop-as-one-line-graph-instead-of-separate-results-of-each-iteration#comment_149189

There're many variables undefined in your code. And if the last PLOT function is outside of your for loop, it won't be the result of each iteration.

## catarina (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/75942-how-to-plot-results-from-for-loop-as-one-line-graph-instead-of-separate-results-of-each-iteration#comment_149193

Hi Jing, I've added all the necessary variables now. Cant figure this out.

Log in to comment.