I am trying to understand how Movie in Matlab works, I have never used it before. I have a code for the heat equation that finds temperature for the amount of segments I decide to put on the length of the object (rod). I am able to plot a normal plot that shows all the values at different conditions however. I am in left field trying to understand how to change it into a movie.
A=0.1; %Diffusivity or conductivity (switching for variable K)
L=1; %Length of rod
dt= 10^(-4); %value of timestep
X=100; %Wants to Discretize rod into 100 separate sections
t= 10; % 50 timesteps will be taken (dt)
dx=L/X; %delta X needed for the heat equation for this case Pi/100
x1=linspace(0, L, X); %Take 100 equally spaced sections between 0 and L
y1=linspace(0, t, 51);
T1= zeros(X,t); %set a X x t matrix of zeros
%Set boundary and initial conditions
%T0=0; Ends of rod temperature are 0
%TL=0;
%Create a for loop to continuously solve the heat equation until time is up
for i= 2:X-1 %X-1 because we already know initial point
for j=1:50 % used to update heat equation each step
T1(:,1)= sin((pi*x1(i))/L);%start solving the heat equation at second
% value in the matrix in column 1 (skip
% 1st because we have initial condition)
T1(1,:) = 0; %Gives the first column = 0
T1(100,:) = 0; %Gives last column = 0 just as initial conditions state
%Heat Equation for T^n+1 (i)
T1(i,j+1)= A*dt*((T1(i+1,j)-2*T1(i,j)+T1(i-1,j)/dx^2))+ T1(i,j);
end
end
figure(1)
plot(x1,T1);
Any advise would be great. I have looked at several different examples from other peoples post, however they all do not seem to access a movie the same.
movie
requires that you have a series of RGB images in memory, and they are stored in an array of structures that is designed for the movie
function.
What you can do is plot one signal at a time with hold on
. Additionally, you can use getframe
to actually capture each plot into an image to get the required format for movie
, concatenate it all into a structure array and finally play the movie.
I see you're plotting all of your signals simultaneously with one column per signal. To make this easy, make another for
loop and cycle through each signal separately - one per column and plot these one at a time.
A=0.1; %Diffusivity or conductivity (switching for variable K)
L=1; %Length of rod
dt= 10^(-4); %value of timestep
X=100; %Wants to Discretize rod into 100 separate sections
t= 10; % 50 timesteps will be taken (dt)
dx=L/X; %delta X needed for the heat equation for this case Pi/100
x1=linspace(0, L, X); %Take 100 equally spaced sections between 0 and L
y1=linspace(0, t, 51);
T1= zeros(X,t); %set a X x t matrix of zeros
%Set boundary and initial conditions
%T0=0; Ends of rod temperature are 0
%TL=0;
%Create a for loop to continuously solve the heat equation until time is up
for i= 2:X-1 %X-1 because we already know initial point
for j=1:50 % used to update heat equation each step
T1(:,1)= sin((pi*x1(i))/L);%start solving the heat equation at second
% value in the matrix in column 1 (skip
% 1st because we have initial condition)
T1(1,:) = 0; %Gives the first column = 0
T1(100,:) = 0; %Gives last column = 0 just as initial conditions state
%Heat Equation for T^n+1 (i)
T1(i,j+1)= A*dt*((T1(i+1,j)-2*T1(i,j)+T1(i-1,j)/dx^2))+ T1(i,j);
end
end
figure(1)
hold on; % New
% New - To store the frames for the movie
frames = repmat(struct('cdata', [], 'colormap', []), 50, 1);
for j = 2 : 51
plot(x1,T1(:,j));
frames(j - 1) = getframe(gcf);
end
close all; % Close the figure
% Play the movie
figure;
movie(frames, 1, 5); % Play the movie once at 5 frames per second
The new code will store the required plot as images - you'll have to see the figure plotting though in order for the frames to be grabbed. After that, we close the figure and play the movie at 5 FPS. After this point, you just have to run the last movie
command to play the movie as many times as you like. The second parameter specifies how many times you want the movie to repeat and the third parameter specifies the frames per second of the movie.