Forward difference method for heat equation

In summary, the Backward-Difference Method for Heat Equation approximates the solution to a parabolic partial-differential equation subject to boundary and initial conditions, using a tridiagonal linear system and Algorithm 6.7. The function F must be created before the INPUT procedure. The method requires input for the lefthand and righthand endpoints on the X-axis, the maximum value of the time variable T, and a constant ALPHA. The output includes approximations W(I,J) to u(x(I),t(J)) for each I and J, and is displayed using the OUTPUT procedure.
  • #1
ahmedo047
5
3
I don't be able to convert the following code(HEAT EQUATION BACKWARD-DIFFERENCE ALGORITHM in the Burden-faires numerical analysis book).I need HEAT EQUATION FORWARD-DIFFERENCE ALGORITHM C like following code.I don't be able to convert FORWARD-DIFFERENCE the following code .Please help me.

Code:
/*
*   HEAT EQUATION BACKWARD-DIFFERENCE ALGORITHM 12.2
*
*   To approximate the solution to the parabolic partial-differential
*   equation subject to the boundary conditions
*                  u(0,t) = u(l,t) = 0, 0 < t < T = max t,
*   and the initial conditions
*                  u(x,0) = F(x), 0 <= x <= l:
*
*   INPUT:   endpoint l; maximum time T; constant ALPHA; integers m, N.
*
*   OUTPUT:  approximations W(I,J) to u(x(I),t(J)) for each
*            I = 1, ..., m-1 and J = 1, ..., N.
*/

#include<stdio.h>
#include<math.h>
#define pi 4*atan(1)
#define true 1
#define false 0

double F(double X);
void INPUT(int *, double *, double *, double *, int *, int *);
void OUTPUT(double, double, int, double *, double);

main()
{
   double W[25], L[25], U[25], Z[25];
   double FT,FX,ALPHA,H,K,VV,T,X;
   int N,M,M1,M2,N1,FLAG,I1,I,J,OK;

   INPUT(&OK, &FX, &FT, &ALPHA, &N, &M);
   if (OK) {
      M1 = M - 1;
      M2 = M - 2;
      N1 = N - 1;
      /* STEP 1 */
      H = FX / M;
      K = FT / N;
      VV = ALPHA * ALPHA * K / ( H * H );
      /* STEP 2 */
      for (I=1; I<=M1; I++) W[I-1] = F( I * H );
      /* STEP 3 */
      /* STEPS 3 through 11 solve a tridiagonal linear system
         using Algorithm 6.7 */
      L[0] = 1.0 + 2.0 * VV;
      U[0] = -VV / L[0];
      /* STEP 4 */
      for (I=2; I<=M2; I++) {
         L[I-1] = 1.0 + 2.0 * VV + VV * U[I-2];
         U[I-1] = -VV / L[I-1];
      } 
      /* STEP 5 */
      L[M1-1] = 1.0 + 2.0 * VV + VV * U[M2-1];
      /* STEP 6 */
      for (J=1; J<=N; J++) {
         /* STEP 7 */
         /* current t(j) */
         T = J * K;
         Z[0] = W[0] / L[0];
         /* STEP 8 */
         for (I=2; I<=M1; I++)
            Z[I-1] = ( W[I-1] + VV * Z[I-2] ) / L[I-1];
         /* STEP 9 */
         W[M1-1] = Z[M1-1];
         /* STEP 10 */
         for (I1=1; I1<=M2; I1++) {
            I = M2 - I1 + 1;
            W[I-1] = Z[I-1] - U[I-1] * W[I];
         } 
      }
      /* STEP 11 */
      OUTPUT(FT, X, M1, W, H);
   }
   /* STEP 12 */
   return 0;
}

/* Change F for a new problem */
double F(double X)
{
   double f;

   f =  sin(pi * X);
   return f;
}

void INPUT(int *OK, double *FX, double *FT, double *ALPHA, int *N, int *M)
{
   int FLAG;
   char AA;

   printf("This is the Backward-Difference Method for Heat Equation.\n");
   printf("Has the function F been created immediately\n");
   printf("preceding the INPUT procedure? Answer Y or N.\n");
   scanf("\n%c", &AA);
   if ((AA == 'Y') || (AA == 'y')) {
      printf("The lefthand endpoint on the X-axis is 0.\n");
      *OK =false;
      while (!(*OK)) {
         printf("Input the righthand endpoint on the X-axis.\n");
         scanf("%lf", FX);
         if (*FX <= 0.0)
            printf("Must be positive number.\n");
         else *OK = true;
      } 
      *OK = false;
      while (!(*OK)) {
         printf("Input the maximum value of the time variable T.\n");
         scanf("%lf", FT);
         if (*FT <= 0.0)
            printf("Must be positive number.\n");
         else *OK = true;
      } 
      printf("Input the constant alpha.\n");
      scanf("%lf", ALPHA);
      *OK = false;
      while (!(*OK)) {
         printf("Input integer m = number of intervals on X-axis\n");
         printf("and N = number of time intervals - separated by a blank.\n");
         printf("Note that m must be 3 or larger.\n");
         scanf("%d %d", M, N);
         if ((*M <= 2) || (*N <= 0))
            printf("Numbers are not within correct range.\n");
         else *OK = true;
      } 
   }  
   else {
      printf("The program will end so that the function F can be created.\n");
      *OK = false;
   }  
}

void OUTPUT(double FT, double X, int M1, double *W, double H)
{
   int I, J, FLAG;
   char NAME[30];
   FILE *OUP;

   printf("Choice of output method:\n");
   printf("1. Output to screen\n");
   printf("2. Output to text file\n");
   printf("Please enter 1 or 2.\n");
   scanf("%d", &FLAG);
   if (FLAG == 2) {
      printf("Input the file name in the form - drive:name.ext\n");
      printf("for example:   A:OUTPUT.DTA\n");
      scanf("%s", NAME);
      OUP = fopen(NAME, "w");
   }
   else OUP = stdout;
   fprintf(OUP, "THIS IS THE BACKWARD-DIFFERENCE METHOD\n\n");
   fprintf(OUP, "  I        X(I)    W(X(I),%12.6e)\n", FT);
   for (I=1; I<=M1; I++) {
      X = I * H;
      fprintf(OUP, "%3d %11.8f    %14.8f\n", I, X, W[I-1]);
   }
   fclose(OUP);
}
 
Physics news on Phys.org
  • #2
Isn't there anyone to help me.

if I write VV = - [ALPHA * ALPHA * K / ( H * H )]; instead of VV = ALPHA * ALPHA * K / ( H * H );in the HEAT EQUATION BACKWARD-DIFFERENCE ALGORITHM then have I obtained HEAT EQUATION FORWARD-DIFFERENCE ALGORITHM?
 
  • #3
problem is solved:
finally code
Code:
#include<stdio.h>
#include<math.h>
#define pi 4*atan(1)
#define true 1
#define false 0

double F(double X);
void INPUT(int *, double *, double *, double *, int *, int *);
void OUTPUT(double, double, int, double *, double);

main()
{
  double W[25], U[25], Z[25];
  double FT,FX,ALPHA,H,K,VV,T,X;
  int N,M,M1,M2,N1,FLAG,I1,I,J,JJ,OK;

  INPUT(&OK, &FX, &FT, &ALPHA, &N, &M);
  if (OK) {
     M1 = M - 1;
     M2 = M - 2;
     N1 = N - 1;
     /* STEP 1 */
     H = FX / M;
     K = FT / N;
     VV = ALPHA * ALPHA * K / ( H * H );
     /* STEP 2 */
W[M]=0;
W[0]=0;
     for (I=1; I<=M1; I++) W[I] = F( I * H );
     /* STEP 3 */
for(J=1;J<=N;J++)
{ T=J*K;
    for (I=1;I<=M1;I++)
U[I]=VV*W[I+1]+(1-2*VV)*W[I]+VV*W[I-1];
for(JJ=1;JJ<=M1;JJ++)
W[JJ]=U[JJ];
}
     /* STEP 11 */
     OUTPUT(FT, X, M1, U, H);
  }
  /* STEP 12 */
  return 0;
}

/* Change F for a new problem */
double F(double X)
{
  double f;

  f =  sin(pi * X);
  return f;
}

void INPUT(int *OK, double *FX, double *FT, double *ALPHA, int *N, int *M)
{
  int FLAG;
  char AA;

  printf("This is the Backward-Difference Method for Heat Equation.\n");
  printf("Has the function F been created immediately\n");
  printf("preceding the INPUT procedure? Answer Y or N.\n");
  scanf("\n%c", &AA);
  if ((AA == 'Y') || (AA == 'y')) {
     printf("The lefthand endpoint on the X-axis is 0.\n");
     *OK =false;
     while (!(*OK)) {
	printf("Input the righthand endpoint on the X-axis.\n");
	scanf("%lf", FX);
	if (*FX <= 0.0)
	   printf("Must be positive number.\n");
	else *OK = true;
     }
     *OK = false;
     while (!(*OK)) {
	printf("Input the maximum value of the time variable T.\n");
	scanf("%lf", FT);
	if (*FT <= 0.0)
	   printf("Must be positive number.\n");
	else *OK = true;
     }
     printf("Input the constant alpha.\n");
     scanf("%lf", ALPHA);
     *OK = false;
     while (!(*OK)) {
	printf("Input integer m = number of intervals on X-axis\n");
	printf("and N = number of time intervals - separated by a.\n");
	printf("Note that m must be 3 or larger.\n");
	scanf("%d %d", M, N);
	if ((*M <= 2) || (*N <= 0))
	   printf("Numbers are not within correct range.\n");
	else *OK = true;
     }
  }
  else {
     printf("The program will end so that the function F can be\n");
     *OK = false;
  }
}

void OUTPUT(double FT, double X, int M1, double *W, double H)
{
  int I, J, FLAG;
  char NAME[30];
  FILE *OUP;

  printf("Choice of output method:\n");
  printf("1. Output to screen\n");
  printf("2. Output to text file\n");
  printf("Please enter 1 or 2.\n");
  scanf("%d", &FLAG);
  if (FLAG == 2) {
     printf("Input the file name in the form - drive:name.ext\n");
     printf("for example:   A:OUTPUT.DTA\n");
     scanf("%s", NAME);
     OUP = fopen(NAME, "w");
  }
  else OUP = stdout;
  fprintf(OUP, "THIS IS THE BACKWARD-DIFFERENCE METHOD\n\n");
  fprintf(OUP, "  I        X(I)    W(X(I),%12.6e)\n", FT);
  for (I=1; I<=M1; I++) {
     X = I * H;
     fprintf(OUP, "%3d %11.8f    %14.8f\n", I, X, W[I]);
  }
  fclose(OUP);
}
 

1. What is the Forward Difference Method for solving the Heat Equation?

The Forward Difference Method is a numerical approach used to approximate solutions to the Heat Equation, which is a partial differential equation that describes the distribution of heat in a given space over time. This method involves discretizing the space and time variables and using finite differences to approximate the derivatives in the equation.

2. How does the Forward Difference Method work?

The Forward Difference Method works by dividing the space and time variables into small intervals and using the values at these intervals to approximate the derivatives in the Heat Equation. This creates a system of equations that can be solved iteratively to obtain a numerical solution to the equation.

3. What are the advantages of using the Forward Difference Method?

One advantage of the Forward Difference Method is that it is relatively easy to implement and does not require advanced mathematical knowledge. It is also a computationally efficient method and can handle complex boundary conditions. Additionally, it is a stable method, meaning that small changes in the input values will not greatly affect the output.

4. What are the limitations of the Forward Difference Method?

The Forward Difference Method is subject to truncation error, meaning that the approximation of the derivatives may not be entirely accurate. This method also requires a small time step to ensure accuracy, making it computationally expensive for large time intervals. Additionally, it is only suitable for simple geometries and may not be applicable to more complex systems.

5. How is the accuracy of the Forward Difference Method evaluated?

The accuracy of the Forward Difference Method can be evaluated by comparing the numerical solution obtained using this method with an analytical solution, if one exists. Additionally, the truncation error can be estimated and used to determine the accuracy of the approximation. The smaller the truncation error, the more accurate the solution will be.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
873
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
2
Views
923
  • Introductory Physics Homework Help
Replies
11
Views
758
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
986
Back
Top