Thread: problems with matrix

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

    problems with matrix

    Heys Pals,
    Currently I have the next problem with my code, because of matrix somebody can watch it and help me later thx in advance
    Code:
    float x[max],y[max],f[max][max],u[max][max],v[max][max],c[max][max];
    
    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];	
    		
    	}
    I dont know wether is valid or not because I dont get good results yet

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    How you matrixes are initialized?
    what is imax?
    what this code should do?
    what results you get?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    well this is the whole code
    Code:
    /*Laplace equation 2D*/
    /*Build the Mesh*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define max 500
    /*====================================================================
                       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=fabs(res[i][j]);
    	}
    }
    /*===================================================================
                              End resi
    =====================================================================*/
    /*===================================================================
                         Main Body
    =====================================================================*/
    
    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];	
    		
    	}  
    	
    	for(i=1;i<=imax;i++)
    	{
    		for(j=1;j<=jmax;j++)
    		printf("%f\n",u[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("%f\n",u[i][j]);*/
    		
    		}
    		/*printf("\n");*/
    	}
    	/*fclose(mesh);*/	
    	
    	exit(0);
    }
    I have problems with u[i][j], coz the print out of u[i][j] is 0's, I dont expect it

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should work on indentation

    Code:
    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));
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    I did it, but doesn't work :-((

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Indentation doesn't solve errors, it only makes the code more readable.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good indentation allows us to use our trained eyes to spot errors, much more quickly and easily.

    You will see that same benefit increase, as you program and work with correcting code, more. You are underestimating the benefit as well as the importance of using a consistent and common style.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vikingcarioca View Post
    I did it, but doesn't work :-((
    What exactly did you do?

    --
    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.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This part of your code, calculates velocities, but isn't really doing anything like what you make it look like:

    Code:
    	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];	
    		
    	}  
    	
    	for(i=1;i<=imax;i++)
    	{
    		for(j=1;j<=jmax;j++)
    		printf("%f\n",u[i][j]);
    	}
    	
    	
    
    //What it is doing is really like this:
    
    	for(i=2;i<=imax-1;i++)
    	{
    
    /*Is this ( for j=2 ) loop what you wanted, or is this an error that the other two lines aren't
       included in the loop?
    
    
       Use your eyes - Luke - they're getting trained, *feel* your indentation *flow*. Help you 
       they will in the ways of the Code. :)  
    */
    	   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];	
    		
    	}  
    	
    	for(i=1;i<=imax;i++)
    	{
    		for(j=1;j<=jmax;j++)
    		   printf("%f\n",u[i][j]);
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring the 'row' for my matrix - problems
    By qwertysingh in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 01:12 PM
  2. Simple operator overloading issue
    By Desolation in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2007, 08:56 PM
  3. Replies: 1
    Last Post: 03-08-2006, 06:47 PM
  4. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM