Double integration using MEX - matlab

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?

Related

How can I import a function of Octave to MATLAB?

I am running my code in Matlab. But I also want to call a function in octave. How should I import the qp function of Octave to Matlab?
The Octave language is a superset of the Matlab language. If qp only made use of the Matlab language, then you could simply add it to your Matlab path and be done with it.
However, Octave's qp makes extensive use of Octave only language so you basically have to port the code yourself. There are no tools for this, you have to convert the code from one language to another. In addition, the actual solver is the function __qp__ which is written in C++ and uses liboctave.
Two easier alternatives than porting qp are:
save the data from your Matlab session into a file save foo.mat mydata, call Octave to do the work and save the results system ('octave --eval ''load ("foo.mat"); qp (...); save foo.mat ...;', and read the file back load foo.mat.
or the much simpler alternative, just use Octave.
Octave syntaxt is not totally compatible with MATLAB. For example the preferred syntax for defining function in Octave is like this:
function ret = f()
%do something
endfunction
but MATLAB doesn't accept that syntax and there are other differences like differences in calling native codes and ... so it is not simple to convert each statement of octave library to matlab or convert oct c++ source to mex.
A straightforward way is that you should have an installation of Octave and run octave script from it then save the results to a mat file and in MATLAB load the file . You may use system function to execute octave or run it from shell.
so lets say you have 2 files in the same directory. a.m and b.m In the script b.m if you type a as a line of code everything in a.m will happen (variable assignments function definitions computations etc...)
Additionally you can use the import statement for adding things to your import list. as seen here.

How do I convert Mathematica code into Matlab?

I'm just starting to learn MatLab.
This is what I'd like to convert into MatLab code:
http://postimg.org/image/jqdvcrbod/
Since a lot of my variables are functions or other variables, does that mean that when I write them as functions in MatLab I have to save each of the functions as a separate file? Is there any other way? I don't want to end up with a million separate files, but as of right now, if I write more then one function in the editor, it starts getting confused and doesn't recognize the second function.
Also, is there a way to use actual symbols (like the square root symbol instead of writing "sqrt()") like in Mathematica? I feel like long equations (like the last one) are easier on the eye that way, but that's purely aesthetics.
Is there a way to have MatLab output unassigned variables? Like in Mathematica, if I have y(x) = 2x, if i don't put anything for x, it just outputs 2x.
One quick way to do is use a package in Mathematica called ToMatlab (I attached the download link). It is able to convert Mathematica symbol syntax to Matlab .m file.
Hope this would help.

Call dll function from matlab

I have an m file from which I use to create dll using the Matlab deploytool. the code simply reads as:
function hello
disp('Hello')
end
there are six functions in the compiled dll exported as:
uint8 helloInitialize
[uint8, voidPtr, voidPtr] helloInitializeWithHandlers(voidPtr, voidPtr)
helloPrintStackTrace
helloTerminate
uint8 mlfHello
[uint8, MATLAB arrayPtr, MATLAB arrayPtr] mlxHello(int32, MATLAB arrayPtr, int32, MATLAB arrayPtr)
Now I want to run this dll from my matlab command window using calllib and use
the hello function. Assuming that I use the correct function mlfHello, calllib('hello','mlfHello') gives me nothing. Please advise me on what function to call and how to do it?
I'm not 100% its still the case but it certainly used to be that you couldn't load DLL's which were created in Matlab back into Matlab.
I suspect its still the case - so you cant do what your trying to do.
[edit] I dont have a link because they dont like to advertise the fact. The reason AFAIK is to avoid users compiling toolbox capability into DLL and giving to others to use in Matlab without a toolbox license.

Is there a GNU Octave equivalent for the Matlab function "fit"?

My teacher in the signal analysis course has given me some Matlab code that I have to execute in order to complete a home assignment. I have always been using GNU Octave without troubles, but this time there is this command that is giving me headaches.
[c8,g8]=fit(time, sin_4_harmonic,’fourier8’)
I cannot find the function "fit" in GNU Octave, which is referenced for Matlab at the following url http://www.mathworks.se/help/curvefit/fit.html
Does anyone knows which package should I load, or if there is any equivalent?
Thanks =)
As far as I know, that function is not present in any of the Octave packages. However, the best place to look for something similar would be the optim package, probably the function nonlin_curvefit. Looking at the documentation, the model fourier8 is of the type Y = a0+a1*cos(x*p)+b1*sin(x*p)... +a8*cos(8*x*p)+b8*sin(8*x*p).
A work-around may be using "polyfit" function. To get the values, use "polyval".

how can this function "resizeColumnscore" resizes image?

I want to know how can this function(from MATLAB) resize the columns of an input image using weights an indices previously computed.
Which equations uses to do that?
resizeColumnsCore(double(in), weights', indices');
When I looked for a function called resizeColumnsCore in MATLAB 7.11.0 (R2010b) I didn't find anything. However, I did find a MEX-file by that name in MATLAB 7.8.0 (R2009a) in this subdirectory of the Image Processing Toolbox:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\
I guess they've phased it out or replaced it with another function in newer MATLAB versions. Now, if you want to know what the MEX-file does, you need to look at the source code it is compiled from. Luckily, it appears that this source code resizeColumnsCore.cpp can be found in the following directory:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\src\misc\
And you can look through that code to determine the algorithms used to resize the columns of an image given a set of weights and indices.
Now, if you want to know how these input arguments to resizeColumnsCore are computed, you'll have to look at the code of a function that calls it. I know of at least one function in the IPT that calls this function: IMRESIZE. If you type edit imresize at the command prompt it will open that function in the Editor, allowing you to look through the code so you can see how the arguments to resizeColumnsCore are created.
What I can tell you for R2009a is that there is a subfunction in the file imresize.m called contributions which computes the weights and indices that are ultimately passed as arguments to resizeColumnsCore. That is where you will want to start looking to determine what algorithms are used to compute these arguments.
Looks like this isn't a proprietary MATLAB function. Could we see some code or a link to the code?