text: /* Code for demonstrating boundary value problem for various Backward Difference Scheme for grid size of 0.01*/
  
  #include <stdio.h>
  #include <time.h>
  #include <math.h>
  
  int Thomas_Algorithm(int n, float a[n - 1][n - 1], float b[n - 1][1])
  {
      float x[n - 1][1], ratio; /* Matrix X and ratio, needed for matrix transformation */
  
      int i, j, k; /* Loop control variables */
  
      FILE *fptr = fopen("Value_0.01_BDS.csv", "w");
      fprintf(fptr, "Node_Number,phi_Value\n");
      fprintf(fptr, "0,1.0\n");
  
      for (i = 0; i < n - 2; i++) /* Loop for forward elimnation */
      {
          ratio = a[i + 1][i] / a[i][i];
          a[i + 1][i + 1] -= ratio * a[i][i + 1];
          b[i + 1][0] -= ratio * b[i][0];
      }
  
      for (i = n - 2; i >= 0; i--) /* Loop for backward substitution */
      {
          if (i == n - 1)
          {
              x[i][0] = b[i][0] / a[i][i];
          }
          else
          {
              x[i][0] = b[i][0];
              x[i][0] -= a[i][i + 1] * x[i + 1][0];
              x[i][0] = x[i][0] / a[i][i];
          }
      }
  
      for (i = 0; i < n - 1; i++) /* Loop for printing Matrix X */
      {
          printf("phi_%d : %f units \n", i + 1, x[i][0]);
          fprintf(fptr, "%d,%f\n", i + 1, x[i][0]);
      }
  
      fprintf(fptr, "%d,0\n", n);
      fclose(fptr);
  }
  
  int main()
  {
      clock_t begin = clock(); /*Clock staring from this line*/
  
      float tau = 0.1, delx = 0.01, alpha, beta, gamma;
      float phi_start = 1, phi_end = 0; /* Boundary Values */
  
      int n = 1 / delx, i, j; /* No. of cells */
  
      float A[n - 1][n - 1], B[n - 1][1]; /* Matrix Declaration */
  
      alpha = tau;
      beta = delx - 2 * tau - delx*delx;
      gamma = tau - delx;
  
      for (i = 0; i < n - 1; i++) /* Matrix A and B Initialization */
      {
          if (i == 0)
          {
              A[i][i] = beta;
              A[i][i + 1] = alpha;
              B[i][0] = -gamma * phi_start;
          }
          else if (i == n - 2)
          {
              A[i][i - 1] = gamma;
              A[i][i] = beta;
              B[i][0] = -alpha * phi_end;
          }
          else
          {
              A[i][i - 1] = gamma;
              A[i][i] = beta;
              A[i][i + 1] = alpha;
              B[i][0] = 0;
          }
      }
  
      Thomas_Algorithm(n, A, B);
  
      clock_t end = clock();                                                               /* Clock Ends here */
      double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;                          /* Calculating time taken for code to execute */
      printf("Time taken for code execution : %0.6f milli seconds.\n", 1000 * time_spent); /* Printing the time taken */
  }