Thread: print ut my results

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    print ut my results

    Hi pals,
    My problem is I cant print out my results, I tried a lot of things but unfortunately I didn't get help me please
    follow my code
    Code:
    /*Laplace equation 2D*/
    /*Build the Mesh*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define max 100
    /*====================================================================
                       Defining function VOID
    ======================================================================*/
    /*====================================================================
                         Function Contour
    ======================================================================*/
        void contour(float f[max][max],float x[max],float y[max],int jmax, int imax, float ui,
    	 int i, int j, float il,float it)
    	{
    		
    	for(j=1;j<=jmax;j++)
    	{
    		f[1][j]=ui*x[1];
    		f[imax][j]=ui*x[imax];
    	}
    	for(i=1;i<=imax;i++)
    	{
    		f[i][jmax]=ui*x[i];
    		if((i>=il) && (i<=it))
    		f[i][1]=f[i][2]-0.05*(y[2]-y[1])*(1.0-2*x[i]);
    		else
    		f[i][1]=f[i][2];
    	}
    	}// end contour ()
    /*===================================================================
                             end VOID
    =====================================================================*/
    /*====================================================================
                          Function Res
    ======================================================================*/
    void resi(float res[max][max],float x[max],float y[max],float f[max][max],int i, int j,
    int imax, int jmax, float remax)	
    {
    	for (i=2;i<=imax-1;i++)
    	{
    		for(j=2;j<=jmax-1;j++)
    		res[i][j]=(2/(x[i+1]-x[i-1]))*((f[i+1][j]-f[i][j])/(x[i+1]-x[i])-(f[i][j]-f[i-1][j])/(x[i]-x[i-1]))
    		+(2/(y[j+1]-y[j-1]))*((f[i][j+1]-f[i][j])/(y[j+1]-y[j])-(f[i][j]-f[i][j-1])/(y[j]-y[j-1]));
    	}
    	remax=0;
    	for(i=2;i<=imax-1;i++)
    	{
    		for(j=2;j<=jmax-1;j++)
    		if (fabs(res[i][j])>=remax)
    		remax=abs(res[i][j]);
    	}
    }
    /*================================================================
                              End resi
    ==================================================================*/
    
    
    int 
    main()
    {
    	FILE *mesh;
    	float dx,x[max],y[max],f[max][max],u[max][max],v[max][max],c[max][max],df[max][max],res[max][max];
    	float ys,xs,il,it,ui,dy,remax;
    	
    	int imax,jmax,nmax,n,i,j,s;
    	mesh=fopen("mesh.data","w");
    	
    	il=11;
    	it=31;
    	imax=41;
    	jmax=12;
    	ys=1.24;
    	xs=1.24;
    	ui=1.0;
    	nmax=10000;
    	/*=========================================================
    	               Generating the Mesh
    	===========================================================*/
    	dx=-1.0/(il-it);
    	
    	for (i=il;i<=it;i++)
    	{
    		x[i]=(i-il)*dx;
    		
    	}
    	for (i=it+1;i<=imax;i++)
    	{
    		x[i]=x[i-1]+(x[i-1]-x[i-2])*xs;
    		
    	}
    	for(i=il-1;i>=1;i--)
    	{
    	    x[i]=x[i+1]+(x[i+1]-x[i+2])*xs;
    	    
    	}
    	y[1]=-dx/2;
    	y[2]=dx/2;
    	for(j=3;j<=jmax;j++)
    	{
    	    y[j]=y[j-1]+(y[j-1]-y[j-2])*ys;
    	}
    	/*===============================================================*/
    
    	/*===============================================================
    	                   Initial Conditions
    	=================================================================*/
    	for(i=1;i<=imax;i++)
    	{
    		for(j=1;j<=jmax;j++)
    		f[i][j]=ui*x[i];
    		
    		
    	}
    	/*============================================================*/
    	
    	for(n=1;n<=nmax;n++)
    	{
            contour( f, x, y,jmax,imax, ui,i, j, il,it);
    		resi( res,x,y,f,i,j,imax,jmax,remax);
       
       for(i=2;i<=imax-1;i++)
       {
       	for(j=2;j<=jmax-1;j++)
       	dx=(x[i+1]-x[i-1])/2;
       	dy=(y[j+1]-y[j-1])/2;
       	df[i][j]=-res[i][j]/(-2/(dx*dx)-2/(dy*dy));
       }
       for(i=2;i<=imax-1;i++)
       {
       	for(j=2;j<=jmax-1;j++)
       	f[i][j]=f[i][j]+df[i][j];
       }
       
    	}	
    	
    	
    	/*=============================================================*/
    	/*===============================================================
    	                Calculus of Velocities
    	=================================================================*/
    	for(i=2;i<=imax-1;i++)
    	{
    		for(j=2;j<=jmax-1;j++)
    		u[i][j]=(f[i+1][j]-f[i-1][j])/(x[i+1]-x[i-1]);
    		v[i][j]=(f[i][j+1]-f[i][j-1])/(y[j+1]-y[j-1]);
    		c[i][j]=1.0-u[i][j]*u[i][j]+v[i][j]*v[i][j];
    		
    	}  
    	
    	
    	/*===============================================================
                       	Print the Results
    	==================================================================*/
    	for(j=1;j<=jmax;j++)
    	{
    		for(i=1;i<=imax;i++)
    		/*fprintf(mesh,"%1.15f  %1.15f\n",u[i][j],v[i][j]);*/
    		printf("%lf\n",u[i][j]);
    		printf("\n");
    	}
    	fclose(mesh);	
    	
    	exit(0);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you explain exactly what your problem with printing the result is?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Hi, yeah sorry for my bad explanation, well in the section print results i would like to print out the velocities u[i][j] and v[i][j], but I did't get

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vikingcarioca View Post
    Hi, yeah sorry for my bad explanation, well in the section print results i would like to print out the velocities u[i][j] and v[i][j], but I did't get
    Didn't get what? What are you seeing, and how is that different from what you exect?

    Also, is "%lf" really the right format specifier?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    well I am seeing the results of u[i][j] as 0's, means is wrong when I print out u[i][j], and i hink format %lf is correct. I am new in C not an expert

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as your values are "float", I don't see how "%lf" could possibly be even nearly right - it either is undefined, means long double, or double, depending on which year the printf() code was produced.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    so what i need is to change float for double is this correct ?

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    One thing: seeing your code I'd recommend doubles instead of floats. Except you only need 6 decimal digits. But in float one can quickly use some accuracy. It's your decision. If you use floats, change %lf to %f.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Randomly!
    By davewang in forum C Programming
    Replies: 3
    Last Post: 12-09-2008, 09:04 AM
  2. constantly print an array to the screen?
    By .exe in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 10:41 PM
  3. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  4. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 09:45 AM
  5. How to print out the data throw printer.Attn Salem
    By Jason in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 05:58 AM