Thread: a numerical method... has some error

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    Exclamation a numerical method... has some error

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    float*** ivp(float***,int,int,int,float);
    float*** bvp(float***,int,int,int,float);
    float*** solve(float***,int,int,int,float,float);
    void print_box(float***,int,int,int);
    void print(float***,int,int,int);
    
    
    void main()
    {
      float alph,h,l,***grid,***a;
      int i,j,n,p,q,r,tot_len_x,tot_len_y,count=0;
      
      printf("Enter the value of h\n");
      scanf("%f",&h);
      printf("Enter the value of alpha\n");
      scanf("%f",&alph);
      
      printf("Enter the length of the plate\n");
      scanf("%f",&tot_len_x);
      printf("Enter the width of the plate\n");
      scanf("%f",&tot_len_y);
      i=(int)((tot_len_x/h)+0.5);
      j=(int)((tot_len_y/h)+0.5);
      i=4;
      j=4;
      printf("Enter number of time steps\n");
      scanf("%d",&n);
     // printf("*****");
     // printf("aaaaa");
      grid=(float ***)malloc((n+1)*sizeof(float**));
      for(p=0;p<=n;p++)
      {
        grid[p]=(float **)malloc((i+1)*sizeof(float*));
        
      }
    // printf("aaaab");
      for(p=0;p<=n;p++)
      {
        for(q=0;q<=i;q++)
        {
        
          grid[p][q]=(float *)malloc((j+1)*sizeof(float));
        }
        count=count+q;
      }
      printf("%d",count);
    //  printf("aaaaaaa");
      a=ivp(grid,n,j,i,h);
    //  printf("%f",a[0][0][0]);
      print(a,n,j,i);
      grid=bvp(a,n,j,i,h);
      grid=solve(grid,n,j,i,h,alph);
      print_box(grid,n,j,i);
    
      fflush(stdin);
      getchar();
      
      
    }
    
    float*** ivp(float ***a,int n, int j, int i,float h)
    {
     int p,q,r;
     
     float pi;
     pi=4.0*atan(1.0);
    //printf("Entering ivp");
     for(p=0;p<=n;p++)
     {
       for(q=0;q<=i;q++)
       {
       
         for(r=0;r<=j;r++)
         {
           if(p==0)
           {
          a[p][q][r]= sin(2*pi*q*h)*sin(2*pi*r*h);
          printf("%f",sin(2*pi*q*h)*sin(2*pi*r*h));
           }
         }
         
         
      }
       
       
     }
    // printf("Exiting ivp");
     
    }
    
    float*** bvp(float ***a,int n, int j, int i,float h)
    {
      int p,q,r;
    // printf("Entering bvp");
      for(p=0;p<=n;p++)
      {
        for(q=0;q<=i;q++)
        {
          for(r=0;r<=j;r++)
          {
        if(p>0)
        {
          if(q==i||r==j)
          {
            a[p][q][r]=0;
          }
          if(q==0||r==0)
          {
            a[p][q][r]=0;
            
          }
          
        }
          }
        }
      }
    // printf("Exiting bvp");
    }
    
    
    float*** solve(float ***a,int n,int j,int i,float h,float alph)
    {
      int p,q,r;
     // printf("entering solve");
      
      for(p=0;p<=n;p++)
      {
        for(q=0;q<=i;q++)
        {
          for(r=0;r<=j;r++)
          {
        if(p>0)
        {
          if(q>0&&r>0&&q<i&&r<j)
          {
            a[p][q][r]=(1-4*alph)*a[p-1][q][r]+alph*(a[p-1][q-1][r]+a[p-1][q+1][r]+a[p-1][q][r-1]+a[p-1][q][r+1]);
          }
        }
        
          }
        }
      }
     // printf("Exiting solve");
    }
    
    void print_box(float ***a ,int n,int j,int i)
    {
      int p,q,r;
      for(p=0;p<=n;p++)
      {
        printf("        ===========\n");
        printf("        ||time=%d||\n",p);
        printf("======================================\n\n\n");
        for(q=i;q>=0;q--)
        {
          
          for(r=j;r>=0;r--)
          {
        printf("\t%f",a[p][q][r]);
          }
          printf("\n");
        }
        printf("======================================\n\n\n");
      }
      
    }
    
    void print(float ***a,int n,int j,int i)
    {
    //  printf("%f",a[0][0][0]);
      int p,q,r;
      for(p=0;p<=n;p++)
      {
        for(q=0;q<=i;q++)
        {
          for(r=0;r<=j;r++)
          {
        printf("U(%d,%d,%d) = %f",q,r,p,a[p][q][r]);
          }
        }
      }
    }

    I don't know what is the problem! Tried to debug it.. but the array is not getting printed... It's a program for solving heat equation in 2 dimension.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    I can only imagine that n has not been entered correctly.

    You need to test your diagnostic print routines separately, on known grids. If you can't print the results, then there's no way you can tell what the rest of the program is doing.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    several of your functions are prototyped as returning a value, but they don't. don't even try to run your program when you have significant compiler warnings.
    warning C4716: 'solve' : must return a value
    warning C4716: 'bvp' : must return a value
    warning C4716: 'ivp' : must return a value

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Another problem which you would have noticed if you would have read the warnings:

    Code:
    int i,j,n,p,q,r,tot_len_x,tot_len_y,count=0;
      
    printf("Enter the length of the plate\n");
    scanf("%f",&tot_len_x);
    printf("Enter the width of the plate\n");
    scanf("%f",&tot_len_y);
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WebBrowser method error in loop
    By merlol in forum C# Programming
    Replies: 0
    Last Post: 07-09-2012, 03:38 AM
  2. Replies: 8
    Last Post: 11-28-2011, 06:25 PM
  3. method call error
    By DaleZ in forum C# Programming
    Replies: 2
    Last Post: 11-19-2010, 08:46 AM
  4. Replies: 7
    Last Post: 06-21-2010, 09:20 AM
  5. Error with friend method
    By Tirno in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 01:01 AM

Tags for this Thread