Problem fitting data 3d
2 views (last 30 days)
Show older comments
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
Answers (2)
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
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
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!