Saturday, June 16, 2012

C code to solve Laplace's Equation by finite difference method



/*******************************************************************************/
/*************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

22 comments:

  1. Hey!
    Can i know which package have you used for plot?

    ReplyDelete
  2. C program stored result in .dat file and it is opened in Origin to plot.

    ReplyDelete
  3. Sir..
    here how we r implementing the convergence relaxation(residual)..

    ReplyDelete
  4. with addition to this I have done for residual also.. Thanking you..

    ReplyDelete
  5. Here i have attached the iteration loop with convergence limit..hope u can get it..
    for(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........

    ReplyDelete
  6. i cant locate the .dat file...please help

    ReplyDelete
    Replies
    1. fp=fopen("c:\\laplace.dat","w");
      in the above line, select the location where you want to store the result.
      try D: or E:

      Delete
  7. Sir, could you explain the //initialisation of interior grid points part????

    ReplyDelete
    Replies
    1. I have taken the top value as initial value for interior grid points.
      Other way is to check the nearest boundary point.

      Delete
  8. Sir, I want to know ,how iteration will convergs

    or tell me that how to stop calculation

    ReplyDelete
    Replies
    1. Good question. I didn't implement that in my code.This code takes 100 iterations.
      To 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.

      Delete
    2. Thank you sir,
      But 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 .

      Delete
    3. Thank you sir,
      But 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 .

      Delete
    4. for(k=0;k<100&&d>0.0001;k++)// while(d>=0.0001)//
      { //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

      Delete
  9. Sir, I want to know ,how iteration will convergs

    or tell me that how to stop calculation

    ReplyDelete
  10. Sir plz tell me how to plot the graph.

    ReplyDelete
  11. Sir plz tell me how to plot the graph.

    ReplyDelete
    Replies
    1. Plotted with Origin. Not using C. Sorry for the delayed reply.

      Delete