I have been unsuccessfully looking for a C++ library that can approximate a polynomial function f(x,y) = z given a bunch of known points (x,y,z) in 3D space.

This is trivial to do using the fit function or curve-fitting tool in matlab (see image in link for a specific example), but I need to integrate this functionality into some existing software which is written in C/C++ hence why I'm specifically looking for a C++ library. Can anyone point me towards something that might work?

I have come across many curve fitting libraries, but they all seem to be limited to fitting 1D curves. I have been looking for regression and interpolation functions, perhaps I am looking for the wrong thing?

Curve fitting in matlab example

1 Answer 1


Would a C library be acceptable?

cminpack is a good Levenberg-Marquardt implementation, which is a state-of-the-art curve fitting method, which also works for nonlinear problems.

Your problem is linear in its parameters and can be solved by multivariate linear least squares methods available in GSL. You will need to:

  • Translate your coordinate vectors into a multi-column X matrix containing columns for x, , y, , and so on for every known observation
  • If your z values are stored in a matrix, unfold it into a vector containing N*M values

Once it has been done, feed the predictor matrix and the regressor vector to gsl_multifit_linear, and you'll get the polynomial coefficients.

If you do need to solve nonlinear problems, GSL has a nonlinear least squares solver too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.