0

I am trying to convert Matlab code into c code, I verifying each step with Matlab result. everything matches except for subtraction the error scale is 1e-14 but due iterations it affects the whole answer.

Matlab:

 s = c-A'*y;

c code:

 for (i=0;i<A_cols;i++)
{
 s[i][0] = c[i][0]-Temp_Mat_0[i][0];
}

I have done this (A'*y) aside and matched it to Matlab code

I entered the same values for both, but the are saved or displayed as follows: Values are:

  A_example[16]= {8,1,1,2,-1,5,23,7,5,8,10,1,12,-1,5,23};
  b_example[16]= {5.3326,12.8165,10.0277,19.4862};
  c_example[16]= {13.3215,4.0611,24.2008,27.7622};

c code: saves as as

enter image description here

output for s:

enter image description here

Matlab Output is:

K>> full(A)

ans =

     8     1     1     2
    -1     5    23     7
     5     8    10     1
    12    -1     5    23

K>> full(b)

ans =

    5.3326
   12.8165
   10.0277
   19.4862

K>> full(c)

ans =

   13.3215
    4.0611
   24.2008
   27.7622

s =

   1.0e-13 *

   -0.0355
    0.1599
   -0.1421
    0.0711

this error in addition to other errors based on subtraction makes my c code calculations drifting very far! is there any way to make both codes work with the same precision?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Karam Abo Ghalieh
  • 428
  • 1
  • 5
  • 19
  • I am using double for everything – Karam Abo Ghalieh May 16 '17 at 05:08
  • 3
    Please take a look at the discussion of floating point arithmetic in the linked duplicate. Your errors appear to be at the limit of machine precision for double precision variables, and the differences likely arise from differences in the order of operations between the two solutions. – gnovice May 16 '17 at 05:09

0 Answers0