How to do 2D interpolation

11 次查看(过去 30 天)
Mekala balaji
Mekala balaji 2017-8-29
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
Many thanks in advance,

回答(2 个)

Akira Agata
Akira Agata 2017-8-30
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
Josh Meyer 2017-8-29
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
  4 个评论
Mekala balaji
Mekala balaji 2017-8-31
Sir,
When we use "extrap", will it predict outside range (extrapolation) as with in range prediction?
how to replace "table"
Josh Meyer
Josh Meyer 2017-9-5
  • 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!

Translated by