我期待在MATLAB中创建一个简单的log(x)图表,其中模型显示沿着曲线随时间移动的点。在MATLAB中创建沿着图形移动的点
总体目标是将这些图中的两个并排放置,并将算法应用于它们。我真的不确定从哪里开始。
我是比较新的MATLAB编码,所以任何帮助将非常有用!
感谢 卢克
我期待在MATLAB中创建一个简单的log(x)图表,其中模型显示沿着曲线随时间移动的点。在MATLAB中创建沿着图形移动的点
总体目标是将这些图中的两个并排放置,并将算法应用于它们。我真的不确定从哪里开始。
我是比较新的MATLAB编码,所以任何帮助将非常有用!
感谢 卢克
这是@Jacob的解决方案的变化。相反,在重新绘制每一帧(clf
)的一切,我们简单地更新点的位置:
%# control animation speed
DELAY = 0.01;
numPoints = 600;
%# create data
x = linspace(0,10,numPoints);
y = log(x);
%# plot graph
figure('DoubleBuffer','on') %# no flickering
plot(x,y, 'LineWidth',2), grid on
xlabel('x'), ylabel('y'), title('y = log(x)')
%# create moving point + coords text
hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
%# infinite loop
i = 1; %# index
while true
%# update point & text
set(hLine, 'XData',x(i), 'YData',y(i))
set(hTxt, 'Position',[x(i) y(i)], ...
'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))
drawnow %# force refresh
%#pause(DELAY) %# slow down animation
i = rem(i+1,numPoints)+1; %# circular increment
if ~ishandle(hLine), break; end %# in case you close the figure
end
你可能想看看在COMET功能,这将使得曲线的动画。
例如(使用相同的数字作为@Jacob)
x = 1:100;
y = log(x);
comet(x,y)
如果你想显示点移动就行了(不是“拉”它),你只需
之前绘制的线x = 1:100;
y = log(x);
plot(x,y,'r')
hold on %# to keep the previous plot
comet(x,y,0) %# 0 hides the green tail
一个简单的解决办法是:
x = 1:100;
y = log(x);
DELAY = 0.05;
for i = 1:numel(x)
clf;
plot(x,y);
hold on;
plot(x(i),y(i),'r*');
pause(DELAY);
end
这不是一个好的解决方案,因为你每次都清理整个图形,所以速度,扩展空间等都不是最好的方式。 – mike 2015-05-31 09:22:51
沿着相同的路线作为@Jac稍微复杂的解决方案OB。在这里,我使用句柄图形和MATLAB电影对象添加一些优化来播放。
x=1:100;
y=log(x);
figure
plot(x,y);
hold on; % hold on so that the figure is not cleared
h=plot(x(1),y(1),'r*'); % plot the first point
DELAY=.05;
for i=1:length(x)
set(h,'xdata',x(i),'ydata',y(i)); % move the point using set
% to change the cooridinates.
M(i)=getframe(gcf);
pause(DELAY)
end
%% Play the movie back
% create figure and axes for playback
figure
hh=axes;
set(hh,'units','normalized','pos',[0 0 1 1]);
axis off
movie(M) % play the movie created in the first part
+1我不知道彗星。顺便说一句,如果彗星似乎没有做任何事情尝试多点(例如x = 1:.01:100;) – Azim 2011-02-10 18:01:59