Search code examples
matlabmatlab-figurecurve-fittingsurfacedata-fitting

How to fit a surface ((x,y,z) matrix) using Matlab?


I made AFM (Atomic Force Microscopy) measurements. I exported my data from Gwyddion to a text file (can be download here), such as I load it in Matlab like:

data = importdata('001_Zsensor.xyz');
x=data(:,1);y=data(:,2);z=data(:,3);
shading('interp');
tri = delaunay(x,y);
figure(1)
tsurf=trisurf(tri,x,y,z,'EdgeColor','none','Facecolor','interp');

So now I have my surface. It corresponds to the rugosity of a bead, so I need to extract the spherical feature of this surface in order to recover the rugosity landscape over a plane surface (from which I can then compute my physical parameters).

Basically, I want to fit a ellipsoid to the surface I previously defined (tsurf). I tried using cftool (even though I'd rather use command so I can put this in a Matlab script) but as the equation should be of form

z=f(x,y)

and the ellipsoid's equation is

((x-x0)/a)^2 + ((y-y0)/b)^2 + ((z-z0)/c)^2 = 1,

I did not manage to get the fit to work. How can I do so?

Thank you very much.


Solution

  • Ok, found it! Here it is:

    Following the previous code lines I wrote (see the question of this post):

    ffit = fit([x y],z,'poly22')
    figure(1)
    hold on
    plot(ffit)
    

    Of course, the fit can be more constrained if needed.