如何解决如何在 MATLAB 中从一系列矩阵
我有使用 Wolff 算法在 MATLAB 中模拟 XY 模型的代码,我想实现一个 pcolor/color 映射,以根据它们在整个系统中的角度来演示每个旋转。但我希望它能够随着角度的变化而变化。
知道怎么做吗?
这是我希望它看起来如何https://i.stack.imgur.com/aSp7s.png
解决方法
如果将晶格的每个快照保存在一个元胞数组A{t}中,可以使用以下函数查看并保存为视频(如果fileName不为空,该函数保存一个mp4视频)。
另一种选择是调整函数 view_lattice
来运行您的模拟(老实说,我不建议这样做,因为性能问题)。我将标记您应该编辑的位置以进行“实时”模拟
这至少是 MATLAB R2019b(虽然它可能兼容早期版本,但不能保证)。
文件view_lattice.m
function view_lattice(A,fileName)
% for a 'live' simulation,you will have to remove A from the input
% parameters and add the ones you need for the XY Wolff algorithm,% which will be used to calculate each configuration A in the time loop below
% you will also need to remove the assert statements for 'live' simulation
%
% otherwise,you save snapshots from your simulation
% and use this function as is
%
% A -> A{k}[m,n] snapshot k containing the angles of spins in lattice site at row m and col n
% fileName -> if contains string,then records a video with the snapshots and name it with this string
assert(iscell(A) && all(cellfun(@(a)isnumeric(a) && ismatrix(a),A)),'A must be cell of numeric matrices');
assert(ischar(fileName),'fileName must be either an empty char or contain a file name');
recordVideo = ~isempty(fileName);
if recordVideo
vw = setup_video(fileName);
else
vw = [];
end
% setting some default axis properties to speed-up plotting
set(0,'DefaultAxesPlotBoxAspectRatio',[1 1 1],'DefaultAxesDataAspectRatioMode','manual','DefaultAxesDataAspectRatio',[1,1,1],'DefaultAxesNextPlot','replace');
fh = figure;
ax=axes;
for t = 1:numel(A) % for 'live' simulation,this loop should be the time loop
% here you calculate the new configuration A
% and call the function below with A instead of A{t}
vw = record_frame(vw,fh,ax,A{t},t,recordVideo);
end
% any video to close?
if recordVideo
vw.close();
end
end
function vw = record_frame(vw,A,recordVideo)
imagesc(ax,A);
title(ax,sprintf('snapshot %g',t)); % if you want,y
axis(ax,'square');
daspect(ax,1]);
pause(0.01);
if recordVideo
vframe = getframe(fh);
vw.writeVideo(vframe);
end
end
function vw = setup_video(fileName)
vid_id = num2str(rand,'%.16g');
vid_id = vid_id(3:6);
vid_id = [fileName,'_',vid_id];
% Initialize video
vw = VideoWriter([vid_id,'.mp4'],'MPEG-4'); %open video file
vw.Quality = 100;
vw.FrameRate = 16;
vw.open();
end
测试脚本:test.m
clearvars
close all
A = cell(1,30);
for t = 1:numel(A)
% creating a sequence of random snapshots only for illustration
A{t} = rand(20,20);
end
% viewing the animation and saving it as a video with name test
view_lattice(A,'test');
输出
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。