/*******************************************************************************/
/*************Program
to solve Laplace's equation by finite difference method*************/
/***************************
Developed by Mahesha M G ***************************/
#include
<stdio.h>
main()
{
int i,j,k,m,n,x,y;
float
a[20][20],l,r,t,b;
FILE *fp;
clrscr();
fp=fopen("c:\\laplace.dat","w");
//output will be stored in this file
printf("\t_______________________________________________________________\n");
printf("\tProgram to solve Laplace's equation by finite difference
method\n");
printf("\t******************
Developed by Mahesha M G *******************\n");
printf("\t_______________________________________________________________\n");
printf("\tEnter boundary conditions\n");
printf("\tValue on left side: ");
scanf("%f",&l);
printf("\tValue on right side: ");
scanf("%f",&r);
printf("\tValue on top side: ");
scanf("%f",&t);
printf("\tValue on bottom side: ");
scanf("%f",&b);
printf("\tEnter length in x direction: ");
scanf("%d",&x);
printf("\tEnter number of steps in x direction: ");
scanf("%d",&m);
printf("\tEnter length in y direction: ");
scanf("%d",&y);
printf("\tEnter number of steps in y direction: ");
scanf("%d",&n);
m++;
n++; //number of mesh
points is one more than number of steps
for(i=1;i<=m;i++)
//assigning boundary values begins
{
a[i][1]=b;
a[i][n]=t;
}
for(i=1;i<=n;i++)
{
a[1][i]=l;
a[m][i]=r;
}
//assigning boundary values ends
for(i=2;i<m;i++)
for(j=2;j<n;j++)
a[i][j]=t; //initialization of
interior grid points
for(k=0;k<100;k++)
{
for(i=2;i<m;i++)
{
for(j=2;j<n;j++)
{
a[i][j]=(a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1])/4;
}
}
}
//calculation by Gauss-Seidel Method
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
fprintf(fp,"%.2f\t",a[i][j]);
fprintf(fp,"\n");
}
fclose(fp);
printf("\nData stored\nPress any key to exit...");
getch();
}
SAMPLE INPUT
PLOT OF DATA GENERATED BY C PROGRAM
Hey!
ReplyDeleteCan i know which package have you used for plot?
C program stored result in .dat file and it is opened in Origin to plot.
ReplyDeleteThanks a lot sir..
ReplyDeleteSir..
ReplyDeletehere how we r implementing the convergence relaxation(residual)..
with addition to this I have done for residual also.. Thanking you..
ReplyDeleteok. If you don't mind, share it.
DeleteHere i have attached the iteration loop with convergence limit..hope u can get it..
ReplyDeletefor(k=1;k<1000;k++)
{
z=0;
for(j=2;j<n;j++)
{
for(i=2;i<m;i++)
{
c[i][j]=a[i][j];
a[i][j]=(a[i-1][j]+a[i+1][j]+a[i][j+1]+a[i][j-1])/4;
}
}
for(j=2;j<n;j++)
{
for(i=2;i<m;i++)
{
if(abs(c[i][j]-a[i][j])<=0.1)
{
z++;
}
}
}
if(z==(m-2)*(n-2))
printf........
i cant locate the .dat file...please help
ReplyDeletefp=fopen("c:\\laplace.dat","w");
Deletein the above line, select the location where you want to store the result.
try D: or E:
Sir, could you explain the //initialisation of interior grid points part????
ReplyDeleteI have taken the top value as initial value for interior grid points.
DeleteOther way is to check the nearest boundary point.
Sir, I want to know ,how iteration will convergs
ReplyDeleteor tell me that how to stop calculation
Good question. I didn't implement that in my code.This code takes 100 iterations.
DeleteTo check the convergence, compare ith and (i+1)th iteration values and it will decrease as we go to higher iterations. One can terminate the loop once the required accuracy is achieved.
Thank you sir,
DeleteBut I implemented convergence in the way that, calculated maximum difference in new and old values at each iteration and checked for tolerance.
but question is I want to set tolerance and how much.
I written program like this. kindly reply with changes.
one thing the data got in file is what it shows.
and how to plot it .
Thank you sir,
DeleteBut I implemented convergence in the way that, calculated maximum difference in new and old values at each iteration and checked for tolerance.
but question is I want to set tolerance and how much.
I written program like this. kindly reply with changes.
one thing the data got in file is what it shows.
and how to plot it .
for(k=0;k<100&&d>0.0001;k++)// while(d>=0.0001)//
Delete{ //printf("Iteration %d\n",k);
for(i=2;idmax)
diff[k]=d;
}printf("\n");
} printf("\n");
for(i=2;i<m;i++)
{
for(j=2;j<n;j++)
{
a[i][j]=anew[i][j];
}
}
fprintf(fp2,"%d\t%.6f\n",k,diff[k]);
} //calculation by Gauss-Jacobi Method
Sir could you explain how to converge iteration
ReplyDeletewhat cammand used for plotting data.
ReplyDeleteSir, I want to know ,how iteration will convergs
ReplyDeleteor tell me that how to stop calculation
Sir plz tell me how to plot the graph.
ReplyDeleteSir plz tell me how to plot the graph.
ReplyDeletePlotted with Origin. Not using C. Sorry for the delayed reply.
Delete