is there a faster version of fminbnd in matlab? - matlab

I am now using fminbnd in Matlab, and I find it relatively slow (I am using it inside a nested loop). The function itself, its interface and the values it returns are great, but when looking into the .m file I see it is not optimized. As a matter of fact, I was hoping for something like that to be written as a mex.
Anyone knows of an alternative to fminbnd that works much faster and does not have as much overhead?

It's written like that because it has to evaluate (feval) your user-defined function(s) on every iteration. Matlab's ODE solvers work in the same way. In current Matlab it's costly for a C/C++ code to call a user-defined Matlab function and read in its return values iteratively.
Make sure you're using the options correctly, that fminbnd is the correct tool (maybe a simpler scheme would be better or, since this in a loop, maybe a multi-dimensional method like fminsearch would be more appropriate), and have optimized your objective function. The next easiest thing would be to try compiling your Matlab code to C or C++ (see codgen). You'll likely need to compile in your objective function, and all of the options, as well in order to avoid the slowdown issues mentioned above. I've not tried this for fminbnd, but I did see mention of it working online. If your objective function itself is complicated, you could try just converting it to a mex function.
fminbnd is based on Brent's method. You can find C, C++, and FORTRAN code for that here. The GSL also has a version: gsl_min_fminimizer_brent.

Related

Does Octave have a ScaleProblem equivalent for fmincon?

I'm in the process of porting a large set of MATLAB scripts to Octave. Some of the scripts use the MATLAB Optimization toolbox, specifically the fmincon function. In the optim package in Octave, the fmincon function exists but has different parameters. Is there a way to replicate the ScaleProblem parameter in Octave's fmincon?
In my Octave script, I use optimset:
options = optimset('fmincon','Algorithm','sqp','ScaleProblem','obj-and-constr', ...);
Which causes the following warning:
warning: optimset: unrecognized option: ScaleProblem
Is there a workaround for this?
Apart from the trivial advise to write a wrapper that looks for this parameter-value pair, scales the matrices & calls fmincon without the ScaleProblem option, I can only emphasize to use the NLopt-toolbox from the MIT: https://nlopt.readthedocs.io/en/latest/ it got far more optimization algorithms with a neat matlab/octave interface, all open-source, and -- to my experience -- often more accurate and faster indeed =)

Why is Matlab function interpn being modified?

The Matlab function interpn does n-grid interpolation. According to the documentation page:
In a future release, interpn will not accept mixed combinations of row and column vectors for the sample and query grids.
This page provides a bit more information but is still kind of cryptic.
My question is this: Why is this modification being implemented? In particular, are there any pitfalls to using interpn?
I am writing a program in fortran that is supposed to produce similar results to a Matlab program that uses interpn as a crucial component. I'm wondering if the Matlab program might have a problem that is related to this modification.
No, I don't think this indicates that there is any sort of problem with using interpn, or any of the other MATLAB interpolation functions.
Over the last few releases MathWorks has been introducing some new/better functionality for interpolation (for example the griddedInterpolant, scatteredInterpolant and delaunayTriangulation classes). This has been going on in small steps since R2009a, when they replaced the underlying QHULL libraries for computational geometry with CGAL.
It seems likely to me that interpn has for a long time supported an unusual form of input arguments (i.e. mixed row and column vectors to define the sample grid) that is probably a bit confusing for people, hardly ever used, and a bit of a pain for MathWorks to support. So as they move forward with the newer functionality, they're just taking the opportunity to simplify some of the syntaxes supported: it doesn't mean that there is any problem with interpn.

Diagonalization hermitian matrices julia vs fortran

I have a program written in Fortran and in Julia, one of the cases I have symmetric matrices
and I get results more or less similar with both programs. When I switch to a case where I have hermitian matrices, the program in Julia and the program in Fortran give me different stuff. I would guess that maybe the difference comes from the diagonalization procedure, in Fortran I use:
ZHEEVD(..)
while in Julia I simply use:
eig(matrix)
The first thing that I notice is that ZHEEVD fixes the first row of the eigenvector matrices to real numbers (no imaginary part), while eig fixes the last row to real numbers.
Any idea how to overcome this tiny differences? Any more info that can be useful when dealing with julia's linear algebra built-ins?
Digging in to the Julia methods (the #less macro is very handy for this), you'll find that it eventually calls the LAPACK.syevr! method, which in the Complex128 case is a wrapper for the ZHEEVR LAPACK method (scroll down a bit to see the actual definition).
If you'd prefer to keep using ZHEEVD, you can access it via the ccall interface: see the manual section on Calling C and Fortran code. The LAPACK wrappers linked above should provide plenty of examples (LAPACK comes as part of OpenBLAS, which is included in Julia, so you shouldn't need to install anything else).

Gaussian hypergeometric function 2F1

I would like to know if there is any available Gaussian hypergeometric function (2F1 function) code for Matlab.
I perfectly know that in the newest Matlab releases there is the hypergeom function, but it works really slow.
Therefore I was wondering about the existance of any mex function or whatever similar code performing what hypergeom does.
I thank you all in advance for support.
Best regards,
Francesco
The GNU Scientific Library implements hypergeometric functions including 2F1. You shouldn't have too much trouble wrapping that inside a mex-file.
I expect you'll find other sources knocking around on the Internet too.
Do report back and let us know if it does work faster than the intrinsic function.
After googleing a bit in the Internet, I came up with this tool provided within the Mathworks File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/35008-generation-of-random-variates/content/pfq.m
It consists of 1900 distributions, and among them the Gaussian hypergeometric function 2F1.
Furthermore, it has better performances than the standard hypergeom function.

MATLAB Mex files

Is there a way to get the mex file for a built-in MATLAB m- file? If yes, how? If no, does that mean I have to write the C code myself (oh nooo!!!)
The Matlab built-in functions are closed-source. Thus, you won't be able to get the code for them. It is possible to call Matlab functions from C code, though, if that solves your problem.
Depending on the function you want, you can find some of it elsewhere. For example, linear algebra operations are in LAPACK, and you may be able to get something from the source of OCTAVE.