How to do 2D interpolation

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,

Antworten (2)

Akira Agata
Akira Agata am 30 Aug. 2017

2 Stimmen

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 am 29 Aug. 2017

1 Stimme

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 Kommentare

??? Undefined function or method 'table' for input arguments of type 'cell'.
1. Sir, is there any alternative to "table" my Matlab version is R2010a
2. How to auto select if the query data is with in the data use interpolation, and if the query data is out side the data then use extrapolation
3. I want to predict y1 & y2 for new X= 0.8,8.3, 1.8,4.6
Josh Meyer
Josh Meyer am 30 Aug. 2017
1. The table command is just to help explain the output F, but isn't actually needed here for the computation.
2. interp1 does this automatic selection for you. By default, it returns NaN values for query points outside of the sample points, but when you select 'extrap' it will extrapolate those points instead. Interior points are interpolated in either case.
3. The xq input to interp1 contains the query points. To add more points, just add them to the xq vector.
Sir,
When we use "extrap", will it predict outside range (extrapolation) as with in range prediction?
how to replace "table"
Josh Meyer
Josh Meyer am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 29 Aug. 2017

Kommentiert:

am 5 Sep. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by