i wanna animate 2 subplots in the same time and each one for different numbers.

2 次查看(过去 30 天)
in animation Section
i wanna first subplot to animate until first value of variable Nt_values (541) and the second value of variable Nt_values (361) and the animation of the 2 subplots happen in the same time
Defining Variables
clc,clear;
v = 0.000217; % Kinematic Viscosity
h = 0.04; dy = 0.001; % Y Dimension
Ny = length(linspace(h,0,round(h/dy))) ;
dt_values = [0.002, 0.003]; % Time Dimension
Nt_values = [541 , 361];
% Initial Condition
ic = 0;
% Boundary Conditions
BC_Top = 0;
BC_Bottom = 40;
Solution = cell(1,length(dt_values)) ;
for dt_index = 1:length(dt_values)
dt = dt_values(dt_index);
Nt = Nt_values(dt_index);
% Diffusion Number
d = (v * dt) / (dy^2);
Defining Domain
U = zeros(Ny + 1, Nt);
Initial Condition
U(:, 1) = ic;
Boundary Conditions
U(1, :) = BC_Top;
U(end, :) = BC_Bottom;
Solution
for n = 1:Nt - 1
if n == 1
for j = 2:Ny
U(j, n + 1) = U(j, n) + d * (U(j + 1, n) - 2 * U(j, n) + U(j - 1, n));
end
else
for j = 2:Ny
U(j, n + 1) = (U(j, n - 1) + 2 * d * (U(j + 1, n) - U(j, n - 1) + U(j - 1, n))) / (1 + 2 * d);
end
end
end
Solution{dt_index} = U;
Postprocessing
subplot(length(dt_values), 1,dt_index);
y = linspace(h, 0, round(h / dy) + 1);
Marker = ['o', '+', '*', 'x', 'd', 's', 'h'];
t_plot = round(linspace(1,Nt,5));
% Initialize legend array >> Just to make Compiling Faster
legend_Values = strings(1, length(t_plot));
for t_plot_i = 1:length(t_plot)
i = t_plot(t_plot_i);
title(['Velocity Profile Vs. Time at Δt = ', num2str(dt), ' Sec']);
xlabel('Velocity (m/s)');
ylabel('Height (m)');
plot(U(:, i ), y, 'LineWidth', 1, "Marker", Marker(t_plot_i));
hold on;
% Put legend Values(String Type) in a cell array
legend_Values{t_plot_i} = ['at time = ' num2str((i-1) * dt)];
end
legend(legend_Values);
end
Animation
figure ;
for an = 1:Nt
subplot(2,1,1);
plot(U(:, an), y, 'LineWidth', 2);
title(['Velocity Profile Vs. Time at Δt = ', num2str(dt_values(1)), ' Sec']);
subtitle(['Velocity Profile at Time =', num2str(dt_values(1)*an), ' Sec']);
xlabel('Velocity (m/s)');
ylabel('Height (m)');
drawnow;
subplot(2,1,2);
plot(U(:, an), y, 'LineWidth', 2);
title(['Velocity Profile Vs. Time at Δt = ', num2str(dt_values(2)), ' Sec']);
subtitle(['Velocity Profile at Time =', num2str(dt_values(2)*an), ' Sec']);
xlabel('Velocity (m/s)');
ylabel('Height (m)');
drawnow;
end

采纳的回答

Voss
Voss 2023-12-7
% Animation
figure
NN = numel(Solution);
ax = gobjects(1,NN);
hh = gobjects(1,NN);
for ii = 1:NN
ax(ii) = subplot(NN,1,ii);
hh(ii) = line(ax(ii), 'LineWidth', 2, 'Color', [0 0.447 0.741]);
title(ax(ii),['Velocity Profile Vs. Time at Δt = ', num2str(dt_values(ii)), ' Sec']);
xlabel(ax(ii),'Velocity (m/s)');
ylabel(ax(ii),'Height (m)');
end
for an = 1:max(Nt_values)
for ii = 1:NN
if an <= Nt_values(ii)
set(hh(ii),'XData',Solution{ii}(:,an),'YData',y);
subtitle(ax(ii),['Velocity Profile at Time =', num2str(dt_values(ii)*an), ' Sec']);
drawnow;
end
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by