# MATLAB Modelling of Heat Equation

April 29, 2022

By  Shubham

lang: en_US

## Introduction:

According to Wikipedia, a partial differential equation (PDE) is an equation that imposes relations between the various partial derivatives of a multivariable function. PDE forms the foundational basis for modern scientific understanding of sound, heat, diffusion, thermodynamics, and quantum mechanics. This blog will deal with applying partial differential equations in the form of the heat equation and its modelling in MATLAB.

## The Heat Equation: Mathematical Preliminaries

### Obtaining the heat equation:

Let’s start with the derivation of the heat equation. Consider a heat-conducting homogeneous rod, extending from to along the axis having a uniform cross-section .

Heat conduction in a thin bar

Let the material density be . Assume the rod is laterally insulated so that heat flows only in the direction of the axis. Let denote the temperature of the cross-section at point at any instant of time t and sigma be the specific heat of the rod.

The amount of heat in the rod between the cross-section at and is,

Rate of heat flowing into the segment is,

Rate of heat flowing out of the segment is,

Here denotes the thermal conductivity of the rod. You might be wondering about the negative sign in front of the two equations given above. The negative sign indicates that the heat flows in the direction of colder regions. The amount of heat content between the section and must be equal to the difference between the amount of heat that flows in at cross-section and the amount heat that flows out the at cross-section . Hence,

To solve the integration, assume the function to be a continuous function of and apply the mean value theorem.

This equation is called one-dimensional heat equation. Here, . This constant is called the diffusivity within the solid.

### Initial and boundary conditions:

An initial boundary value problem for the heat equation consists of the PDE along with three other conditions specified at , and . More formally it can be written as:

and it describes the temperature distribution in the rod at any time subject to conditions

And

Equation describes the initial conditions while equation describes the boundary conditions.

### Solving the heat equation:

let’s reconsider our heat equation. We have to find the solution to the homogeneous heat equation

Having the initial condition and boundary conditions .

We will use the variable separation method to solve the equation. The solution that we seek will be in the form

Since, and . Therefore, the above equation becomes . This equation can be rewritten as . Here, is a constant. This equation gives us two ordinary differential equations: and . The boundary condition changes to and for .

Depending on the value of , there are three cases: , and . For all the three cases the general solution for the first differential equation will be as follows:

• For ,
• For ,
• For ,

For second differential equation, we will use in the equation . The general solution is .

The particular solution of the considered heat equation is . The general solutions hence equal a linear sum of the particular solution. Here the coefficient can be obtained by setting for the entire equation. Hence,

And,

Now that the heat equation is ready let’s model it in MATLAB.

The initialization conditions and boundary values are based on the heat equation we used and derived earlier. Hence, if the boundary conditions are different, they should be changed in the codes. With this info in mind, let’s dive into the MATLAB code.

## Modelling the heat equation in MATLAB:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Shubham Kumar, MATLAB Helper
% Topic: Heat Equation
% Website: https://matlabhelper.com
% Date: 03-04-2022
% Version: R2021b
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc;
clear;
close all;

% Initialisation
M = 15;
dx = 0.125;
dt = 0.05;
X = 0:dx:pi;
T = 0:dt:2;
u = zeros(length(T),length(X));

% Boundary Conditions
Xx = repmat(X,[length(T) 1]);
Tt = repmat(T',[1 length(X)]);

% Solving the equations
for m = 1:M
temp = 2*m-1;
coeff = 8 / (pi * temp * temp * temp);
u = u + coeff * sin(temp*Xx) .* exp(-temp * temp * Tt);
end

% Plotting the heat equation
surf(Xx,Tt,u)
xlabel('distance','Fontsize',20);
ylabel('time','Fontsize',20);
zlabel('u(x,t)','Fontsize',20);

Seeing the code above, one might wonder what the repmat() function is and why it is used. As per the MathWorks website, the repmet() function returns copies of an array. Hence, we are making copies of the function for the different values of X and T. To solve the equations, we will introduce a for loop which will go up to the value of M and calculates the coefficients and hence the function .

Finally, let’s plot the equations and check our result.

On running the above code, MATLAB will generate the following graph:

Graph of heat equation

## Analyzing the result:

Since the result is a 3D plot, it can be rotated to a different point of view and analyzed. The unrotated plot tells us that temperature within a thin bar is zero at the ends. This result satisfies the boundary conditions. Now, let’s rotate the graph.

Graph of heat equation rotated horizontally

The graph shows that the heat flows out from the center of the thin metallic bar towards the colder region and the ends of the metallic bar where the temperature is removed.

Graph of heat equation rotated sidewise

The parabolic shape is reduced as we move away from the hotter region towards the colder area with the increase in time. This again shows that the heat is being removed from the ends of the metallic bar.

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules.

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

#### Shubham

I have done my bachelor degree in electronics from University of Delhi. I like to tinker with the hardware to understand how it actually works.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

#### Connect with MATLAB Helper ®

Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

Use Website Chat or WhatsApp at +91-8104622179

Watch our Latest Video

Meet Industrial Experts & Ask your Questions