├── README.md
├── SECURITY.md
├── SimpleHeatEquation.md
├── SimpleHeatEquation.mlx
├── SimpleHeatEquation.pdf
├── SimpleHeatEquation_images
├── figure_0.png
├── figure_1.png
└── image_0.png
├── getRHS.m
├── heat.gif
└── license.txt
/README.md:
--------------------------------------------------------------------------------
1 | # Finite differences for the 2D heat equation
2 | [](https://matlab.mathworks.com/open/github/v1?repo=mathworks/Simple-Heat-Equation-solver)
3 | [](https://jp.mathworks.com/matlabcentral/fileexchange/59916-simple-heat-equation-solver)
4 |
5 | Implementation of a simple numerical schemes for the heat equation.
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Applying the second-order centered differences to approximate the spatial derivatives,
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | Neumann boundary condition is employed for no-heat flux, thus please note that the grid location is staggered. Once the right hand side is obtained, the equation can be solved by the ODE suite. Here we use ode15s. Copyright 2015-2016 The MathWorks, Inc.
22 |
23 |
24 |
25 |
26 | 
27 |
28 |
29 | # Problem Setup
30 | ```matlab
31 | N = 50; % Number of grid in x,y-direction
32 | L = 4*pi; % Domain size
33 |
34 | % Grid point
35 | x = linspace(0,L,N);
36 | y = linspace(0,L,N);
37 | % Make it staggered.
38 | x = (x(1:end-1)+x(2:end))/2;
39 | y = (y(1:end-1)+y(2:end))/2;
40 | [X,Y] = meshgrid(x,y);
41 | ```
42 | # Initial Condition
43 | ```matlab
44 | % Let's use MATLAB logo.
45 | % A variable u0 is defined at the center of each grid cell
46 | % thus the number of grid point is N-1.
47 | u0(:,:) = peaks(N-1);
48 |
49 | % Plot it
50 | handle_surf = surf(X,Y,u0);
51 | handle_axes = gca;
52 | handle_axes.ZLim = [-10,10];
53 | handle_axes.CLim = [-10,10];
54 | title('Evolution of MATLAB Logo by Heat equation');
55 | ```
56 |
57 | 
58 |
59 | ```matlab
60 | ```
61 | # Simulation
62 | ```matlab
63 | dx = x(2)-x(1); % spatial grid size
64 | alpha = 2; % coefficient
65 | tspan = linspace(0,1,40);
66 | [t,u] = ode15s(@(t,x)getRHS(x,alpha,dx,N),tspan,u0(:));
67 | ```
68 | # Visualize
69 | ```matlab
70 | Tn = length(t);
71 | u = reshape(u,Tn,N-1,N-1);
72 |
73 | filename = 'heat.gif';
74 | for ii=1:Tn
75 | Z = u(ii,:,:);
76 | Z = squeeze(Z);
77 | handle_surf.ZData = Z;
78 | drawnow;
79 | frame = getframe(gcf);
80 | im = frame2im(frame);
81 | [A,map] = rgb2ind(im,256);
82 | if ii==1
83 | imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.05);
84 | else
85 | imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.05);
86 | end
87 | end
88 | ```
89 |
90 | 
91 |
92 | Copyright 2015-2016 The MathWorks, Inc.
93 | [](https://jp.mathworks.com/matlabcentral/fileexchange/59916-simple-heat-equation-solver)
94 |
95 |
96 |
--------------------------------------------------------------------------------
/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.
--------------------------------------------------------------------------------
/SimpleHeatEquation.md:
--------------------------------------------------------------------------------
1 | # Finite differences for the 2D heat equation
2 |
3 |
4 | Implementation of a simple numerical schemes for the heat equation.
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | Applying the second-order centered differences to approximate the spatial derivatives,
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | Neumann boundary condition is employed for no-heat flux, thus please note that the grid location is staggered. Once the right hand side is obtained, the equation can be solved by the ODE suite. Here we use ode15s. Copyright 2015-2016 The MathWorks, Inc.
21 |
22 |
23 |
24 |
25 | 
26 |
27 |
28 | # Problem Setup
29 | ```matlab
30 | N = 50; % Number of grid in x,y-direction
31 | L = 4*pi; % Domain size
32 |
33 | % Grid point
34 | x = linspace(0,L,N);
35 | y = linspace(0,L,N);
36 | % Make it staggered.
37 | x = (x(1:end-1)+x(2:end))/2;
38 | y = (y(1:end-1)+y(2:end))/2;
39 | [X,Y] = meshgrid(x,y);
40 |
41 | ```
42 | # Initial Condition
43 | ```matlab
44 | % Let's use MATLAB logo.
45 | % A variable u0 is defined at the center of each grid cell
46 | % thus the number of grid point is N-1.
47 | u0(:,:) = peaks(N-1);
48 |
49 | % Plot it
50 | handle_surf = surf(X,Y,u0);
51 | handle_axes = gca;
52 | handle_axes.ZLim = [-10,10];
53 | handle_axes.CLim = [-10,10];
54 | title('Evolution of MATLAB Logo by Heat equation');
55 | ```
56 |
57 | 
58 |
59 | # Simulation
60 | ```matlab
61 | dx = x(2)-x(1); % spatial grid size
62 | alpha = 2; % coefficient
63 | tspan = linspace(0,1,40);
64 | [t,u] = ode15s(@(t,x)getRHS(x,alpha,dx,N),tspan,u0(:));
65 | ```
66 | # Visualize
67 | ```matlab
68 | Tn = length(t);
69 | u = reshape(u,Tn,N-1,N-1);
70 |
71 | filename = 'heat.gif';
72 | for ii=1:Tn
73 | Z = u(ii,:,:);
74 | Z = squeeze(Z);
75 | handle_surf.ZData = Z;
76 | drawnow;
77 | frame = getframe(gcf);
78 | im = frame2im(frame);
79 | [A,map] = rgb2ind(im,256);
80 | if ii==1
81 | imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.05);
82 | else
83 | imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.05);
84 | end
85 | end
86 | ```
87 |
88 | 
89 |
90 |
--------------------------------------------------------------------------------
/SimpleHeatEquation.mlx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mathworks/Simple-Heat-Equation-solver/a3025c4dfd6a2978f7360a92b90593e44642a731/SimpleHeatEquation.mlx
--------------------------------------------------------------------------------
/SimpleHeatEquation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mathworks/Simple-Heat-Equation-solver/a3025c4dfd6a2978f7360a92b90593e44642a731/SimpleHeatEquation.pdf
--------------------------------------------------------------------------------
/SimpleHeatEquation_images/figure_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mathworks/Simple-Heat-Equation-solver/a3025c4dfd6a2978f7360a92b90593e44642a731/SimpleHeatEquation_images/figure_0.png
--------------------------------------------------------------------------------
/SimpleHeatEquation_images/figure_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mathworks/Simple-Heat-Equation-solver/a3025c4dfd6a2978f7360a92b90593e44642a731/SimpleHeatEquation_images/figure_1.png
--------------------------------------------------------------------------------
/SimpleHeatEquation_images/image_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mathworks/Simple-Heat-Equation-solver/a3025c4dfd6a2978f7360a92b90593e44642a731/SimpleHeatEquation_images/image_0.png
--------------------------------------------------------------------------------
/getRHS.m:
--------------------------------------------------------------------------------
1 | function du = getRHS(u,alpha,dx,N)
2 | % Copyright 2015-2016 The MathWorks, Inc.
3 |
4 | % Reshape the date in 2D
5 | u = reshape(u,N-1,N-1);
6 |
7 | % Copy it to new array with boundaries.
8 | ubig = zeros(N+1,N+1);
9 | ubig(2:end-1,2:end-1) = u;
10 |
11 | % Neumann Boundary condition
12 | % set the zero-gradient = no heat flux
13 | ubig(1,:) = ubig(2,:);
14 | ubig(end,:) = ubig(end-1,:);
15 | ubig(:,1) = ubig(:,2);
16 | ubig(:,end) = ubig(:,end-1);
17 |
18 | % Get the second derivatives
19 | u = ubig;
20 | du = alpha/dx^2*(u(1:end-2,2:end-1)-2*u(2:end-1,2:end-1)+u(2:end-1,3:end)...
21 | + u(2:end-1,1:end-2)-2*u(2:end-1,2:end-1)+u(3:end,2:end-1));
22 |
23 | du = du(:);
--------------------------------------------------------------------------------
/heat.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mathworks/Simple-Heat-Equation-solver/a3025c4dfd6a2978f7360a92b90593e44642a731/heat.gif
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------