在MatLab中使用for循环绘制图形 [英] Plotting graph using for loop in MatLab

查看:2084
本文介绍了在MatLab中使用for循环绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  x = linspace(0,2 *)我想使用for循环绘制一个简单的图,如下所示: PI,100); 
for i = 1:numel(x)
y = sin(x(i));
plot(x(i),y)
持有
结束



然而,我的身材并没有出现。为什么会这样?

解决方案

为什么会发生这种情况...



plot(x(i),y)绘制100个单点(每次迭代一次),默认情况下不显示。因此,情节看起来是空的。






解决方案1:向量化计算和直接绘图



我假设你打算画一条连续的线。在这种情况下,不需要for-loop,因为你可以直接在MATLAB中计算和绘制矢量。所以下面的代码可能是你想要的:

  x = linspace(0,2 * pi,100); 
y = sin(x);
plot(x,y);

请注意, y 也是一个向量(x(n)) x 为所有 n 。如果您想自己绘制点,请使用 LineSpec 当调用 plot >像这样 1

 积(X,Y, '*'); 

1)其他类型的点也是可能的,请参阅上面的链接文档。 / sup>






解决方案2:计算for循环内的值并随后绘图



如果要计算for循环内的值并在之后进行绘图:预先分配所需的变量(在本例中为 y

 ),计算for循环中的值,最后在计算后用一个单独的命令绘制它。

x = linspace(0,2 * pi,100);

y = zeros(size(x));
for i = 1:numel(x)
y(i)= sin(x(i));
end

plot(x,y);






解决方案3:动态更新plot while计算



如果您坚持在每次迭代中进行绘图,以前的 Solution 2 中的代码可以扩展如下:Create一个数字,给它添加一个'空'图并存储它的句柄。在for循环中计算这些值并将它们添加到 y -vector,如上所示。作为最后一步,您可以通过更改 XData YData 属性并调用的DrawNow 。请注意,在for循环中每次调用 plot 时都是不必要的昂贵,我不推荐它。

 %创建图并绘制
图;
ph = plot(0,0);
ax = gca;
set(ax,'XLim',[0,2 * pi]);
set(ax,'YLim',[ - 1,1]);

%计算并更新绘图
x = linspace(0,2 * pi,100);
y = zeros(size(x));
for i = 1:numel(x)
y(i)= sin(x(i));
set(ph,'XData',x(1:i));
set(ph,'YData',y(1:i));
drawnow;
end


I'm trying to plot a simple graph using for loop as shown below

x=linspace(0,2*pi,100);
for i=1:numel(x)
    y=sin(x(i));
    plot(x(i),y)
    hold on
end

However, nothing appear on my figure. Why is that?

解决方案

Why this happens...

With plot(x(i),y) you are plotting 100 single points (one in each iteration) and they are not shown by default. Therefore the plot looks empty.


Solution 1: Vectorized calculation and direct plot

I assume you meant to draw a continuous line. In that case no for-loop is needed because you can calculate and plot vectors directly in MATLAB. So the following code does probably what you want:

x = linspace(0,2*pi,100);
y = sin(x);
plot(x,y);

Note that y is a vector as well as x and that y(n) equals to sin(x(n)) for all n. If you want to plot the points itself, use LineSpec-syntax when calling plot like this1:

plot(x,y,'*');

1) Other types of points are possible as well, see the above linked documentation.


Solution 2: Calculate values within for-loop and plot afterwards

If you want to calculate the values within a for-loop and plot it afterwards: Pre-allocate the needed variable (in this case y), calculate the values within the for-loop and finally plot it with one single command after the calculation.

x = linspace(0,2*pi,100);

y = zeros(size(x));
for i = 1:numel(x)
    y(i) = sin(x(i));
end

plot(x,y);


Solution 3: Dynamically update plot while calculating

In case you insist on plotting within each iteration, the previous code from Solution 2 can be expanded as follows: Create a figure, add an 'empty' plot to it and store its handle. Within the for-loop calculate the values and add them to the y-vector as shown above. As a last step you can update the plot by changing its XData and YData properties and calling drawnow. Note that calling plot every time within the for-loop is unnecessarily expensive and I don't recommend it.

% create figure and plot
figure;
ph = plot(0,0);
ax = gca;
set(ax,'XLim',[0,2*pi]);
set(ax,'YLim',[-1,1]);

% calculate and update plot
x = linspace(0,2*pi,100);
y = zeros(size(x));
for i = 1:numel(x)
    y(i) = sin(x(i));
    set(ph,'XData',x(1:i));
    set(ph,'YData',y(1:i));
    drawnow;
end

这篇关于在MatLab中使用for循环绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆