Understanding the Behavior of interp2 and interp3 (2024)

12 views (last 30 days)

Show older comments

Michael Boutros on 20 Sep 2019

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3

Answered: Athul Prakash on 24 Sep 2019

Accepted Answer: Athul Prakash

Open in MATLAB Online

I'm finding that the behavior of interp2 and interp3 is counterintuitive, and I'd like to understand how properly to use them. Here's a simple example using interp2:

XX_grid = linspace(eps,10,10);

YY_grid = linspace(eps,50,10);

[XX,YY] = meshgrid(XX_grid,YY_grid);

V = rand(10,10);

x_i = 4; y_i = 9;

interp2(XX_grid,YY_grid,V,XX_grid(x_i),YY_grid(y_i)) % (a)

interp2(YY_grid,XX_grid,V,YY_grid(y_i),XX_grid(x_i)) % (b)

V(x_i,y_i)

I setup two interpolations, (a) and (b). To test the code, I call the interpolations at two points exactly on the grid, which should exactly match the value in the interpolated matrix, V. However, (a) does not match the value from V, while (b) does. (a) is the original way I'd setup the interpolation but it didn't work, and by experimenting, I found that reversing the order of X and Y in (b) does the trick.

I tried the same thing with interp3 and, again, the code doesn't work as I expect. However, because we're in 3D space now, it isn't valid to simply change the order of the grids (and Matlab rightfully throws an error). Can someone shed some light on how I can get this to work?

X_grid = linspace(eps,5,10);

Y_grid = linspace(eps,7,10);

Z_grid = linspace(eps,3,10);

[X,Y,Z] = meshgrid(X_grid,Y_grid,Z_grid);

V = rand(10,10,10);

x_i = 3; y_i = 10; z_i = 5;

interp3(X,Y,Z,V,X_grid(x_i),Y_grid(y_i),Z_grid(z_i))

V(x_i,y_i,z_i)

Thanks for any and all help!

3 Comments

Show 1 older commentHide 1 older comment

Bjorn Gustavsson on 20 Sep 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#comment_747968

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#comment_747968

You compare the wrong component the first time around. If you display a matrix with imagesc you will find that the matrix representation is M(yi,xi) - that is first index is row-number (what you'd think of as y-coordinate) second is column (x-coordinate).

HTH

Michael Boutros on 20 Sep 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#comment_747973

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#comment_747973

Yes, and this is why reversing X and Y yields the interpretation that I have in mind. However, I can't see how this translates to the 3D case.

Bjorn Gustavsson on 20 Sep 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#comment_747977

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#comment_747977

Open in MATLAB Online

No. Reset.

This is showing that the interp2-function does as it should:

XX_grid = linspace(eps,10,10);

YY_grid = linspace(eps,50,12);

V = rand(12,10);

[XX,YY] = meshgrid(XX_grid,YY_grid);

x_i = 4; y_i = 9;

Vi = interp2(XX_grid,YY_grid,V,XX_grid(x_i),YY_grid(y_i));

[Vi V(y_i,x_i) (Vi-V(y_i,x_i))]

After your call to interp2 you should do the matlab-correct comparison.

If I recall correctly you can call interp3 with coordinate grids made with meshgrid and ndgrid. Chose wisely - I don't remember having problems with the selection I made, but your application might have different requirements. Just make sure you test your choise works afterwards - most plotting-functions etc works well with grids produced with meshgrid.

Check the outputs of those functions for small arrays to see the difference:

[Xm,Ym] = meshgrid(-1:2,0:2:4)

Xm =

-1 0 1 2

-1 0 1 2

-1 0 1 2

Ym =

0 0 0 0

2 2 2 2

4 4 4 4

[Xn,Yn] = ndgrid(-1:2,0:2:4)

Xn =

-1 -1 -1

0 0 0

1 1 1

2 2 2

Yn =

0 2 4

0 2 4

0 2 4

0 2 4

HTH

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Athul Prakash on 24 Sep 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#answer_393251

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/481260-understanding-the-behavior-of-interp2-and-interp3#answer_393251

Open in MATLAB Online

Hey Michael,

Consider the syntax:

Vq = interp2(XX, YY, V, Xq, Yq);

The query point is interpreted in a graphical sense, therefore 'Xq' works along the horizontal direction (i.e. second dimension) of 'V' and 'Yq' is taken along the first dimension of 'V'. Therefore in your (a) line, you are asking for the point with co-ordinates (XX_grid(4), YY_grid(9)), which is mapped to the 4th horizontal and 9th vertical value in matrix 'V'. This is indexed as V(9, 4), since MATLAB treats first index as the row number (i.e along vertical dimension).

Hence,

interp2(XX_grid,YY_grid,V,XX_grid(x_i),YY_grid(y_i))

V(y_i, x_i)

these 2 lines give the same output.

If you want to interpolate 3D functions, the tricky part is still the same as before: y dimension (vertical) is the first index of the underlying matrix and x (horizontal) dimension if the second index. The z dimension is simply the last index. Hence, a graphical co-ordinate (x,y,z) would have indices V(y,x,z) for a 3D matrix V. [Swap x-y; z is the same]

In your example,

interp3(X,Y,Z,V,X_grid(x_i),Y_grid(y_i),Z_grid(z_i))

V(y_i, x_u, z_i)

these 2 lines would give the same output.

Hope it helps!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABMathematicsInterpolationInterpolating Gridded Data

Find more on Interpolating Gridded Data in Help Center and File Exchange

Tags

  • interpolation

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Understanding the Behavior of interp2 and interp3 (6)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Understanding the Behavior of interp2 and interp3 (2024)
Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6050

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.