Using the same data, I obtain different surface fits on Mathematica (using ListSurfacePlot3D) and Python (using a code for best-fit quadratic curve (2nd-order)). Here's my code on Mathematica:

data = {{1, 1, 1}, {2, 2, 2}, {0, 2, 0}, {4, 2, 8}, {1, 2, 5}, {3, 3,
e = ListPointPlot3D[data];
f = ListSurfacePlot3D[data];
Show[e, f]

enter image description here

Here's my Python code (inspired from here):

import numpy as np
import scipy.linalg
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


data = np.c_[x,y,z]

# regular grid covering the domain of the data
mn = np.min(data, axis=0)
mx = np.max(data, axis=0)
X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))
XX = X.flatten()
YY = Y.flatten()

# best-fit quadratic curve (2nd-order)
A = np.c_[np.ones(data.shape[0]), data[:,:2], np.prod(data[:,:2], axis=1), data[:,:2]**2]
C,_,_,_ = scipy.linalg.lstsq(A, data[:,2])

# evaluate it on a grid
Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX*YY, XX**2, YY**2], C).reshape(X.shape)

# plot points and fitted surface using Matplotlib
fig = plt.figure(figsize=(10, 10))
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)
ax.scatter(data[:,0], data[:,1], data[:,2], c='r', s=50)

enter image description here

The fit on Python seems to have less details in terms of its curvature, and the overall shape is different. What could this be due to?

  • 2
    $\begingroup$ If you expect a universally unique "fit" to just 6 data points, you are very optimistic. I, being not so optimistic, would prefer the software respond with "Come back when you have more data." Also you'll notice that Mathematica does not fit the 3rd coordinate as a function of the first two as there is more than one value for some values of the first two coordinates. $\endgroup$ – JimB Jun 10 at 20:47
  • $\begingroup$ So you believe adding many data points will yield the same surface fit? I'm trying to understand the differences in methodology used by these 2 techniques to obtain surface fits, so I first decided to use few data points to check, and there is indeed a methodological difference. Hopefully some of you will have an idea what those methodological differences are. $\endgroup$ – Tommy95 Jun 10 at 20:51
  • $\begingroup$ Many points would certainly raise the probability of a more-or-less common fit. And I'm not well-versed enough to tell you how Mathematica does it. $\endgroup$ – JimB Jun 10 at 20:54
  • 3
    $\begingroup$ ListSurfacePlot3D isn't really fitting a surface to the points in the way you're thinking. It's actually closer to this paper: hhoppe.com/recon.pdf (This is just the first one on the topic I found with a search; actual implementation/details may differ, etc...) $\endgroup$ – Brett Champion Jun 10 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.