Reputation: 61
I am trying to plot a graph of value value of a variable x on the x-axis; however, there are three values for each point, that is to say, natural frequency 1, natural frequency 2 and natural frequency 3. This is the code and it is not working:
M = [3 0 0; 0 2 0; 0 0 0.5]
% disp('Mass matrix [M]');
% disp(M);
for i=1:1000:60e06
K = [i+7e06 i -6e06; i i+3e06 -3e06; -6e06 -3e06 10e06];
% disp(K)
[V,L]=eig(K,M); % eig is a standard Matlab function
values=diag(L); % diag gets the values from the leading diagonal
[values,I]=sort(values); % sort into ascending order of frequency
V(:,I)=V; % now sort the mode shape vectors
freq=sqrt(values)/(2*pi); % this divides all elements by 2 pi
% disp('Eigenvalues [s^(-2)]');
% disp(values'); % the quote mark after values prints the column vector as a row
% disp('Frequencies [Hz]');
% disp(freq');
% disp('Mode shape vectors in the columns of matrix [V]');
% disp(V);
loglog(i, freq(1:1), i, freq(2:1), i, freq(3:1)
end
Apologies for the mistakes, I am a beginner.
Upvotes: 0
Views: 92
Reputation: 22529
Your problem is that you have the loglog
inside the loop.
Before the loop, I put:
i_vals = 1:1000:60e06;
freq = zeros(3, length(i_vals));
I change the top of the loop to:
for n=1:length(i_vals)
i = i_vals(n);
K = [i+7e06 i -6e06; i i+3e06 -3e06; -6e06 -3e06 10e06];
...
Also change your freq
assignment:
freq(:, n)=sqrt(values)/(2*pi);
Delete your loglog
and put this after/outside your loop.
loglog(i_vals, freq')
Doing all of that gives me this:
Upvotes: 2
Reputation: 3330
You are missing a closing bracket in the end of your line
loglog(i, freq(1:1), i, freq(2:1), i, freq(3:1)
Also, I don't see the reason for your 2:1
, 3:1
, ...?!
Replace it by
loglog(i, freq(1), i, freq(2), i, freq(3))
and I don't see anything wrong with your plotting.
In general I would suggest to store the values of freq in some array and plot it separately. This will speed the script up a lot, i.e.
%preallocate freq, the number of iterations in the loop is important here
freq = zeros(3,length(1:1e3:60e6))
for i = ...
%your loop goes here
%remove freq = ...
%remove loglog( ...
freq(:,i) = sqrt(values)/(2*pi); % this divides all elements by 2 pi
end
loglog(1:1e3:60e6, freq(1,:)) %plot first curve
loglog(1:1e3:60e6, freq(2,:)) %plot second curve
loglog(1:1e3:60e6, freq(3,:)) %plot third curve
Upvotes: 2