Open In App

Cubic Spline Data Interpolation in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Cubic spline interpolation is a type of interpolation in which data of degree 3 or less is interpolated. Refer to this article to understand the proper theoretical concept of Cubic Spline Interpolation. Now we will look at how to perform cubic spline interpolation in MATLAB.  MATLAB provides a simple function for performing cubic spline interpolation on a given data, the spline() function. 

Syntax:

spline(<x_data>, <y_data>, <query_points>)

The x_data and y_data are the input data for interpolation and the query contains values for which the user wants the value of the unknown function. We will understand the same with help of various examples.

Example 1:

Matlab




% MATLAB program spline data interpolation
  x = [1,3,5,7,9];
  y = sin(x);
  query = [0.5,pi,1.37];
  spline(x, y, query)


Output:

 

Example 2:

Matlab




% MATLAB program for the interpolated data.
  query = 0:11;
  x = [1,3,5,7,9];
  y = (x.^3) - (x.^2) + (x.^-1);
  yx = spline(x, y, query);
  plot(x, y, 'o', query, yx)


Output:

 

Conclusion:

This article discussed the usage of the spline function in MATLAB for performing cubic spline interpolation. The above could be used for any data which can be interpolated with a polynomial of degree 3 or less.


Last Updated : 06 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads