Thread: problem with matrixes

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27

    problem with matrixes

    hi !
    I am having a (probably ) simple problem which i am not able to solve

    For an interpolation function i have to fill my matrix A & B . I have declared them & then tried to allot them values.

    The problem now is that i just wanted to see ( before the values get into the interpolation function ) if the values of the matrix elements are right ... i printed out Sys_[0][0] but am getting a "junk" value. I then printed out the individual termas which make up Sys_[0][0] , i.e (double)s[2].s1 & (double) s[1].s1 ... these values are printed right. But the "calculated" value is "junk" ...

    WIll be great if anyone notices the mistake :-)
    Thx

    Code:
    
        struct Sensor 
               
    	{
    		int nr,n;
    		float d;
    		double s1,s2,s3,s4,a;
    
    	};
    ....
    static struct Sensor s[100];
    ...
    
    double Sys_[4][4]; 
    
    double b_[4];    
    
    double z_[4];   
    
    ....
    ...
    ...
    
    Sys_[0][0]=  (double)s[2].s1 -(double) s[1].s1;
    Sys_[0][1]=  (double)s[3].s1 - (double)s[1].s1;           
    Sys_[0][2]= (double) s[4].s1 - (double) s[1].s1;
    Sys_[0][3]= (double)s[5].s1 - (double)s[1].s1;
    
    
    Sys_[1][0]= (double)s[2].s2 - (double)s[1].s2;
    Sys_[1][1]= (double)s[3].s2 - (double)s[1].s2;
    Sys_[1][2]= (double)s[4].s2 - (double)s[1].s2;
    Sys_[1][3]= (double)s[5].s2 - (double)s[1].s2;
    
    
    Sys_[2][0]= (double)s[2].s3 - (double)s[1].s3;
    Sys_[2][1]= (double)s[3].s3 - (double)s[1].s3;
    Sys_[2][2]= (double)s[4].s3 - (double)s[1].s3;
    Sys_[2][3]= (double)s[5].s3 - (double)s[1].s3;
    
    
    Sys_[3][0]= (double)s[2].s4 - (double)s[1].s4;
    Sys_[3][1]= (double)s[3].s4 - (double)s[1].s4;
    Sys_[3][2]= (double)s[4].s4 - (double)s[1].s4;
    Sys_[3][3]= (double)s[5].s4 - (double)s[1].s4;
    
    
    
    
    b_[0] = (double)DataVal0  - (double)s[1].s1;
    b_[1]=  (double)DataVal10 - (double)s[1].s2; 
    b_[2]=  (double)DataVal20 - (double)s[1].s3; 
    b_[3]=  (double)DataVal30 - (double)s[1].s4;
    
    
      gj4(Sys_, z_, b_);
    Last edited by sdherzo; 07-01-2007 at 03:28 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gj4(Sys_, z_, b_);
    So what does the prototype / implementation of this function look like?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    hm ... whwn i am printing sys_[][] before the func i get the right answer , so i think i know where the prob was ... thbaks ;-)


    I do have another problem now tho ... i wated to calculate s[i ].a for 72 values ( as 72 is the number of elemnts in the structure ... have i created the for loop correct ?? i am just getting the same s[i].a values fpr all values ... for eg. s[1].a = s[34].a ... but i know it cant be the same ;-)


    Code:
     for ( i = 1 ; i <= 72 ;i++ ) 
      {
    
      s[i].a =    ( ( s[i].s1 - DataVal0) * (s[i].s1 - DataVal0 )) + ((s[i].s2 - DataVal10 )*( s[i].s2 - DataVal10 )) + ((s[i].s3 - DataVal20) * (s[i].s3-DataVal20)) + (( s[i].s4 - DataVal30 ) * ( s[i].s4 - DataVal30 ));	
       
       }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in C/C++ array indexes start at zero
    so probably
    for ( i = 0 ; i < ndata ;i++ )
    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
    Apr 2007
    Location
    Germany
    Posts
    27
    Quote Originally Posted by vart View Post
    in C/C++ array indexes start at zero
    so probably
    for ( i = 0 ; i < ndata ;i++ )
    does not seem to be the problem :-(

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    i think the problem is that DataVal is declared as static unsigned long & s[i].s1 as double . i broke up the formula & tried ( s[1].s1 - DataVal0) & ( s[23].s1 - DataVal0) i am just getting "-DataVal0" as the answer ... putting a (double) before the DataValo0 doenst help ...

    any suggestions ???

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, post more code.
    Because your attempts at guessing which lines of code are important just isn't working.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    Code:
    struct Sensor 
               
    	{
    		int nr,n;
    		float d;
    		double s1,s2,s3,s4,a;
    
    	};
    
    ...
    
    static struct Sensor s[100];
    static unsigned short DataVal, DataVal1, DataVal2, DataVal3;
        static unsigned long DataVal0, DataVal10, DataVal20, DataVal30;DataVal0 = (double)mu *  ( (double)DataVal - (double)DataVal0 ) + DataVal0;
    			DataVal10 = (double)mu * ( (double)DataVal1 - (double)DataVal10 ) + DataVal10;
    			DataVal20 = (double)mu * ( (double)DataVal2 - (double)DataVal20 ) + DataVal20;
    			DataVal30 = (double)mu * ( (double)DataVal3 - (double)DataVal30 ) + DataVal30;
    
    for ( i = 1 ; i <= 72 ;i++ ) 
      {
    
      s[i].a =    ( ( s[i].s1 - DataVal0) * (s[i].s1 - DataVal0 )) + ((s[i].s2 - DataVal10 )*( s[i].s2 - DataVal10 )) + ((s[i].s3 - DataVal20) * (s[i].s3-DataVal20)) + (( s[i].s4 - DataVal30 ) * ( s[i].s4 - DataVal30 ));	
       
       }
    & like i said the problem is with the subtraction as for eac block it just gives me DataVal0, ... back.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    on the other hand if i perform ( s[1].s1 * DataVal0) & give the answer out as &#37;ld i get the correct answer out of the multiplication ... so why doesnt ( s[1].s1 - DataVal0) work ...

  10. #10
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    ok ... also solved ;-) declarde the s1,... as float ... Thanks anyway :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 10:31 PM