Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Problem fitting data 3d

Asked by dave on 31 May 2012

Hello, Does anyone know a way to fit 3D data to a function which is defined in 3 dimensions? In particular I have some points (X,Y,Z) that correspond to the points of a surface in 3d, I'd like to find the best approximant function f(x,y). Are there functions in MatLab that allow me to find the interpolating function?

Thanks!

0 Comments

dave

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 16 Jul 2012

There are an infinite number of interpolating functions that exactly match any given finite set of data points. If you do not know the form of the required function ahead of time, then all of the functions are as valid as each other, and the probability of choosing the "right" function becomes zero.

0 Comments

Walter Roberson
Answer by Star Strider on 16 Jul 2012

The ‘lsqcurvefit’ function can fit Z-data to (X,Y)-data generated by ‘meshgrid’. It is not well-documented so you will need to experiment with your objective function. It may be necessary to set ‘TolFun’ and ‘TolX’ to 1E-8 and ‘MaxFunEvals’ and ‘MaxIter’ to higher than the default values, since these functions may more difficult for ‘lsqnonlin’ to fit.

To illustrate:

        a = 2.5;
        b = 6.6;
        x2 = [ [1 : 0.1 : 10];  2*[1 : 0.1 : 10] ]'
        [Xgrid,Ygrid] = meshgrid(x2(:,1), x2(:,2));
        midpt = [median(x2(:,1))  median(x2(:,2))];
        y3 = exp([-(a.*(Xgrid-midpt(1))/10).^2 + -(b.*(Ygrid-midpt(2))/10).^2]);
        Xind = Xgrid-midpt(1);
        Yind = Ygrid-midpt(2);
        XYmtx = [Xind  Yind];
        f3 = @(B,XY) exp(-(B(1).*XY(:,1:size(XY,1))/10).^2  -(B(2).*XY(:,size(XY,1)+1:end)/10).^2);
        est_b3 = lsqcurvefit(f3, rand(2,1), [Xind  Yind], y3)

Somewhat to my surprise, ‘est_b3’ was a precise fit to the parameters with the default options. You will have to experiment with your function and data and generalize my code to match your requirements.

0 Comments

Star Strider

Contact us