2011-02-10 5 views
3

私は、モデルが時間とともに曲線に沿って移動する点を示すMATLAB内の単純なlog(x)グラフを作成したいと考えています。MATLABでグラフに沿って移動する点を作成する

全体の目的は、これらのグラフの2つを互いに並べて、それらにアルゴリズムを適用することです。私はここでどこから始めたらいいのか分かりません。

私はMATLABコーディングで比較的新しいので、どんな助けも非常に便利です!

おかげ ルーク

答えて

7

ここには@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 

enter image description here

2

あなたは曲線のアニメーションを行います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 
+0

+1することができ、私は彗星については知りませんでした。ところで、彗星がもっと多くの点(例えばx = 1:.01:100;)を試していないと思われる場合 – Azim

2

簡単な解決策は、次のとおり

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 
+0

文字通りすべてを再描画するたびに図全体をクリアするので、良い解決策ではありません。スピード、拡張の余地などは、このように最善ではありません。 – mike

0

@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 
0

をソリューションは、このよう

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

ベンはもっと説明してください。 – Christos

+0

これは、ジョナスの答えでまだカバーされていない新しいものを追加しません。 – gnovice

関連する問題