[英]Plotting a graph with logarithic axis - 3 y-values for every x-value
我正在尝试在x轴上绘制变量x的值图。 但是,每个点都有三个值,即自然频率1,自然频率2和自然频率3。这是代码,它不起作用:
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
对于错误,我深表歉意。
您在行尾缺少右括号
loglog(i, freq(1:1), i, freq(2:1), i, freq(3:1)
另外,我看不出您的2:1
3:1
,...?的原因!
替换为
loglog(i, freq(1), i, freq(2), i, freq(3))
而且我认为您的绘图没有任何问题。
通常,我建议将freq的值存储在某个数组中并分别绘制。 这将加快脚本的速度,即
%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
您的问题是循环内有loglog
。
在循环之前,我输入:
i_vals = 1:1000:60e06;
freq = zeros(3, length(i_vals));
我将循环的顶部更改为:
for n=1:length(i_vals)
i = i_vals(n);
K = [i+7e06 i -6e06; i i+3e06 -3e06; -6e06 -3e06 10e06];
...
同时更改您的freq
分配:
freq(:, n)=sqrt(values)/(2*pi);
删除您的loglog
并将其放在循环之后/循环之外。
loglog(i_vals, freq')
做到所有这些给我这个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.