您的位置:首页 > 编程语言 > MATLAB

Lissajous曲线动画演示(Matlab实现)

2017-07-03 01:41 495 查看
在别人的博文里看到 Lissajous曲线的动态图, 感觉还挺帅,于是自己也想玩一下。



最终效果

Lissajous曲线

在数学上,Lissajous曲线 [1] (又称Lissajous图形或Bowditch曲线)是由如下参数方程构成的曲线族的图形

{x=Asin(at+φ)y=Bsin(bt)

纳撒尼尔·鲍迪奇(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


完整代码链接

参考

http://www.matrix67.com/blog/archives/6947

https://codepen.io/handsomeone/full/Kgmbqg

https://cn.mathworks.com/matlabcentral/fileexchange/28987-lissajous-curve

https://cn.mathworks.com/examples/matlab/community/22655-lissajous-curves

http://www.baike.com/wiki/%E5%88%A9%E8%90%A8%E5%A6%82%E5%9B%BE%E5%BD%A2

本文作者: 小路

本文链接: http://tinyland.site/posts/2017-07-02-matlab-implementation-for-lissajous-curve-animation-demo.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息