Roland Y.
Roland Y.

Reputation: 423

2D interpolation using TriScatteredInterp (Matlab)

Let us consider I have a set of points, which are described as a pair of 2D coordinates. At every single point, I have the value of a given parameter, let us say, temperature.

Point 1 : (x1, y1, t1)

Point 2 : (x2, y2, t2)

...

Point n : (xn, yn,tn)

All those points are contained within a 2D domain which is shaped as a triangle.

I would like to interpolate parameter t within the extend of the entire domain. Any interpolation method (linear, nearest neighbors,...) would be fine, to me. I am deeply convinced I achieve this using MATLAB - more precisely using TriScatteredInterp. However, it does not seem to work. It fails to create the interpolant.

Here is what I have tried so far :

x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
t = [10, 20, 30, 20]
F = TriScatteredInterp(x, y, t)

The last line yields the following error :

??? Error using ==> TriScatteredInterp

Input data must be specified in column-vector format.

It seems the way I have given the input is wrong. I have made some research over Google, though I couldn't find the problem.

Any help would be greatly appreciated, thanks.

Upvotes: 1

Views: 1404

Answers (1)

rayryeng
rayryeng

Reputation: 104484

The error is pretty clear... it says the data must be in column vectors. You have them as row vectors. Simply put, transpose your data before calling the function:

>> F = TriScatteredInterp(x.', y.', t.')

F = 

  TriScatteredInterp with properties:

         X: [4x2 double]
         V: [4x1 double]
    Method: 'linear'

FWIW, if you read the documentation, you would see that column vectors are required: http://www.mathworks.com/help/matlab/ref/triscatteredinterp.html


Once you create your interpolant, you can use any (x,y) coordinates of any size to be used in the interpolant, and the result will give interpolated values that match the size of both x and y... so something like this could work:

[X,Y] = meshgrid(linspace(min(x),max(x)), linspace(min(y),max(y)));
out = F(X,Y);

The output will be a grid of (x,y) coordinates that was applied to the interpolant... basically, you would get an interpolated surface using X and Y as unique (x,y) pairs.

Upvotes: 1

Related Questions