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
% How do I do polynomial interpolation?
% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% Last Revised : June 10, 2009;
% Abstract: This program shows you how to do polynomial interpolation?
disp(‘ This program shows you how to do polynomial interpolation?’)
disp(‘ Autar K Kaw of https://autarkaw.wordpress.com’)
disp(‘ June 10, 2009’)
% y vs x data to interpolate
% x data
x=[-1 -0.6 -0.2 0.2 0.6 1];
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(‘The x data’)
disp(‘The y data’)
disp(‘The x values where you want to find the interpolated values’)
%% THE CODE
% Find the number of data points
% Fitting to polynomial of order m=n-1
% pp consists of the coefficients of the polynomial
% pp(1) is coefficient of x^m
% pp(2) is coefficient of x^(m-1)
% and so on
% Getting the values at xin
% This is only for plotting the interpolating polynomial
%% DISPLAYING OUTPUTS
disp(‘x values at which function is to be interpolated’)
disp(‘y values at the xin values’)
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)
title(‘y vs x ‘);
legend(‘Points given’,’Points found’,’Polynomial Curve’)
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 and http://www.youtube.com/numericalmethodsguy
Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.
6 thoughts on “How do I do polynomial interpolation in MATLAB”
If we have three variables how we can do the interpolation in MATLAB?
can you help me….?
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) ).
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.
This doesn’t compute the interpolating polynomial. “polyfit” calculates a fitting polynomial via least-squares method, quite a different thing.
If you have n points and one uses polyfit with n-1 order polynomial, it is interpolation.