# How do I do polynomial interpolation in MATLAB

Many students ask me how do I do this or that in MATLAB.  So I thought why not have a small series of my next few blogs do that.  In this blog, I show you how to do polynomial interpolation.

• The MATLAB program link is here.
• The HTML version of the MATLAB program is here.
• DO NOT COPY AND PASTE THE PROGRAM BELOW BECAUSE THE SINGLE QUOTES DO NOT TRANSLATE TO THE CORRECT SINGLE QUOTES IN MATLAB EDITOR.  DOWNLOAD THE MATLAB PROGRAM INSTEAD

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I do polynomial interpolation?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/interpolate_polynomial.m;
% Last Revised : June 10, 2009;
% Abstract: This program shows you how to do polynomial interpolation?
%           .
clc
clear all
clf

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to do polynomial interpolation?’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/interpolation_polynomial.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   June 10, 2009’)
disp(‘ ‘)

%% INPUTS
% y vs x data to interpolate
% x data
x=[-1  -0.6  -0.2  0.2  0.6  1];
% ydata
y=[0.0385    0.1000    0.5000    0.5000    0.1000    0.0385];
% Where do you want to interpolate at
xin=[-0.8  -0.7  0.7  0.8];
%% DISPLAYING INPUTS
disp(‘INPUTS’)
disp(‘The x data’)
x
disp(‘The y data’)
y
disp(‘The x values where you want to find the interpolated values’)
xin
disp(‘  ‘)

%% THE CODE
% Find the number of data points
n=length(x);
% Fitting to polynomial of order m=n-1
m=n-1
% pp consists of the coefficients of the polynomial
% pp(1)*x^m+pp(2)*x^m+…….+pp(m)
% pp(1) is coefficient of x^m
% pp(2) is coefficient of x^(m-1)
% and so on
pp=polyfit(x,y,m);
% Getting the values at xin
yin=polyval(pp,xin);
% This is only for plotting the interpolating polynomial
xplot=x(1):(x(n)-x(1))/10000:x(n);
yplot=polyval(pp,xplot);

%% DISPLAYING OUTPUTS
disp(‘  ‘)
disp(‘OUTPUTS’)
disp(‘x values at which function is to be interpolated’)
xin
disp(‘y values at the xin values’)
yin
disp(‘These are the coefficients of the polynomial interpolant’)
disp(‘pp(1) is coefficient of x^m, pp(2) is coefficient of x^(m-1) and so on’)
fprintf(‘Order of polynomial m =%g’,m)
pp
xlabel(‘x’);
ylabel(‘y’);
title(‘y vs x ‘);
plot(x,y,’o’,’MarkerSize’,10,’MarkerEdgeColor’,’b’,’MarkerFaceColor’,’b’)
hold on
plot(xin,yin,’o’,’MarkerSize’,10,’MarkerEdgeColor’,’k’,’MarkerFaceColor’,’k’)
hold on
plot(xplot,yplot,’LineWidth’,2)
legend(‘Points given’,’Points found’,’Polynomial Curve’)
hold off
disp(‘  ‘)

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos

## 0 thoughts on “How do I do polynomial interpolation in MATLAB”

1. Praveen says:

Sir
If we have three variables how we can do the interpolation in MATLAB?
can you help me….?

2. Praveen says:

Sir
If we have three variables how we can do the interpolation in MATLAB?
can you help me….?

3. Greg A says:

First off, very nice site!

I believe your interpolation example is in fact a prediction example and not interpolation. You are predicting the dependent response, y, from the polynomial function, f(x). Consequently y = f(x). Interpolation is going in the opposite direction, that is, estimating a value for the independent variable x, from the function, x = inverse( f(x) ).

4. Greg A says:

First off, very nice site!

I believe your interpolation example is in fact a prediction example and not interpolation. You are predicting the dependent response, y, from the polynomial function, f(x). Consequently y = f(x). Interpolation is going in the opposite direction, that is, estimating a value for the independent variable x, from the function, x = inverse( f(x) ).

5. Kyle says:

Yes, this did not work for me. I typed in x = [100, 75, 50, 54] y = [161, 114, 70, 58]. The output (0.0004, -0.0947, 8.4721, -170.8182) does not produce accurate results. i.e. inputting 100 into the outputted polynomial does not return 161.

6. Kyle says:

Yes, this did not work for me. I typed in x = [100, 75, 50, 54] y = [161, 114, 70, 58]. The output (0.0004, -0.0947, 8.4721, -170.8182) does not produce accurate results. i.e. inputting 100 into the outputted polynomial does not return 161.

7. Spinor says:

This doesn’t compute the interpolating polynomial. “polyfit” calculates a fitting polynomial via least-squares method, quite a different thing.

8. Spinor says:

This doesn’t compute the interpolating polynomial. “polyfit” calculates a fitting polynomial via least-squares method, quite a different thing.

1. Autar Kaw says:

If you have n points and one uses polyfit with n-1 order polynomial, it is interpolation.