How to do 2D interpolation

조회 수: 11 (최근 30일)
Mekala balaji
Mekala balaji 2017년 8월 29일
댓글: Josh Meyer 2017년 9월 5일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by