├── README.md ├── SECURITY.md ├── Simple1DWaveEquation.md ├── Simple1DWaveEquation.mlx ├── Simple1DWaveEquation_media ├── figure_0.png ├── figure_1.png └── image_0.gif ├── Simple2DWaveEquation.md ├── Simple2DWaveEquation.mlx ├── Simple2DWaveEquation_media ├── figure_0.png ├── figure_1.png └── image_0.gif ├── SimpleWaveEquation.pdf ├── license.txt ├── wave.gif └── wave2D.gif /README.md: -------------------------------------------------------------------------------- 1 | # Finite differences for the wave equation 2 | 3 | This repo provides an example implementation of a simple numerical schemes for the 1D and 2D wave equation. 4 | 5 | To open the repo in MATLAB Online, please click 6 | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=mathworks/Simple-Wave-Equation-solver) 7 | 8 | To view this file on File Exchange, please click 9 | [![View Simple Wave Equation solver on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://jp.mathworks.com/matlabcentral/fileexchange/59915-simple-wave-equation-solver) 10 | 11 | 12 | ## 1D case 13 | 14 | See [Simple1DWaveEquation.md](./Simple1DWaveEquation.md) or click 15 | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=mathworks/Simple-Wave-Equation-solver&file=/Simple1DWaveEquation.mlx) to open the livescript in MATLAB Online. 16 | 17 | image_0.gif 18 | 19 | 20 | ## 2D case 21 | 22 | See [Simple2DWaveEquation.md](./Simple2DWaveEquation.md) or click 23 | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=mathworks/Simple-Wave-Equation-solver&file=/Simple2DWaveEquation.mlx) to open the livescript in MATLAB Online. 24 | 25 | 26 | image_0.gif 27 | 28 | Copyright 2015-2023 The MathWorks, Inc. 29 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Reporting Security Vulnerabilities 2 | 3 | If you believe you have discovered a security vulnerability, please report it to 4 | [security@mathworks.com](mailto:security@mathworks.com). Please see 5 | [MathWorks Vulnerability Disclosure Policy for Security Researchers](https://www.mathworks.com/company/aboutus/policies_statements/vulnerability-disclosure-policy.html) 6 | for additional information. -------------------------------------------------------------------------------- /Simple1DWaveEquation.md: -------------------------------------------------------------------------------- 1 | 2 | # Finite differences for the wave equation 3 | 4 | Implementation of a simple numerical schemes for the wave equation. 5 | 6 | $$ \frac{\partial^2 u}{\partial t^2 }=c^2 \frac{\partial^2 u}{\partial x^2 }. $$ 7 | 8 | Applying the second-order centered differences to approximate the derivatives, 9 | 10 | $$ \frac{u_j^{n+1} -2u_j^n +u_j^{n-1} }{\Delta t^2 }=c^2 \frac{u_{j+1}^n -2u_j^n +u_{j-1}^n }{\Delta x^2 }. $$ 11 | 12 | It can be arrange to obtain 13 | 14 | $$ u_j^{n+1} =s(u_{j+1}^n +u_{j-1}^n )+2(1-s)u_j^n -u_j^{n-1} , $$ 15 | 16 | where 17 | 18 | $$ s=c^2 \frac{\Delta t^2 }{\Delta x^2 }. $$ 19 | 20 | It is known that the scheme needs to satisfy $s\le 1$ for stability. Copyright 2015-2016 The MathWorks, Inc. 21 | 22 | 23 | image_0.gif 24 | 25 | # Proboem Setup 26 | ```matlab 27 | N = 101; 28 | L = 4*pi; 29 | x = linspace(0,L,N); 30 | 31 | % It has three data set; 1: past, 2: current, 3: future. 32 | u = zeros(N,3); 33 | s = 0.5; 34 | ``` 35 | # Initial Condition 36 | ```matlab 37 | % Gaussian Pulse 38 | y = 2*exp(-(x-L/2).^2); 39 | u(:,1) = y; 40 | u(:,2) = y; 41 | 42 | % Plot the initial condition. 43 | handle_line = plot(x,u(:,2),'LineWidth',2); 44 | axis([0,L,-2,2]); 45 | xlabel('x'); ylabel('u'); 46 | title('Wave equation'); 47 | ``` 48 | 49 |
figure_0.png
50 | 51 | # Boundary condition 52 | ```matlab 53 | % Dirichet Boundary conditions 54 | u(1,:) = 0; 55 | u(end,:) = 0; 56 | ``` 57 | # Simulation 58 | ```matlab 59 | filename = 'wave.gif'; 60 | for ii=1:100 61 | disp(['at ii= ', num2str(ii)]); 62 | u(2:end-1,3) = s*(u(3:end,2)+u(1:end-2,2)) ... 63 | + 2*(1-s)*u(2:end-1,2) ... 64 | - u(2:end-1,1); 65 | u(:,1) = u(:,2); 66 | u(:,2) = u(:,3); 67 | handle_line.YData = u(:,2); 68 | drawnow; 69 | frame = getframe(gcf); 70 | im = frame2im(frame); 71 | [A,map] = rgb2ind(im,256); 72 | if ii==1 73 | imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.05); 74 | else 75 | imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.05); 76 | end 77 | end 78 | ``` 79 | 80 |
figure_1.png
81 | 82 | -------------------------------------------------------------------------------- /Simple1DWaveEquation.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple1DWaveEquation.mlx -------------------------------------------------------------------------------- /Simple1DWaveEquation_media/figure_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple1DWaveEquation_media/figure_0.png -------------------------------------------------------------------------------- /Simple1DWaveEquation_media/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple1DWaveEquation_media/figure_1.png -------------------------------------------------------------------------------- /Simple1DWaveEquation_media/image_0.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple1DWaveEquation_media/image_0.gif -------------------------------------------------------------------------------- /Simple2DWaveEquation.md: -------------------------------------------------------------------------------- 1 | 2 | # Finite differences for the 2D wave equation 3 | 4 | Copyright 2023 The MathWorks, Inc. 5 | 6 | 7 | Implementation of a simple numerical schemes for the wave equation. 8 | 9 | $$ \frac{\partial^2 u}{\partial t^2 }=c^2 \left(\frac{\partial^2 u}{\partial x^2 }+\frac{\partial^2 u}{\partial y^2 }\right). $$ 10 | 11 | Applying the second-order centered differences to approximate the derivatives, 12 | 13 | $$ \frac{u_{i,j}^{n+1} -2u_{i,j}^n +u_{i,j}^{n-1} }{\Delta t^2 }=c^2 \left(\frac{u_{i+1,j}^n -2u_{i,j}^n +u_{i-1,j}^n }{\Delta x^2 }+\frac{u_{i,j+1}^n -2u_{i,j}^n +u_{i,j-1}^n }{\Delta y^2 }\right). $$ 14 | 15 | It can be arrange to obtain 16 | 17 | $$ u_{i,j}^{n+1} =s_x (u_{i+1,j}^n +u_{i-1,j}^n )+s_y (u_{i,j+1}^n +u_{i,j-1}^n )+2(1-s_x -s_y )u_{i,j}^n -u_{i,j}^{n-1} , $$ 18 | 19 | where 20 | 21 | $$ s_x =c^2 \frac{\Delta t^2 }{\Delta x^2 },s_y =c^2 \frac{\Delta t^2 }{\Delta y^2 }. $$ 22 | 23 | It is known that the scheme needs to satisfy $s\le 1$ for stability. 24 | 25 | 26 | image_0.gif 27 | 28 | # Proboem Setup 29 | ```matlab 30 | N = 101; 31 | L = 4*pi; 32 | x = linspace(0,L,N); 33 | y = linspace(0,L,N); 34 | 35 | % It has three data set; 1: past, 2: current, 3: future. 36 | u = zeros(N,N,3); 37 | s = 0.5; 38 | ``` 39 | # Initial Condition 40 | ```matlab 41 | % Gaussian Pulse 42 | [X,Y] = meshgrid(x,y); 43 | init_u = 2*exp(-2*(X-L/4).^2-2*(Y-L/3).^2); 44 | u(:,:,1) = init_u; 45 | u(:,:,2) = init_u; 46 | 47 | % Plot the initial condition. 48 | handle_surf = surf(X,Y,init_u); 49 | axis([0,L,0,L,-2,2]); 50 | xlabel('x'); 51 | ylabel('y'); 52 | title('2D Wave equation'); 53 | ``` 54 | 55 |
figure_0.png
56 | 57 | # Boundary condition 58 | ```matlab 59 | % Dirichet Boundary conditions 60 | u(1,:,:) = 0; 61 | u(end,:,:) = 0; 62 | u(:,1,:) = 0; 63 | u(:,end,:) = 0; 64 | ``` 65 | # Simulation 66 | ```matlab 67 | filename = 'wave2D.gif'; 68 | for ii=1:200 69 | % disp(['at ii= ', num2str(ii)]); 70 | % advance time 71 | u(2:end-1,2:end-1,3) = s*(u(2:end-1,3:end,2)+u(2:end-1,1:end-2,2)) ... 72 | + s*(u(3:end,2:end-1,2)+u(1:end-2,2:end-1,2)) ... 73 | + 2*(1-2*s)*u(2:end-1,2:end-1,2) ... 74 | - u(2:end-1,2:end-1,1); 75 | u(:,:,1) = u(:,:,2); 76 | u(:,:,2) = u(:,:,3); 77 | 78 | % update the figure 79 | handle_surf.ZData = u(:,:,2); 80 | drawnow; 81 | 82 | % capture frame to generate gif 83 | frame = getframe(gcf); 84 | im = frame2im(frame); 85 | im = imresize(im,0.5); 86 | [A,map] = rgb2ind(im,128); 87 | if ii==1 88 | imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.02); 89 | else 90 | imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.02); 91 | end 92 | end 93 | ``` 94 | 95 |
figure_1.png
96 | 97 | -------------------------------------------------------------------------------- /Simple2DWaveEquation.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple2DWaveEquation.mlx -------------------------------------------------------------------------------- /Simple2DWaveEquation_media/figure_0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple2DWaveEquation_media/figure_0.png -------------------------------------------------------------------------------- /Simple2DWaveEquation_media/figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple2DWaveEquation_media/figure_1.png -------------------------------------------------------------------------------- /Simple2DWaveEquation_media/image_0.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/Simple2DWaveEquation_media/image_0.gif -------------------------------------------------------------------------------- /SimpleWaveEquation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/SimpleWaveEquation.pdf -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, The MathWorks, Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | * In all cases, the software is, and all modifications and derivatives of the 14 | software shall be, licensed to you solely for use in conjunction with 15 | MathWorks products and service offerings. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 21 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /wave.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/wave.gif -------------------------------------------------------------------------------- /wave2D.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Simple-Wave-Equation-solver/b2f66cf54e254f0fb3ec1ab4c4d48fa2af1dc445/wave2D.gif --------------------------------------------------------------------------------