2D Interpolat​ion/Extrap​olation on Irregular shaped grid

7 visualizzazioni (ultimi 30 giorni)
I'm starting with a grid that is evenly distributed over a recangular region:
M = load("function_matrix.mat")
M = struct with fields:
function_matrix: [20×20 double]
function_matrix = M.function_matrix
function_matrix = 20×20
0 0 0 0 0 0 0 0 -76.2225 -86.5401 -96.8417 -107.1065 -117.3124 -127.4292 -137.4142 -147.2091 -156.7384 -165.9125 -174.6321 -182.7946 0 0 0 0 0 0 0 0 -70.8491 -80.6616 -90.3755 -99.9543 -109.3594 -118.5454 -127.4580 -136.0343 -144.2031 -151.8869 -159.0034 -165.4676 0 0 0 0 0 0 0 0 -62.9376 -72.1101 -81.1053 -89.8773 -98.3804 -106.5638 -114.3707 -121.7385 -128.5994 -134.8813 -140.5088 -145.4046 0 0 0 0 0 0 0 0 -52.7790 -61.2207 -69.4132 -77.3044 -84.8447 -91.9809 -98.6556 -104.8072 -110.3703 -115.2765 -119.4546 -122.8306 0 0 0 0 0 0 0 0 -40.5530 -48.1950 -55.5224 -62.4779 -69.0095 -75.0629 -80.5808 -85.5027 -89.7650 -93.3008 -96.0409 -97.9183 0 0 0 0 0 0 0 0 -26.3824 -33.1680 -39.5796 -45.5549 -51.0398 -55.9794 -60.3166 -63.9923 -66.9442 -69.1060 -70.4116 -70.7992 0 0 0 0 0 0 0 0 -10.3565 -16.2362 -21.6887 -26.6455 -31.0508 -34.8489 -37.9825 -40.3937 -42.0231 -42.8047 -42.6716 -41.5684 0 0 0 0 0 0 0 0 7.4553 2.5265 -1.9287 -5.8320 -9.1267 -11.7583 -13.6685 -14.7970 -15.0874 -14.4808 -12.9051 -10.3021 0 0 0 0 0 0 0 0 0 23.0642 19.6402 16.8195 14.6625 13.2256 12.5607 12.7269 13.7897 15.8030 18.8236 22.9324 0 0 0 0 0 0 0 0 0 45.3288 42.9693 41.2628 40.2677 40.0413 40.6448 42.1318 44.5524 47.9818 52.4722 58.0746
[x,y] = meshgrid((1:20),(1:20))
x = 20×20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
y = 20×20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
I have 20x20 matrix that contains the values of a function over a subregion of this grid, and everywhere outside of this subregion the matrix has the value 0 (I've attached it to this post). The subregion where I do have the values is given by the below indexing matrix (1 corresponds to having the value and 0 corresponds to not having the value and just having a placeholder 0 in the function_matrix. My hope was to use interp2 to extrapolate to the values I don't have. I was going to do that with the following code:
id_matrix = logical([ 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]);
extrapolated_function_matrix = interp2(x(id_matrix),y(id_matrix),function_matrix(id_matrix),x,y,'spline')
Error using griddedInterpolant
Interpolation requires at least two sample points for each grid dimension.

Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});

Error in interp2 (line 126)
F = makegriddedinterp({X, Y}, V, method,extrap);
However, this gives me the following error: "The grid vectors must contain unique points."
I did some search on the forum, and people suggested using the unique() function on the coordinates, but that gave me another error. I just wanted to know if there was a way to extrapolate from a subregion of a grid to the whole grid like I am trying to do.
Thanks!

Risposte (1)

Torsten
Torsten il 13 Dic 2023
Modificato: Torsten il 13 Dic 2023
In order to use "interp2", you must specify a rectangular region where values of your function are given on a regular grid. Thus if you use 1:20 x 1:20 as rectangular region, you must specify values for f at 1:20 x 1:20. But if you had values on 1:20 x 1:20, there wouldn't be the need to extrapolate. So "interp2" is not useful to solve your problem.
You will have to use "scatteredInterpolant". But don't be disappointed if the extrapolated values are far from what you expect.

Categorie

Scopri di più su Interpolation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by