无法绘制数据

时间:2012-11-08 18:26:51

标签: matlab plot

我编写了以下matlab函数

function [t x] = MSD(xo, z, fo, c)
T = 1/fo; t = 0:T/10:10*T; fd = fo * sqrt(1-z*z);
wo = 2*pi*fo; w1 = wo * z; wd = 2*pi * fd;
x = xo * exp(-w1*t) .* cos(wd*t);
grid on;
plot(t,x,c); 

xlabel('time [s]'); 
ylabel('displacement');
s = sprintf('unforced Mass-Spring Damper [damped freq: %.3f Hz]', fd); title(s);
end

当我运行它时,命令窗口中出现以下错误:

MSD(.1, .7,.4, .2)
??? Error using ==> plot
Data must be a single matrix Y or a list of pairs X,Y

Error in ==> MSD at 13
plot(t,x,c);

2 个答案:

答案 0 :(得分:5)

Acorbe的回答是正确的,但它没有解释为什么

简而言之,您可以使用plot命令在向量x上绘制向量y,如下所示:

plot(x, y)

或在同一轴上绘制多个图形(向量x1y1,向量x2y2,依此类推......),例如:< / p>

plot(x1, y1, x2, y2, x3, y3, ...)

后一种语法要求输入向量的数量为偶数,因为它用绘制它们。

我认为您希望将c显示为与x(t)图形相交的线条。为此,您需要创建一个新函数y(t) = c,以便绘制它:

y = c * ones(size(t));  % # Or c * ones(1, length(t)) like Acorbe has shown

这只是创建一个t大小相同的向量,其中每个元素等于c。只有这样你才能在与x(t)相同的轴上绘制它,如下所示:

plot(t, x, t, y)

或缩写形式:

plot(t, x, t, c * ones(size(t)))


希望能够解决问题!

答案 1 :(得分:2)

因此你的电话应该是

 plot(t,x,t,c*ones(1,length(t)));

,简化,相当于:

 plot(t,x);
 hold on

 c_vect = c * ones(1, length(t));  //you need a vector (constant in this case) 
                                   //to be plotted against t!!
                                   //ones(1, length(t)) will give you
                                   //[1111 ... 1] (as many ones as 
                                   //the entries of t)
 plot(t,c_vect);