Advertisement
davidagross

wave_eqn_neumman_2d

Dec 9th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.07 KB | None | 0 0
  1. function [udata] = wave_eqn_neumman_2d
  2. % wave_eqn_neumman_2d
  3. %
  4. % Sovles the following 2D PDE with Neumann boundary conditions.  
  5. % PDE is numerically solved on the interval [0,L] X [0,M]
  6. %
  7. % utt = D * (uxx + uyy)
  8. %
  9. % Created By: David Gross
  10. % Date Created: Apr 18, 2006
  11. % Date Modified: Apr 21, 2006
  12.  
  13. % -------------------------------------------------------------------------
  14. % Grid, variables, and initial data:
  15. % -------------------------------------------------------------------------
  16. Nx = 50;                                   % Number of mesh points
  17. Ny = 50;
  18. L = 1;                                     % Domain Size  
  19. M = 1;
  20. dx = L/Nx;
  21. dy = M/Ny;
  22. x = (0:Nx)*dx;                              % Vector to solve PDE on [0,L]    
  23. y = (0:Ny)*dy;
  24. D = 1;                                     % Diffusion constant
  25. t = 0;                                     % Starting Time
  26. dt = 0.005; tmax = 1;                    % Time step and final time
  27. nux = D*dt/dx^2;
  28. nuy = D*dt/dy^2;
  29.  
  30. [xx, yy] = meshgrid(x,y);
  31. % u0 = exp(-40*((xx-L/2).^2+(yy - M/4).^2));
  32.  
  33. u0_x = '1*(-0.5*(cos(2*pi*(x-.25)/L)-1)).^4';
  34. u0_y = '1*(-0.5*(cos(2*pi*(y-.25)/M)-1)).^4';  % Initial Data
  35. % u0_x = 'sin(3*pi*x/(L))';
  36. % u0_y = 'abs(sin(pi/1.5*y/M))';
  37.  
  38. % -------------------------------------------------------------------------
  39. % Implicit Matrices
  40. % -------------------------------------------------------------------------
  41.  
  42. vars = {'x' 'y'};
  43. for i = 1:length(vars)
  44.     eval(['row = zeros(1,N' vars{i} '+1);']);
  45.     eval(['row(1) = 1+nu' vars{i} ';']);
  46.     eval(['row(2) = -nu' vars{i} '/2;']);
  47.     eval([vars{i} 'mat = toeplitz(row);']);
  48.         % Boundary Conditions
  49.     eval([vars{i} 'mat(1, 1:3) = [-3,4,-1];']);
  50.     eval([vars{i} 'mat(N' vars{i} '+1, N' vars{i} '-1:N' vars{i} '+1) = [1, -4, 3];']);
  51. end
  52.  
  53. % -------------------------------------------------------------------------
  54. % Fix initial conditions and equations
  55. % -------------------------------------------------------------------------
  56. % u = eval(u0);
  57. u = eval(vectorize(u0_x))'*eval(vectorize(u0_y));
  58. % u = u0;
  59. u_old = u;
  60.  
  61. % -------------------------------------------------------------------------
  62. % Plot values
  63. % -------------------------------------------------------------------------
  64. nplots = round(tmax/dt);
  65. frames = 50;
  66. nplt = floor(nplots/frames);
  67.  
  68. % -------------------------------------------------------------------------
  69. % Put initial values in memory
  70. % -------------------------------------------------------------------------
  71. udata(:,:,1) = u;
  72. tdata = t;
  73.  
  74.         clf
  75.        
  76.         set(gcf, 'Renderer', 'zbuffer');
  77.         [xxx, yyy] = meshgrid(0:1/32:L,0:1/32:M);
  78.         uuu =  interp2(xx, yy, u, xxx, yyy, 'cubic');
  79.         surf(xxx, yyy, uuu);
  80. %         surf(x, y, u);
  81.         % view(150,30)
  82.         zlim([-.15 1]);
  83.         colormap(jet);
  84.         title(['t = ' num2str(t)]);
  85.         xlabel('y');
  86.         ylabel('x');
  87.         drawnow;
  88.        
  89.         pause
  90.  
  91. % -------------------------------------------------------------------------
  92. % Solve PDE and plot results
  93. % -------------------------------------------------------------------------
  94. tic
  95. for n = 1:nplots
  96.    
  97.     t = t+dt;
  98.    
  99.     [uxx uyy] = laplacian(u);
  100.    
  101.     u_new = 2*u - u_old + dt^2*D*(uxx+uyy); % Leap-frogging for 2nd time deriv.
  102.    
  103.     u_old = u;
  104.     u = u_new;
  105.  
  106.     % printing when we want to see it
  107.     if mod(n,nplt) == 0
  108.         udata(:,:,n) = u; tdata = [tdata t];
  109.        
  110.         clf
  111.        
  112.         set(gcf, 'Renderer', 'zbuffer');
  113.         [xxx, yyy] = meshgrid(0:1/32:L,0:1/32:L);
  114.         uuu =  interp2(xx, yy, u, xxx, yyy, 'cubic');
  115.         surf(xxx, yyy, uuu);
  116. %         surf(y, x, u);
  117. %         view(150,30)
  118.         zlim([-.15 1]);
  119.         colormap(jet);
  120.         title(['t = ' num2str(t)]);
  121.         xlabel('y');
  122.         ylabel('x');
  123.         drawnow;
  124.        
  125.     end
  126.    
  127.     % Stop if we see NaNs or numbers > 2^120
  128.     if any(any([isnan([u]) (abs([u]) > 2^120)]))
  129.         disp('Calculation halted due to inaccuracies'); break;
  130.     end
  131.    
  132. end
  133. toc
  134. % time_elapsed = toc
  135.  
  136. % -------------------------------------------------------------------------
  137. % Plotting
  138. % -------------------------------------------------------------------------
  139.  
  140.  
  141.  
  142. % -------------------------------------------------------------------------
  143. % Laplacian for 2D derivative calculation:
  144. % -------------------------------------------------------------------------
  145. function [uxx uyy] = laplacian(u)
  146. x_N = length(u(1,:));
  147. y_N = length(u(:,1));
  148.  
  149. vars = {'x' 'y'};
  150. for i = 1:length(vars)
  151.     if (eval(['mod(' vars{i} '_N,2)==0']))
  152.         eval(['k_' vars{i} ' = sqrt(-1)*[0:' vars{i} '_N/2-1 0 -' vars{i} '_N/2+1:-1];']);
  153.     else
  154.         eval(['k_' vars{i} ' = sqrt(-1)*[0:(' vars{i} '_N-1)/2 0 -(' vars{i} '_N-1)/2+1:-1];']);
  155.     end
  156. end
  157.  
  158. uxx = [];
  159. uyy = [];
  160.  
  161. for i=1:y_N
  162.     ux = real(ifft(k_x .* fft(u(i,:))));
  163.     uxx = [uxx ; real(ifft(k_x .* fft([0 ux(2:x_N-1) 0 ])))];
  164. end
  165.  
  166. for i=1:x_N
  167.     uy = real(ifft(k_y .* fft(u(:,i)')));
  168.     uyy = [uyy , real(ifft(k_y .* fft([0 uy(2:y_N-1) 0 ])))'];
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement