0

I would like to solve an equation (A*x=b) with Matlab in my iOS app. I generated the C source files and added them to Xcode. The strange thing is that for the same (A,b), I get different results in Matlab and in Xcode.

I use the default settings. Any suggestion?

Matlab:

%#codegen
function X = solveEquation(A, B)
    X = linsolve(A,B);
end

Xcode:

double* A_data = malloc(num * num * sizeof(double));     
double* B_data = malloc(num * sizeof(double));
for (int i=0; i<num; i++) {
    for (int j=0; j<num; j++) {
        double sum = 0.0;
        for (int k=0; k<9; k++) {
            double diff = powf(A[i][k]-A[j][k],2); 
            sum += diff;
        }
        A_data[j*num+i] = exp(sum/-2);
    }
    B_data[i] = A[i][9];
}

int A_sizes[] = {num, num};
int B_sizes[] = {num, 1};
double* X_data = malloc(num * sizeof(double));
int X_sizes[] = {num, 1};
solveEquation(A_data, A_sizes, B_data, B_sizes, X_data, X_sizes);
Amro
  • 123,847
  • 25
  • 243
  • 454
János
  • 32,867
  • 38
  • 193
  • 353
  • What results do you get? – Bo. Jun 13 '12 at 13:03
  • Start by verifying that your A and B inputs are the same on both sides. Don't forget MATLAB indexes column-major, and C indexes row-major. Also, `powf` is the wrong function for doubles. Use `pow()`, or even better, just multiply them yourself. – Peter Jun 13 '12 at 13:06
  • `B_data[i] = A[i][9];` is indexing beyond A's (initialised) size. (and, hopefully: 9 < size.) – wildplasser Jun 13 '12 at 13:12
  • It is not in the referred code, but I write out to file the values with printf, load it as csv to matlab and use it. Yes, column-major. – János Jun 13 '12 at 13:44
  • I.e. this is the first 5 value in matlab for X: -0.3449 -3.9745 -0.3449 -0.4534 5.8243 and this is in Xcode: -21.1767 11.835 -21.1767 36.1294 -20.1915 – János Jun 13 '12 at 13:45
  • in powf you are right, I replaced it with pow, but the problem is still there – János Jun 13 '12 at 13:52
  • The generated code is simple enough. Why don't you just examine it, and see if is a "solver" in the way you expect? I'd also worry about differences in order of execution (you don't know what Matlab does when solving) and precision; is the answer different only in precision? Is your problem well-conditioned? – Ira Baxter Jun 13 '12 at 14:44
  • I am sure is not well-conditioned, there are a lot of number with the same values. That could be the reason for the high values (21.17). By the way I tried already solve the problem also with other mathematical libraries (GSL, Lis), but that was always something, singularity. Only matlab could solve it. – János Jun 13 '12 at 15:53

1 Answers1

0

How do the results differ? It's possible (even likely) that Matlab's numbers are more sophisticated that C's double data type. This might lead to differences in precision between the results.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • The funny, that I write out the matrix from Xcode in %2.3f a reduced format (normally values need more than 3 digits precision), and after it I load to Matlab. And still, matlab can solve the linear equation problem better, than static library. Residuals are much smaller. – János Jun 13 '12 at 13:36
  • @Kukoda there's nothing strange in it, if the intermediate operations are more precise in Matlab than Xcode. – Luca Geretti Jun 13 '12 at 15:51
  • You are right, this MATLAB Coder is suck, for ill-posed problem, you can't use it. – János Jun 13 '12 at 17:26