Industrial Training




      Solution of Wave Equation


C Program


The solution of wave equation represents the displacement function u(x, t) defined for the value of x form 0 to l and for t from 0 to ∞ which satisfies the initial and boundary conditions. The wave equation arises from the convective type of problems in vibration, wave mechanics and gas dynamics. It’s solution is not as simple as the solution of ordinary differential equation.
The Wave Equation is the simplest example of hyperbolic differential equation which is defined by following equation:
δ2u/δt2 = c2 * δ2u/δt2
The C program for solution of wave equation presented here uses the following boundary conditions to solve the problems:
u(x,0) =f(x)
u1 (x,0) = φ(x)
u(0, t) = ψ1(t)
u(1,t) = ψ2(t)
for 0≤t≤T
When the program for Wave Equation in C language is executed, it solves the wave equation by following the steps listed below:


  • First of all, the program asks for the value of square of c.
  • Then, the user has to input value of initial and boundary conditions i.e, u(0,t) and u(5,t). In this program, u(0,t) and u(5,t) are initial and boundary conditions respectively. They can be changed depending upon the nature of problem and given criteria for solution.
  • After inputting these parameters, the program displays the output on screen.

Source Code for Solution of Wave Equation in C:


Here, the function defined is f(x) = x2 (5-x). X and T are macros whose values are assigned to be 5.


f(x) = x2 (5-x)
rogram for Solution of Wave Equation
#include
#include
#define X 5
#define T 5
float fun(int x)
{
    return x*x*(5-x);

}
main()
{
    float u[X+1][T+1],square_of_c, ut, ue;
    int i,j;
    printf("\n Enter the square of c: ");
    scanf("%d",&square_of_c);
    printf(" Enter the value of u[0][t]:");
    scanf("%f",&ut);
    printf(" Enter the value of u[%d][t]:",X);
    scanf("%f",&ue);
    for(j=0;j<=T;j++)
    {
        u[0][j]=ut;
        u[X][j]=ue;
    }
    for(i=1;i<=X-1;i++)
    u[i][1]=u[i][0]=fun(i);
    for(j=1;j<=T-1;j++)
    for(i=1;i<=X-1;i++)
    u[i][j+1]=u[i-1][j]+u[i+1][j]-u[i][j-1];
    printf(" The values of u[i][j] are: \n");
    for(j=0;j<=T;j++)
    {
        for(i=0;i<=X;i++)
        printf("%6.1f",u[i][j]);
        printf("\n");

    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include< stdio.h>
#include< math.h>
#define X 5
#define T 5
float fun(int x)
{
    return x*x*(5-x);
 
}
main()
{
    float u[X+1][T+1],square_of_c, ut, ue;
    int i,j;
    printf("\n Enter the square of c: ");
    scanf("%d",&square_of_c);
    printf(" Enter the value of u[0][t]:");
    scanf("%f",&ut);
    printf(" Enter the value of u[%d][t]:",X);
    scanf("%f",&ue);
    for(j=0;j<=T;j++)
    {
        u[0][j]=ut;
        u[X][j]=ue;
    }
    for(i=1;i<=X-1;i++)
    u[i][1]=u[i][0]=fun(i);
    for(j=1;j<=T-1;j++)
    for(i=1;i<=X-1;i++)
    u[i][j+1]=u[i-1][j]+u[i+1][j]-u[i][j-1];
    printf(" The values of u[i][j] are: \n");
    for(j=0;j<=T;j++)
    {
        for(i=0;i<=X;i++)
        printf("%6.1f",u[i][j]);
        printf("\n");
 
    }
 
}

Input/Output:



Seeking the solution of wave equation via. manual calculation involves a high probability of occurrence of error, and it requires special techniques of numerical methods leading to lengthy calculations. This C program is simply a programming approach to find solution of wave equation. If you have any question, bring them up from the comments.




Hi I am Pluto.