Interpolation, using MATLAB


Engineering problems often required the analysis of data pairs. For example, the data pair might represent cause and effect, or input-output relationship. In some applications, we want to estimate the variable’s value between the data points. This is called Interpolation. We will be discussing two types of Interpolation ie Linear Interpolation and Cubic-Spline Interpolation. We might get different estimation of values with both type of interpolation. It is impossible to say which estimation gives more accurate answer without studying the dynamics of the physical system. We must always keep in mind that our results will be approximate and should be used with caution.
Suppose a car is travelling along a straight line. The data from observations are given in the following table. We are asked to predict the speed of the car at t=15 min and t = 23

Time (min) 0 6 10 13 17 20 28 32
Velocity(km/hr) 0 6.67 17.33 42.67 37.33 30.10 29.31 28.74

Linear Interpolation:

Linear interpolation is so named because it is equivalent to connecting the data points with a straight line.
Linear interpolation in MATLAB is obtained with the interp1 and interp2 functions.interp1 is used to linearly interpolate a function of one variable only: y =f(x)
Where as interp2 is used to linearly interpolate a function of two variables: z = f(x,y)
The syntax of interp1 is rather simple.
interp1(x,y,est_x)
where,
x is a vector containing independent variable data;
y is a vector containing dependent variable data;
est_x is vector containing the value(s) of independent variable at which we want to interpolates.
Lets see how does m-file looks like:
m-file will be something like this

clc
clear
x = [0 6 10 13 17 20 28 32];
y = [0 6.67 17.33 42.67 37.33 30.10 29.31 28.74];
x_est = [15 23];
y_est = interp1(x,y,x_est)


Cubic-Spline Interpolation:

In Spline interpolation we get smooth curve of the function through a set of points rather than sharp edges at data points.
The syntax of Spline interpolation in MATLAB is very similar to linear interpolation. We just have to replace ‘interp1’ with ‘spline’
Therefore,
spline(x,y,est_x)
where,
x is a vector containing independent variable data;
y is a vector containing dependent variable data;
est_x is vector containing the value(s) of independent variable at which we want to interpolates.

clc
clear
x = [0 6 10 13 17 20 28 32];
y = [0 6.67 17.33 42.67 37.33 30.10 29.31 28.74];
x_est = [15 23];
y_est = spline(x,y,x_est)

The plots of both the interpolation graphs are shown below. The dashed lines represents linear interpolation, and the solid curve is the cubic spline.
interpolation, matlab, spline, cubic, numerical, analysis

Linear and Spline interpolation graph

For more stuff on Numerical Analysis using MATLAB,

View Hammad Ansari's profile on LinkedIn

Advertisements

, , , ,

  1. #1 by Jim on October 20, 2011 - 12:25 am

    Hi, I enjoyed your post on interpolation of Time and Velocity! Great stuff. I am interested in using Matlab to perform a cubic interpolation of animal tracks so that I can create a smooth track/line for animal movements/locations. I have a large number of lat/long locations for an animal that I tracked, with each location having a known time (e.g. 05:15), and I would like to interpolate new lat/long locations at a specific time interval (e.g. every hour). I’m not sure how to accomplish this and was hoping that you may have some advice.

    Thanks.

  2. #2 by Jim on October 20, 2011 - 12:26 am

  1. Numerical Analysis using MATLAB « Hammad Ansari's Blog

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: