griddedInterpolant error "Interpolation requires at least two sample points for each grid dimension."

I'm systematically getting this error trying to use griddedInterpolant. Here's a simple example:
bp1 = [0:1:10];
bp2 = [5:0.5:10];
bp3 = [2:0.1:3];
tv = bp1.*bp3+bp2.^2
f=griddedInterpolant(bp1,bp2,bp3,tv)
But I get the error:
Error using griddedInterpolant
Interpolation requires at least two sample points for each grid dimension.
What have I missed?

Answers (2)

The points (bp1, bp2, bp3) in 3d space do not form a grid. They are equally-spaced points along a line segment from (0,5,2) to (10,10,3).
You can use ndgrid to construct a set of points that form a grid from your bp1, bp2, bp3 vectors.
bp1 = 0:1:10;
bp2 = 5:0.5:10;
bp3 = 2:0.1:3;
% tv = bp1.*bp3+bp2.^2;
[BP1,BP2,BP3] = ndgrid(bp1,bp2,bp3);
TV = BP1.*BP3+BP2.^2;
f=griddedInterpolant(BP1,BP2,BP3,TV)
f =
griddedInterpolant with properties: GridVectors: {[0 1 2 3 4 5 6 7 8 9 10] [5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10] [2 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3]} Values: [11×11×11 double] Method: 'linear' ExtrapolationMethod: 'linear'

5 Comments

First, thanks for pointing out that I hadn't created a grid! (doh!) Second, thanks for pointing me to "ndgrid". That was not so obvious. The challenge will be adapting my real problem, which is a 5 parameter data set with 22000 lines. But I think what you've shown can be applied. I'll let you know if it doesn't work.
Another approach which does not create large arrays BP1, BP2, BP3, is to simply RESHAPE the vectors to have the correct orientations (note that RESHAPE is a very efficient command):
BP1 = reshape(0:1:10, [],1,1);
BP2 = reshape(5:0.5:10, 1,[],1);
BP3 = reshape(2:0.1:3, 1,1,[]);
TV = BP1.*BP3+BP2.^2;
f = griddedInterpolant({BP1(:),BP2(:),BP3(:)},TV)
f =
griddedInterpolant with properties: GridVectors: {[11×1 double] [11×1 double] [11×1 double]} Values: [11×11×11 double] Method: 'linear' ExtrapolationMethod: 'linear'
The challenge I have is: my results vector is the result of simulation of the 22000 combinations of bp1, bp2, bp3, bp4 and bp5. How do I go about creating the equivalent of TV above? In other words, I have a spreadsheet that looks like:
0 59 18 0 50 4805.37
0 59 18 0 150 4806.96
0 59 18 0 250 4806.96
0 59 18 0 350 4806.96
0 59 18 0.2 50 4811.76
0 59 18 0.2 150 4817.66
0 59 18 0.2 250 4817.67
Definining bp1, bp2, bp3, bp4 and bp5 (columns 1-5) is pretty clear, but what do I do with the last column, result vector tv?
Either call RESHAPE on each column of data.
Or simply call SCATTEREDINTERPOLANT and let it do the work for you.
Unfortunately, scatteredInterpolant is limited to 3 parameters. I'll try to reshape each parameter plus the result vector.

Sign in to comment.

The interpn function could be an option. You will need to experiment with the ndgrid function to determine how best to reshape the first 5 columns of your data to conform to it (if they indeed need reshaping — they may not) and then do the interpolation. I am not certain how easy it would be to make sense of the results, much less plot them, since everything in this universe is limited to 3 spatial dimensions and time, last I heard.
If you only want to interpolate specific points or ranges of points within the limits of the original vectors (so not extrapolating), interpn could do what you want.

Products

Release

R2021a

Asked:

on 1 Dec 2023

Answered:

on 3 Dec 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!