Creating a point moving along a graph in MATLAB

2019-01-25 23:26发布

问题:

I am looking to create a simple log(x) graph within MATLAB in which the model shows the point moving along the curve with time.

The overall aim is to have two of these graphs alongside one another and to apply an algorithm to them. I am really unsure where to start here.

I am relatively new at MATLAB coding so any help would be very useful!

Thanks Luke

回答1:

Here is a variation on @Jacob's solution. Instead of redrawing everything at each frame (clf) we simply update the point's location:

%# 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



回答2:

A simple solution is:

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


回答3:

You may want to have a look at the COMET function, which will make an animation of the curve.

For example (using the same numbers as @Jacob)

x = 1:100;
y = log(x);
comet(x,y)

If you want to show the point moving on the line (not 'drawing' it), you simply plot the line before

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


回答4:

a little more complex solution along the same lines as @Jacob. Here I add some optimization using handle graphics and a MATLAB movie object for playback.

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


回答5:

solution can be this way

x = .01:.01:3;
comet(x,log(x))