%%%%%%%%%%   HW 15, Template for making a movie out of the simulation.

% Set up the spatial and temporary grids and all related quantities.

    ............


% Set up parameters for, and then open, the movie file:
set(gca, 'nextplot','replacechildren');  % this may be needed if you want to keep the same axes
set(gcf,'Renderer','zbuffer');           % this is probably redundant, but doesn't seem to hurt

%# create AVI object
nFrames = 20;     % an upper bound for the number of frames in the movie
                  %  (a reasonable number is the length of your
                  %  time-vector)
vidObj = VideoWriter('yourname_hw15_pX.avi'); 
                  % Instead of "hw15_pX", you may, of course, use any name.
vidObj.Quality = 75;  % this is the default value;
                      %  greater/smaller values improve/degrade quality of
                      %   video and increase/decrease the .avi file size
vidObj.FrameRate = 2; % I find this number of frames per second adequate for this kind of movies
open(vidObj);
      


% Set the boundary and initial conditions.

   ........


% Plot the first frame of the movie, showing the initial condition etc.
subplot(3,1,1);
mesh(x,y,U);
axis([0 xmax 0 ymax 0 max(max(U))*1.1]);
title('Computed solution','fontsize',10)
subplot(3,1,2);
mesh(x,y,zeros(L,M));              % plot of the initial error
axis([0 xmax 0 ymax 0 10^(-3) ]);  
title('Numerical error','fontsize',10)
subplot(3,1,3);
mesh(x,y,U- "PLANAR PART OF THE SOLUTION" );
    % This is relevant for problems 2 and 9 only; for problems 1 and 8 use
    % only two panels in the subplot.
    % For the "PLANAR PART OF THE SOLUTION", you, of course, need to substitute its expression.
axis([0 xmax 0 ymax 0 max(max(U-scale*(1-Y/ymax)))*1.1 ]);
title('Computed solution  MINUS  The Plane','fontsize',10)


% Initialize the movie's frames:
writeVideo(mymovie, getframe(gcf));



% Begin the evolution in time:
for **********
    
    % Compute the solution at the new time level. It is denoted U.
    
        ..................  

    rec_frame=4;      % record only every "rec_frame"-th frame (to reduce the size of the .avi file)
                      %  in this case, you'll be recording every 4th time step
    if rem(n,rec_frame) == 0
    
        % Compute the exact solution for reference.
        
            ..........................
        
		% Plot the computed solution and its error:
		subplot(3,1,1);
        mesh(x,y,U);
        axis([0 xmax 0 ymax 0 max(max(U))*1.1 ]);
		title('Computed solution','fontsize',10)
		subplot(3,1,2);
        mesh(x,y,U-Uexact);
        vert_extent=max(max(abs(U-Uexact)))*1.1 + 10^(-8);   % see below
		axis([0 xmax 0 ymax -vert_extent  vert_extent ]);  
		title(['Numerical error of magnitude   ' num2str(max(max(abs(U-Uexact))))],'fontsize',10)
		subplot(3,1,3);
		mesh(x,y,U- "PLANAR PART OF THE SOLUTION");
                % See the note made when creating the first frame.
		axis([0 xmax 0 ymax 0 max(max(U-scale*(1-Y/ymax)))*1.1 ]);
		title('Computed solution  MINUS  The Plane','fontsize',10)
        

        
        writeVideo(mymovie, getframe(gcf));
         
    end 
end

pause(0.2)

% Plot the _final_ computed solution and its error:

    .....................

writeVideo(mymovie, getframe(gcf));
        
close(mymovie);