2

I need to write a MEX file to evaluate the double integral of an arbitrary function, since in Matlab the numerical integration is too slow. So far I was only able defining by hand all the possible functions that I use and parse a string in the MEX file to choose which one to integrate. This is very ugly. I would like to send to the MEX file directly the function, something like

myIntMex(@(x,y)f,0,1,0,1)

to integrate function f in the interval [0,1]x[0,1].

So far the only option I have found is mexCallMATLAB but I have also read that it is even slower than pure Matlab. I have also found a free code here but it only works for single integrals.

So my question is: how do I do it? How do I give to the MEX file the function handle? Does a code for that already exists?

Simon
  • 5,070
  • 5
  • 33
  • 59
  • Could you provide an example? To my experience the slow performance is typically caused by the function calls, not by `integral2` itself. Did you try increasing the tolerance? – Daniel Apr 04 '15 at 13:50
  • You are probably right. The functions `f` I need to integrate are very long and complex, cannot write them here. And even a simple evaluation of `f` requires a lot of time (30 sec). Increasing the tolerance works a bit, but I have to increase it a lot to obtain decent results (e.g., with `AbsTol` 1000 (a ridiculous value) it takes 40 sec). Anyway, using my (ugly) MEX files I can integrate 100 times faster than default `integral2`. – Simon Apr 04 '15 at 14:07
  • I thought about providing a string which contains the function to the MEX function, but it seems you'd be stuck there too (see this [question](http://stackoverflow.com/questions/1811456/calling-a-function-from-a-string-with-the-function-s-name-in-c)). You could probably create a C(++) file for each new function and use dynamic linking to do the integration. – hbaderts Apr 04 '15 at 14:33
  • @hbaderts Yes, this is the only solution I also could think of. However, I want to avoid creating a C file for each function. Also, if you look at the code I linked, it does exactly what I would like to do, but for only single integrals, i.e., I give to the MEX file any function handle and it computes the integral. So it seems to be possible to do what I want to do, but I have no idea how. – Simon Apr 04 '15 at 14:52
  • @Simon I believe they internally call `mexCallMATLAB`, but have no prove. When you call `GaussLegendre` with a handle to a function containing `fprintf("Test\n");`, then the `Test` appears in the MATLAB command window. The only way I can think of this to happen is if the call `mexCallMATLAB`. I tried to verify this: when calling a `fprintf` with `mexCallMATLAB` in a mex file, it is printed. So it is plausible, but I still cannot prove that they *don't* use `mexCallMATLAB`. – hbaderts Apr 04 '15 at 15:20
  • @hbaderts It is too fast to call `mexCallMATLAB`. If it does, I assume it calls `feval` to get the value of the function at a sample point, but `feval` alone takes (in my case) `0.8s`, the same amount of time of a whole run of `GaussLegendre`. Now I managed to speed everything up using `inline`, taking samples and using `trapz`, but some of my functions are too complex that `inline` gets nowhere. – Simon Apr 05 '15 at 01:07
  • 1
    @Simon you're probably right. I just found an article on [undocumented Matlab](http://undocumentedmatlab.com/blog/undocumented-matlab-mex-api) where the author of the library you linked describes some undocumented MEX features, but without going into details. It could be that he found an undocumented, significantly faster way to call function handles. – hbaderts Apr 05 '15 at 07:58
  • @hbaderts Thanks for the link, it is very interesting. Anyway, after a lot of searching it appears that what I want to do is not doable, so either I write different C files for every function (as you said) or I use `mexCallMATLAB`. – Simon Apr 07 '15 at 01:43

0 Answers0