Partial Differential Equations
(Laplace Equation)
Objective : - To write C++ Program to solve Partial Differential Equation – Laplace Equations
Theory :
Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
int i,j; //Global Definition of i,j
void main()
{
clrscr();
float x=0,y=0,dx=0,dy=0,bc1=0,bc2=0,bc3=0,bc4=0;
int m=0,n=0,k=0,h=0;
float T[25][25];
cout<<setprecision(1);
cout<<"\n Enter the Dimensions of the Plate (x,y)";
cin>>x>>y;
cout<<"\n Enter the Grid Size (dx,dy)";
cin>>dx>>dy;
cout<<"\n Enter the Boundary Conditions (BC1,BC2,BC3,BC4) ";
cin>>bc1>>bc2>>bc3>>bc4;
cout<<"\n Enter the Number of Iterations ";
cin>>h;
m=x/dx;
n=y/dy;
//Seting Initial Condition
for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)
{
T[i][j]=0;
}
}
//Seting Boundary Condition
for(i=0;i<=m;i++)
{
T[i][0]=bc1;//Boundary Condition 1
}
for(j=0;j<=n;j++)
{
T[m][j]=bc2;//Boundary Condition 2
}
for(i=0;i<=m;i++)
{
T[i][n]=bc3;//Boundary Condition 3
}
for(j=0;j<=n;j++)
{
T[0][j]=bc4;//Boundary Condition 4
}
//Calcuting the xTemperature in the Interior Points
for(k=1;k<=h;k++)
{
for(i=1;i<=m-1;i++)
{
for(j=1;j<=n-1;j++)
{
T[i][j]=0.25*(T[i+1][j]+T[i-1][j]+T[i][j+1]+T[i][j-1]);
}
}
}
//Displaying the Value of Temperature
for(j=n;j>=0;j--)
{
cout<<"\n";
for(i=0;i<=m;i++)
{
cout<<T[i][j]<<"\t";
}
}
getch();
}
Output:
1. Solve the elliptical Laplace equation for the Square Mesh of 100 mm x 100 mm, with the boundary conditions of 50 with grid size of 20 mm.