Matlab generated C code does not work properly

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);

Answers


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.


Need Your Help

How do I perform a simple email/password verification in JSP using a SQL database?

java mysql database jsp email-verification

I have the code that successfully establishes a connection to a mySQL database.