Extrapolation with minimum variation
10 views (last 30 days)
Show older comments
Javier Redondo Antón
on 24 May 2023
Commented: Javier Redondo Antón
on 5 Sep 2023
Hello everyone,
I have a function of two variables that I don't know and I want to try to guess. For this I have a small number of samples and their value in the function. To apply my method, I must calculate the gradient of the function (in this case of the interpolation of my function) to see where I find the maximum differences in the gradient to sample more in those areas. If I apply an interpolation (linear for example) to the data I have, I cannot calculate the gradient of the output because I get NaN values (the interpolation only works for points inside the convex hull and the domain of the function is larger). I have tried extrapolating using scatteredInterpolant, inpaint_nans and inpaintn but the values I get in some extrapolated areas differ quite a lot so the gradient there is very high.
I need a method where the extrapolation hardly varies so that the gradient is focused on the points inside the convex area. What is the best way to do this in Matlab? I can't find any other function that comes close to what I want.
I leave you some screenshots so that you can understand what is happening to me:

As can be seen, when extrapolating, very different values are obtained at the edges (the original function is not like that).

So when computing the gradient, the areas with the most variation are at the edges. I need to calculate the variation inside the points I already have.
Any suggestions are welcome. Thank you very much in advance!
1 Comment
Accepted Answer
Karan Singh
on 30 Aug 2023
Hey Javier as I have no access to your data or to your code, you can just try out suggestions that might help. To estimate the gradient of a function using limited samples and minimize the variability in extrapolation, you can consider using a technique called “radial basis function (RBF) approximation”. “RBF” approximation fits a smooth function to the given data points, allowing for more reliable extrapolation. In MATLAB, you can use the “rbfcreate and “rbfinterp” functions from the MATLAB File Exchange, which provide “RBF” interpolation and approximation capabilities. Here for the context is a demo code using the same.
rand('seed',0)
x = rand(50,1)*4-2; y = rand(50,1)*4-2;
z = x.*exp(-x.^2-y.^2);
ti = -2:.05:2;
[XI,YI] = meshgrid(ti,ti);
ZI = griddata(x,y,z,XI,YI,'cubic');
%RBF interpolation
ZI = rbfinterp([XI(:)'; YI(:)'], rbfcreate([x'; y'], z','RBFFunction', 'multiquadric', 'RBFConstant', 2));
ZI = reshape(ZI, size(XI));
%Plot data
subplot(2,2,1); mesh(XI,YI,ZI), hold, axis([-2 2 -2 2 -0.5 0.5]);
plot3(x,y,z,'.r'), hold off; title('Interpolation using MATLAB function griddata(method=cubic)');
subplot(2,2,3); pcolor(abs(ZI - XI.*exp(-XI.^2-YI.^2))); colorbar; title('griddata(method=cubic) interpolation error');
subplot(2,2,2); mesh(XI,YI,ZI), hold
plot3(x,y,z,'.r'), hold off; title('RBF interpolation'); axis([-2 2 -2 2 -0.5 0.5]);
subplot(2,2,4); pcolor(abs(ZI - XI.*exp(-XI.^2-YI.^2))); colorbar; title('RBF interpolation error');
While it should be noted that “RBF” interpolation usually produces much better results that standard MATLAB functions, but computation complexity of RBF interpolation is n^3 thus it is not recommended to use it for more than 2000 nodes. Also, that “rbfcreate” and “rbfinterp” functions are not built-in MATLAB functions but are available on the MATLAB File Exchange. Make sure to download and add them to your MATLAB path before using them.
Here is the MATLAB file exchange link where you can download as well as get more information. Scattered Data Interpolation and Approximation using Radial Base Functions - File Exchange - MATLAB Central (mathworks.com).
Hope it helps!
More Answers (0)
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!