Creating 3D Animations in MATLAB for ODEs/PDEs

In summary: Z; In summary, the code does not allow for animating along an axis. I found a great example on how to do this on Wikipedia.
  • #1
QweticoWoods
11
0
I was tasked with making some animations of simple ODEs and PDEs in MATLAB for use in an undergraduate class. (Vibrating string, Heat Equation, Navier-Stokes)...

I wrote some code and plotted some graphs, but I'm not seeing an option to animate along an axis in 2010a. Maybe I'm blind and it's staring me right in the face.

Thoughts?
 
Physics news on Phys.org
  • #2
Oye... Nevermind. I found a great example on how to do this on Wikipedia.

Heat_eqn.gif


Code:
% illustration of the heat equation
% Solve the heat equation using finite differences and Forward Euler
function main()
 
   % the number of data points. More points means prettier picture.
   N = 100;
 
   L = 2.5; % the box size is [-L, L] x [-L, L]
 
   XX = linspace(-L, L, N);
   YY = linspace(-L, L, N);
   [X, Y] = meshgrid(XX, YY);
 
   scale = 2;
   Z = get_step_function (N, scale, X, Y);
 
   CFL = 0.125; % Courant–Friedrichs–Lewy
   dx = XX(2)-XX(1);  dy = dx; % space grid
   dt = CFL*dx^2;
 
   plot_dt = 0.004; % plot every plot_dt iterations
 
 
% Solve the heat equation with zero boundary conditions
   T = 0:dt:1;
   iter = 0;
   frame_no = 0;
   for t=T
 
      % plot the current temperature distribution
      if floor(t/plot_dt) + 1 > frame_no
 
	 frame_no = frame_no + 1
 
        % plot the surface
	 figure(2); clf; 
	 surf(X, Y, Z);
 
         %  make the surface beautiful
	 shading interp; colormap autumn;
 
         % add in a source of light
	 camlight (-50, 54);
	 lighting phong;
 
         % viewing angle
	 view(-40, 38);
 
	 axis equal; axis off;
	 axis([-L, L, -L, L, 0, scale])
 
	 hold on; plot3(0, 0, 3.4, 'g*'); % a marker to help with cropping
 
	 pause(0.1);
	 %return
 
	 file = sprintf('Movie_frame%d.png', 1000+frame_no); saveas(gcf, file) %save the current frame
 
	 disp(file); %show the frame number we are at
 
         % cut at max_fr_no frames
	 max_fr_no = 15; 
	 if frame_no >= max_fr_no
	    break
	 end
 
      end
 
      % advance in time
      W = 0*Z;
      for i=2:(N-1)
	 for j=2:(N-1)
 
	    W(i, j) = Z(i, j) + dt * ( Z(i+1, j) + Z(i-1, j) + Z(i, j-1) + Z(i, j+1) - 4*Z(i, j))/dx^2;
 
	 end
      end
      Z = W;
 
   end
 
 
% The gif image was creating with the command 
% convert -antialias -loop 10000  -delay 20 -compress LZW Movie_frame10* Heat_eqn.gif 
 
% get a function which is 1 on a set, and 0 outside of it
function Z = get_step_function(N, scale, X, Y)
 
   c = 2;
   d=-1;
   e=1;
   f=0.5;
   k=1.2;
   shift=10;
 
   Z = (c^2-(X/e-d).^2-(Y/f).^2).^2 + k*(c+d-X/e).^3-shift;
 
   Z = 1-max(sign(Z), 0);
   Z = scale*Z;
 

Attachments

  • Heat_eqn.gif
    Heat_eqn.gif
    64.9 KB · Views: 635

1. How can I create 3D animations in MATLAB for ODEs/PDEs?

To create 3D animations in MATLAB for ODEs/PDEs, you will need to use the built-in ode45 solver to solve the differential equations and generate a matrix of solution points. Then, you can use the surf or mesh functions to create a 3D plot of the solution. Finally, use the getframe and movie functions to capture and play the animation.

2. Can I customize the appearance of the 3D animation?

Yes, you can customize the appearance of the 3D animation by adjusting the color, lighting, and viewing angle of the plot. You can also add labels and a legend to provide more information about the animation.

3. How can I save the 3D animation as a video file?

To save the 3D animation as a video file, you can use the VideoWriter function in MATLAB. This will allow you to specify the frame rate, video format, and file name for your animation. You can also use other software such as Adobe Premiere or Windows Movie Maker to edit and save the animation as a video.

4. Can I add multiple solutions to the same 3D animation?

Yes, you can add multiple solutions to the same 3D animation by using the hold on command. This will allow you to plot multiple solutions on the same figure and create a more comprehensive animation.

5. Are there any resources available for learning how to create 3D animations in MATLAB for ODEs/PDEs?

Yes, there are many online tutorials and resources available for learning how to create 3D animations in MATLAB for ODEs/PDEs. You can also refer to the official MATLAB documentation for more information and examples. Additionally, there are books and courses available that specifically focus on creating animations in MATLAB.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
16K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Introductory Physics Homework Help
Replies
14
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
10K
Back
Top