How can i get continuous plots with a video pause?

7 views (last 30 days)
Andi
Andi on 25 Jan 2022
Edited: Vaibhav on 5 Oct 2023
Here is my initial script that works perfectly for my probem:
clear all
clc
xs=8;
v=3;
L=20;
ta=0.2;
t=1; %
n=1:1:40;
x=0:0.1:20;
omga = zeros(1,length(n)) ;
F = zeros(1,length(n)) ;
u = zeros(length(n),length(x)) ;
for j=1:length(n)
F(j)=exp(-((n(j)*pi*v*ta/L).^2)/4);
u(j,:)=sin(n(j)*pi*xs/L)*F(j)*sin(n(j)*pi*x/L)*cos(n(j)*pi*v/L*t(k)); % no loop needed
end
us = sum(u) ;
plot(x,us)
xlabel ('distance')
ylabel('Displacement')
Now I want to make 3 plots for different values of t. BUt it shows only one plot: how can i modify my script to get all the three plots in a short video.
clear all
clc
xs=8;
v=3;
L=20;
ta=0.2;
t=1:1:3; % this line is changed
n=1:1:40;
x=0:0.1:20;
omga = zeros(1,length(n)) ;
F = zeros(1,length(n)) ;
u = zeros(length(n),length(x)) ;
for j=1:length(n)
F(j)=exp(-((n(j)*pi*v*ta/L).^2)/4);
for k=1:length(t) % additional for loop
u(j,:)=sin(n(j)*pi*xs/L)*F(j)*sin(n(j)*pi*x/L)*cos(n(j)*pi*v/L*t(k)); % no loop needed
end
end
us = sum(u) ;
plot(x,us)
xlabel ('distance')
ylabel('Displacement')

Answers (1)

Vaibhav
Vaibhav on 5 Oct 2023
Edited: Vaibhav on 5 Oct 2023
Hi Andi
I understand that you would like to have 3 plots in a short video.
One possible solution is to make use of the "drawnow" and "pause" functionalities. The "drawnow" command ensures immediate updating and display of the plot, while the "pause(x)" command introduces a short delay of 'x' seconds between each plot, creating a video effect. Adjust the duration according to the requirements.
Below is a code snippet demonstrating the application of "drawnow" and "pause" for generating three plots in a single video.
clear all
clc
% Define parameters
xs = 8;
v = 3;
L = 20;
ta = 0.2;
t = 1:1:3;
n = 1:1:40;
x = 0:0.1:20;
% Initialize omega, F, and displacement array
omga = zeros(1, length(n));
F = zeros(1, length(n));
u = zeros(length(n), length(x));
figure % Create a new figure for the plots
% Loop over each time value
for k = 1:length(t)
% Loop over each term in the series
for j = 1:length(n)
% Calculate F value for each term
F(j) = exp(-((n(j) * pi * v * ta / L) ^ 2) / 4);
% Calculate displacement u for each term and distance value
u(j, :) = sin(n(j) * pi * xs / L) * F(j) * sin(n(j) * pi * x / L) * cos(n(j) * pi * v / L * t(k));
end
us = sum(u); % Sum up displacements for each term to get total displacement
% Plot the displacement
plot(x, us)
xlabel('distance')
ylabel('Displacement')
title(['t = ', num2str(t(k))])
drawnow % Force the plot to update and display
pause(0.5) % Pause for a short duration to create a video effect
end
You can refer to below documentation to know more about "drawnow" feature:
You can refer to below documentation to know more about “pause” feature:
Hope this helps!
Regards,
Vaibhav

Tags

Products

Community Treasure Hunt

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

Start Hunting!