Dorian Kartalovski
Dorian Kartalovski

Reputation: 213

Issue with plotting in a for loop on matlab

This is my for loop, which is not plotting correctly:

for w=1:1:1000
    s(w)=w*1j;
    hX(w)=(alphaX22_1+betaX22_1*s(w))/(s(w)^2+2*d_X1*wn_X1*s(w)+wn_X1^2)+...
    (alphaX22_2+betaX22_2*s(w))/(s(w)^2+2*d_X2*wn_X2*s(w)+wn_X2^2);

    mag_hX(w)=sqrt(real(hX(w))^2+imag(hX(w))^2);
    %arg_hX(w)=atan(imag(hX(w))/real(hX(w)));    
end

figure(2)
plot(w,mag_hX(w),'+')

But I only get the last value on the plot. Also when I do 'size s' or 'size hX' I get 1 1, but I can access s(350) and hX(350).

Just so you can run the code, I am adding the numerical values of my variables.

% Natural Frequencies and Damping Ratios
wn_X1=354.3; d_X1=0.038;
wn_X2=467.6; d_X2=0.049;

alphaX22_1 = 0.0043
betaX22_1 = -1.2528e-05
alphaX22_2 = 0.0235
betaX22_2 = 4.4012e-06

Upvotes: 0

Views: 42

Answers (1)

M. Smith
M. Smith

Reputation: 389

I would take a closer look at here or here. I believe you need to put your plotting statement in the for loop. Right now you're only plotting the last value. You should open the figure before your loop.

Upvotes: 1

Related Questions