【发布时间】:2021-04-25 09:00:57
【问题描述】:
我想在 3d 中模拟粒子的轨迹,所以我使用以下代码创建了这个小模拟:
nP=100;
N=100;
z=rand(nP,N);
y=rand(nP,N); % Compute coordinates of particles for each iteration NS
z=rand(nP,N);
f = figure
view(3);
for i=N
h = animatedline('MaximumNumPoints', 1.e4,'color',rand(1,3));
for k = 1:length(x)
addpoints(h,x(i,k),y(i,k),z(i,k));
drawnow
end
end
hold on
hold off
numpoints = 500;
y2 = 3 +square(x+1);
f = figure
h = animatedline('Color','b','LineWidth',2);
h2 = animatedline('Color','r','LineWidth',2);
grid on;
%axis([0,12,-3,+6])
for k = 1:N
addpoints(h,x(k),y(k),z(k))
%addpoints(h2,x(k),y2(k))
drawnow
% Capture the plot as an image
frame = getframe(f);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if k == 1
imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf);
else
imwrite(imind,cm,'test.gif','gif','WriteMode','append');
end
end
我正在尝试将其保存为 gif 或 mp4。一切正常,但最终保存的是粒子在 2d 中的轨迹。有什么想法可以让我完成这项工作吗?
【问题讨论】:
-
第一行
z=...应该是x=...。为什么有两个数字?第二个不应该有view(3)吗?这似乎解决了问题
标签: matlab animation plot gif mp4