0
$\begingroup$

I have grabbed a bunch of points in 3d that define a relatively simple 3d surface (three of them):

normal cold cold

I've been trying to figure out how to come up with a function for each (using curve_fit from scipy), but I'm probably doing something wrong -- I thought that going with something like a quadratic function would be enough for this:

def f(X, a, b, c, d, e, f):
    x, y = X
    return a + b*x + c*y + d*x**2 + e*y**2 + f*x*y
    
popt, pcov = curve_fit(f, (mirek, brightness), normal)
print(popt)

But the resulting coefficients have a huge error that doesn't come anywhere close to approximating the original surface.

Could anyone please point me to the right direction? I'm capable to learn, but this (reversing a math function out of observed behavior) is a completely new thing for me.

Also, I mostly have generic opensource tools at my disposal (ruby, python, gnuplot, etc), so telling me that there's a Mathematica/Matlab package would not help much...

New contributor
Wejn is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
2
  • 2
    $\begingroup$ You could try tensor product splines. In fact the visualization that you have is achieved through those. The question is how low you can get the resolution of said tensor product splines. The fitting for those is a linear least squares problem. $\endgroup$
    – lightxbulb
    Commented Dec 9 at 23:20
  • $\begingroup$ @lightxbulb thanks, I'll check it out, there's scipy.interpolate.NdBSpline ... $\endgroup$
    – Wejn
    Commented 2 days ago

2 Answers 2

2
$\begingroup$

From the graphics of your data, I doubt that quadratic functions would be enough. You could try with higher-order polynomials and compare the results for some of them.

An alternative approach would be to use symbolic regression. There is a Python package called PySR that is somewhat easy to use.

$\endgroup$
1
  • $\begingroup$ OK, I briefly tried 5-degree polynomial, and that helped to get the error down some. I'll experiment further. Also, thanks for the link to the symbolic regression, I'll check it out... $\endgroup$
    – Wejn
    Commented 2 days ago
1
$\begingroup$

Since your data appears to be on a grid, the right approach is to use that grid to define the interpolating function -- for example using piecewise bilinear polynomials as in the finite element method.

$\endgroup$
2
  • 1
    $\begingroup$ I would very much like to avoid using lookup tables and interpolation, since there's definitely some simple generating function for these (PWM controller for lights). But yes, worst case, I'd do that... $\endgroup$
    – Wejn
    Commented 2 days ago
  • $\begingroup$ Then project onto a coarser mesh. That too should be easy enough with gridded data. $\endgroup$ Commented 2 days ago

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.