Industrial Training




  Solution of Heat Equation


C Program


A number of partial differential equations arise during the study and research of applied mathematics and engineering. Some frequently used partial differential equations in engineering and applied mathematics are heat equation, equation of boundary layer flow, equation of electromagnetic theory, poison’s equation etc..
But only few of them can be solved analytically which is more laborious and time consuming. Here, is a C program for solution of heat equation with source code and sample output. It can be used to solve one dimensional heat equation by using Bendre-Schmidt method.
The working principle of solution of heat equation in C is based on a rectangular mesh in a x-t plane (i.e. space-time plane) with the spacing h along x direction and k along t direction or. Simply, a mesh point (x,t) is denoted as (ih,jk). The calculations are based on one dimensional heat equation which is given as:
δu/δt = c2*δ2u/δx2
where c2 = k/sρ is the diffusivity of a substance,
k= coefficient of conductivity of material,
ρ= density of the material, and
s= specific heat capacity
The C source code given here for solution of heat equation works as follows:


  • As the program is executed first of all it asks for value of square of c, value of u(0, t) and u(8, t) .
  • Using the input value the C program calculates the value of alpha.
  • Then, the iteration stats to calculate the value of u at different space and time i.e. u(x,t) using following iteration formula:

Ui,j+1 = ½( ui-1, j + ui+1,j )
  • Finally the the program gives the result as console output as rectangular mess.

In this C program, the function defined is f(x) = 4x-x2/2 and ‘X’ and ‘T’ are macros whose values are 8 and 5 respectively.


f(x) = 4x-x2/2

Source Code for Solution of Heat Equation in C:


#include< stdio.h>
#include< math.h>
#define X 8
#define T 5
float fun(int a)
{
    return(4*a-(a*a)/2.0);
}
main()
{
    float u[X+1][T+1], h=1.0, k=0.0125,c,al,us,ue;
    int j,i;
    printf("\n Enter the square of 'c' :");
    scanf("%f",&c);
    al=c*k/pow(h,2);
    printf(" Enter the value of u[0, t] :");
    scanf("%f",&us);
    printf("  Enter the value of u[%d,t]:", X);
    scanf("%f",&ue);
    for(i=0;i<=T;i++)
    u[0][i]=u[X][i]=us;
    for(j=1;j<=X-1;j++)
    u[j][0]=fun(j);
    for(i=0;i<=T-1;i++)
    for(j=1;j<=X-1;j++)
    u[j][i+1]=al*u[j-1][i]+(1-2*al)*u[j][i]+al*u[j+1][i];
    printf(" The value of alpha is %4.2f\n",al);
    printf(" The value of u[j,i] are:\n ");
    for(i=0;i< T;i++)
    {
        for(j=0;j< X;j++)
        printf("%7.4f\t",u[j][i]);
        printf("\n");
    }
    }

Input/Output:



The analytical solution of heat equation is quite complex. The C program for solution of heat equation is a programming approach to calculate head transferred through a plate in which heat at boundaries are know at a certain time. With help of this program the heat any point in the specimen at certain time can be calculated.




Hi I am Pluto.