# How to do 2D interpolation

4 views (last 30 days)
Mekala balaji on 29 Aug 2017
Commented: Josh Meyer on 5 Sep 2017
Hi,
I have below data, I only knew 1D interpolation(using interp1), but I want to use 2D interpolation, please some one help me,
X y1 y2
1.2 2.03 1.23
2.5 4.20 3.21
3.5 5.60 4.65
4.0 6.12 4.85
5.7 6.78 5.05
6.2 7.27 6.05
I want to predict y1 & y2 for new X= 0.8,8.3

Akira Agata on 30 Aug 2017
Looking at your data, curve fitting could be suitable to evaluate y1 and y2 at x = 0.8, 8.3. Here is the sample code to fit the data by 3rd order polynomial, and evaluate the target values.
A = [1.2 2.03 1.23
2.5 4.20 3.21
3.5 5.60 4.65
4.0 6.12 4.85
5.7 6.78 5.05
6.2 7.27 6.05];
f1 = polyfit(A(:,1),A(:,2),3);
f2 = polyfit(A(:,1),A(:,3),3);
xq = [0.8; 8.3];
T = table(xq, polyval(f1,xq), polyval(f2,xq),...
'VariableNames',{'QueryPoint','y1','y2'});
The output is as follows:
T =
2×3 table
QueryPoint y1 y2
__________ ______ ________
0.8 1.0776 0.041437
8.3 7.6596 9.307

Josh Meyer on 29 Aug 2017
Assuming that y1 and y2 are separate functions evaluated at the points in X, you are still just doing 1-D interpolation. Moreover, since you want to know the values of these functions at X = [0.8 8.3], and these query points lie outside the sample points, you really want to do extrapolation.
So you can still use interp1, but you need to specify a method and 'extrap' to evaluate these query points, for example:
A = [1.2 2.03 1.23
2.5 4.20 3.21
3.5 5.60 4.65
4.0 6.12 4.85
5.7 6.78 5.05
6.2 7.27 6.05];
xq = [0.8 8.3];
F = interp1(A(:,1),A(:,2:3),xq,'linear','extrap');
T = table(xq,F(:,1),F(:,2),'VariableNames',{'QueryPoint','y1','y2'})
T =
2×3 table
QueryPoint y1 y2
__________ ______ _______
0.8 1.3623 0.62077
8.3 9.328 10.25
Josh Meyer on 5 Sep 2017
• You can't replace table, but since it was just meant to show the data in a tidy format you don't even need that command. Just delete the whole T = table(...) line and examine the output F instead.
• 'extrap' just means that interior points are interpolated and exterior points are extrapolated.

### Community Treasure Hunt

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

Start Hunting!