Interpolation for 1-D, 2-D, 3-D, and N-D gridded data in functionformat

1 view (last 30 days)
Mohsen Vazirizade on 2 Jan 2018
Edited: Matt J on 3 Jan 2018
I have an N-D gridded data and I want to fit a hyperplane on them. There are two almost similar commends in Matlab but none of them is the one I'm looking for. The following function is almost the one I'm looking for but it works just for 2D (line) and 3D (surface); however, I'm looking for N-Dimension https://www.mathworks.com/help/curvefit/fit.html Another command might be useful but still is not the exact one I'm looking for is interpn. Although it works for N-D, it is not gerenate a black box or fucntion as the way you can input value and have the output. https://www.mathworks.com/help/matlab/ref/interpn.html
In this regard and all the explanations above, do you know how I can address this issue? Thanks

Stephen Cobeldick on 3 Jan 2018
Edited: Stephen Cobeldick on 3 Jan 2018
ND curve-fitting is not really a trivial task. You can easily define a "black box or fucntion as the way you can input value and have the output" by defining an anonymous function which contains interpn, e.g.:
>> [X1,X2] = ndgrid((-5:1:5));
>> R = sqrt(X1.^2 + X2.^2)+ eps;
>> V = sin(R)./(R);
>> fun = @(q1,q2)interpn(X1,X2,V,q1,q2,'cubic');
>> fun([1,2;3,4],[0,0;1,1])
ans =
0.84147 0.45465
-0.0065407 -0.20163

Matt J on 3 Jan 2018
Edited: Matt J on 3 Jan 2018
Your question isn't terribly clear. Fitting and interpolation are two very different things. I suspect what you might be looking for is lsqcurvefit(). It will let you fit parameters to an N-dimensional black-box function. However, hyperplanes are especially simple functions. If you are merely trying to fit an N-dimensional hyperplane given M data points organized into an MxN array A, it can be done by very simple linear algebra:
d=mean(A,1);
[~,~,V]=svd(A-d,0);
hyperplaneNormal=V(:,end);
hyperplaneShift=d*hyperplaneNormal;