35605

MATLAB: MEX matrix division gives different result than m-file

I've used MATLAB's coder tool to create a MEX version of the matrix exponential function, to be used in another set of functions. The issue is, the MEX version gives different results than the original m-file.

After debugging it, I believe that the reason this is, is because the MEX file and the m-file do not do matrix division (\) the same. Or the MEX file has issues with it in the first place. All the variables leading up to the line where the matrix division occurs are equivalent on both sides.

This is the line where the issue occurs:

F = (V-U)\(2*U) + I

Where I is the identity matrix of the size of V and U.

What is the reason behind the discrepancy when a MEX file does matrix division, and how can I fix this issue? Can this line of code be re-written without the division?

Answer1:

I have no problem generating C code from such an operation.

Here is a test function I tried:

myfcn.m

function F = myfcn(U,V) I = eye(size(U)); F = (V-U)\(2*U) + I; end

here's a test script we'll use to verify the results:

test_myfcn.m

U = rand(5); V = rand(5); F = myfcn(U,V);

I start by launching the code generation tool (ccoder), create a new project set to produce a MEX-file, then add the myfnc.m function from before as entry-point. Then I define both input variables types as:

double (:Inf x :Inf)

which specifies an MxN matrix of unbounded size of type double.

Finally we can build the project. This produces myfcn_mex.mexw64.

Testing both the original M-function and the generated MEX-function, I get pretty much the same result (difference is in the order of machine epsilon):

>> F = myfnc(U,V); >> FF = myfcn_mex(U,V); >> norm(F-FF) ans = 1.4391e-14

Answer2:

MATLAB made a minor change to the EXPM algorithm in the 2014a release. MATLAB Coder implementations are separate, and the corresponding change has not yet been made in the algorithm for code generation, so some discrepancies are possible there. The new implementation does (V - U)\(2*U) + I, whereas the old one does (V - U)\(V + U). These are mathematically equivalent but will give different round-off behaviors in general.

AFAIK, there is no systematic difference in the quality of solutions to linear systems in MATLAB versus MATLAB Coder. The core algorithms are essentially equivalent, with round-off differences expected to creep in from various obscure sources. The residual may be smaller for either MATLAB or MATLAB Coder in a given case. If the difference in the solutions is large, it indicates that the problem being solved is ill-conditioned. I can explain more about this if you like, but it is covered in every numerical analysis textbook. Can you provide a concrete example? At least can you find out what cond(V - U) returns there when your problem is solved in MATLAB?

Recommend

  • Dismiss keyboard of TextField programmatically
  • page-object gem seems not working
  • page-object gem seems not working
  • How should I store twitter token so users don't have to go to twitter_oauth everytime?
  • How to use WSO2 AM to call my backend services that are secured with OATH
  • training UBM with sidekit from custom data
  • Libspotify playing problems
  • Passing variable to WinBugs model in R
  • Cannot declare variable at all with Windows Driver Kit build environment
  • Visual Studio 2012 doesn't build project on run when its dependencies change
  • NSWorkspace launchApplication: fails for the first time with root user
  • How do I stop js files being cached in IE?
  • How to generate random events in android?
  • Yii: any way to save the images in compressed form?
  • Find unique tuples in a relation represented by a BDD
  • jQuery Mobile - Dialogs without changing hash
  • How to implement simple validation in Scala
  • Is WITH the replacement for a #TEMP table?
  • Skip Characters in Oracle TO_DATE function
  • Refactoring advice: maps to POJOs
  • Regex for nested values
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • What is the default HTTP verb in WebApi ? GET or POST?
  • Inversing an interpolation of rotation
  • Aptana 3 remove bundle (jquery)
  • TFS 2015 - Waiting for an agent to be requested
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Object and struct member access and address offset calculation
  • uniform generation of points on 3D box
  • Email verification using google app script and google forms
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • Importing jscolor library in angular 2
  • RestKit - RKRequestDelegate does not exist
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • A cron job substitute?
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • How can I use threading to 'tick' a timer to be accessed by other threads?