LBFGSB (L-BFGS-B) mex wrapper

L-BFGS-B, converted from Fortran to C, with Matlab wrapper
Updated 5 Aug 2022
----------------------- UPDATE Aug 2023 ----------------------------------------
I've linked this code to github ( If you want the original code, you can find that under the "Version History" tab (v1.6 and earlier).
----------------------- UPDATE Feb 2015 ----------------------------------------
Due to the difficulty of compiling Fortran for most people (including myself), I have converted the v3.0 L-BFGS-B Fortran code into C (using f2c and some hand-coded changes). I have a new wrapper for it as well; both the C code and the mex wrapper are available at:
You are still welcome to try this code, and if you get it to compile, there will be no major difference in speed or performance. Feel free to leave comments/feedback below. However, I will no longer support this version of the code.
----------------------- END UPDATE ----------------------------------------------
Description: L-BFGS-B is a variant of the well-known "BFGS" quasi-Newton method. This variant uses limited-memory (like L-BFGS), and also handles simple constraints (to be specific, bound constraints, so this includes x >=0 constraints).
The authors of LBFGSB have had fortran implementations available since 1996, but in 2011 they released a major update (v3.0) which has an improved algorithm (you should use this version!). There are existing mex file wrappers for earlier versions, but none worked with v3.0, so I created this file.
This submission tries to make the mex wrapper simple, and then includes a nice .m file solver that requires very few parameters, yet is customizable for advanced users. Run the compile_mex.m file to compile the files. compile_mex.m also includes two quick sanity checks (the file driver1.m is a helper file for one of these checks)
I also have an example of using L-BFGS-B to solve the non-negative least-squares
(NNLS) problem, and comparing it to other existing solvers. To put it simply,
L-BFGS-B is a fantastic NNLS solver, and much better than Matlab's lsqnonneg.
The fortran lbfgsb is at,
There are also versions 2.1 and 2.4 of the library.
For v 2.1, Peter Carbonetto's mex interface works; see and also
For v 2.4 (I don't know where you can find this version though), use the mex files from here:
The wikipedia page (this was mainly edited by one of the authors of the L-BFGS-B algorithm):
For installing on 64-bit Windows, if you need a compiler, see
=== Update, Jan 2015 ===
I am not able to completely support installation/compiling of this toolbox. If you have issues, please mention in the comments below (there are also some solutions from other users in the comments below). Newer versions of Matlab keep changing how files compile (e.g., recent versions do not allow fortran and C to be compiled together), and I am not able to keep up.
If you are using mac or linux, there is a good chance that we can get it to compile. If you are using 64-bit Windows, there is a slim chance you can get it to compile. So please take this as a warning! If anyone is good at software installation and wants to take over the installation script, please do so. The actual wrapper script is quite simple.

Cite As

Stephen Becker (2024). LBFGSB (L-BFGS-B) mex wrapper (, GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Nonlinear Optimization in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

See release notes for this release on GitHub:

Link to alternative implementation

No changes to files, just declaring that I cannot support 64-bit Windows. Sorry!

Removed unnecessary zip file from the package

adding compile_mex_forWindows.m

June 25, minor tweaks to code and install script to help with 64-bit computers. I also include an experimental f2c version (all in C) that doesn't require a fortran.

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.