Lissajous曲線動畫演示(Matlab實現)

在別人的博文里看到 Lissajous曲線的動態圖, 感覺還挺帥,于是自己也想玩一下。

最終效果

Lissajous曲線

在數學上,Lissajous曲線(又稱Lissajous圖形或Bowditch曲線)是由如下參數方程構成的曲線族的圖形 [1]

![參數方程][Equation]
[Equation]: http://latex.codecogs.com/svg.latex?\begin{cases}x=A\sin(at+\varphi)\y=B\sin(bt)\end{cases}

納撒尼爾·鮑迪奇(Nathaniel Bowditch)在1815年首先研究了這一曲線族,之后在1857年由朱爾斯·安東尼·利薩如(Jules Antoine Lissajous)進行了更詳細的研究。

在物理上,Lissajous曲線可以看作是一個質點同時在X軸和Y軸方向上做簡諧運動形成的運動軌跡(以我僅剩的一點物理知識來說,這大概是叫"運動的合成")。
當這兩個相互垂直方向上的簡諧振動的頻率為任意值時,那么它們合成的運動可能會比較復雜,軌跡是不穩定的,示意圖如下。(關于穩定,我的理解大概就是呈周期性吧)

一個示意圖

而如果這兩個振動的頻率成簡單的整數比,就能合成一個規則的、穩定的閉合曲線,這就是Lissajous曲線(示意圖如下)。

另一個示意圖

具體實現

  • 環境: Matlab R2017a

Animated Line 實現動畫效果

首先用animatedline創建初始動畫線條對象 [2] ,接著在循環中動態地向線條中添加點,并使用 drawnow 在屏幕上顯示該新添加的點,然后用getframe捕獲的當前圖像。

%%
% Create a new figure window,
% Background color is white
% MenuBar is hidden
% Image size is 480*480
figure('Color','w','MenuBar','none','InnerPosition',[0 0 480 480]);
co = [65/255, 131/255, 196/255]; % specify the color
% Create animated line object
headMarker = animatedline('LineStyle','none','Marker','o','Color',co,'MarkerFaceColor',co);
body = animatedline('LineStyle','-','LineWidth',1.3,'Color',co); % trajectory
axis([-1,1,-1,1]); axis off; % hide axis

%% 
% Parameters
a = 13;
b = 18;
phi = pi * 0;

% Parametric Equation of Lissajous Curve
t = linspace(0,2*pi,1000); % generate 1000 linearly equally spaced points
x = sin(a*t + phi);
y = sin(b*t);

%%
% Display
for idx = 1:length(t)
    addpoints(headMarker,x(idx),y(idx));
    addpoints(body,x(idx),y(idx));
    drawnow 
    % Capture the current plot
    frame = getframe; % snapshot of the current axes
    im{idx} = frame2im(frame); % write image data to `im`
    % Remove previous head marker except for the last one
    if idx~=length(t)
        clearpoints(headMarker);
    end
end

寫入GIF動圖

getframe捕獲的每一個圖像,寫入 GIF 動畫文件 [3]

%%
% Save images into a GIF file. 
% Because three-dimensional data is not supported for GIF files, 
% call rgb2ind to convert the RGB data in the image to an indexed image A with a colormap map. 
filename = 'Lissajous.gif'; % specify the output file name
for idx = 1:length(t)
    [A,map] = rgb2ind(im{idx},256);
    if idx == 1
        % Set DelayTime = 0 to display next images as fast as your hardware allows
        imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0);
    else
        % To append multiple images to the first image
        imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0);
    end
end

完整代碼鏈接

參考

  1. http://www.matrix67.com/blog/archives/6947
  2. https://codepen.io/handsomeone/full/Kgmbqg
  3. https://cn.mathworks.com/matlabcentral/fileexchange/28987-lissajous-curve
  4. https://cn.mathworks.com/examples/matlab/community/22655-lissajous-curves
  5. http://www.baike.com/wiki/%E5%88%A9%E8%90%A8%E5%A6%82%E5%9B%BE%E5%BD%A2
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 天還很亮 夜晚卻快來臨 我害怕孤寂 也害怕星星的眼睛 那些眼睛都是你 讓我無法躲避 在你的眼睛里 我失去了我自己 ...
    潛水的鳥閱讀 222評論 0 3
  • 長大,是個名詞,它是骨骼的伸張,是面容的成熟,更是處事的不驚,待人對事的的從容和執著。 隨著年齡的增長,我們其實丟...
    er新心弍閱讀 454評論 0 1
  • 這是“心靈對話寫作”小組的第四篇文章 今天到現在提筆想寫點什么,除了霧霾似乎想不到其它。戶外的活動,讓我感到眼睛和...
    墨芷軒閱讀 252評論 3 2
  • 首先仰臥到墊子上,雙臂體側平舉,雙手掌心朝下,放到地墊上,繼續找站立山式的感覺,把腳后跟蹬出去
    隨筆s閱讀 1,069評論 0 0