MATLAB Examples - Interpolation
MATLAB Examples - Interpolation
?
Interpolation
• Interpolation is used to estimate data points between two known points.
The most common interpolation technique is Linear Interpolation.
• In MATLAB we can use the interp1() function.
• The default is linear interpolation, but there are other types available, such
as:
– linear
– nearest
– spline
– cubic
– etc.
• Type “help interp1” in order to read more about the different options.
Given the following Data Points:
Interpolation
x y
0 15
(Logged
1 10 Data from
2 9 a given
Process)
3 6
4 2
5 0
x=0:5;
y=[15, 10, 9, 6, 2, 0]; ?
plot(x,y ,'o')
grid
Problem: Assume we want to find the interpolated value for, e.g., 𝑥 = 3.5
Interpolation
We can use one of the built-in Interpolation functions in MATLAB:
x=0:5; new_y =
y=[15, 10, 9, 6, 2, 0];
4
plot(x,y ,'-o')
grid on
new_x=3.5;
new_y = interp1(x,y,new_x)
• Plot u versus T.
• Find the interpolated data and plot it in the same graph.
• Test out different interpolation types (spline, cubic).
• What is the interpolated value for u=2680.78 KJ/kg?
clear
clc
figure(1)
plot(u,T, '-o')
%Spline
new_u = linspace(2500,3200,length(u));
new_T = interp1(u, T, new_u, 'spline');
figure(2)
plot(u,T, new_u, new_T, '-o')
T = [100, 150, 200, 250, 300, 400, 500];
u=[2506.7, 2582.8, 2658.1, 2733.7, 2810.4, 2967.9, 3131.6];
figure(1)
plot(u,T, 'o')