Back to tutorial index

Function approximation solving a Vandermonde system

Topics in this lab


Every calculus student knows that working with polynomials is often much easier than working with more complicated functions. For example, it is easy to compute the derivatives of polynomials.

For this reason, among others, it is often useful to approximate more complicated functions with polynomials. In this case, we typically sample the function $y = f(x)$ at a set of points $\{x_j\}$ for $j=0,1,2,...,N$ to get a set of points $(x_j,y_j)$. In this situation, we want our polynomial $p(x)$ to got through, or inteprolate our data points. Approximating functions with polynomials is part of a specialized area of mathematics called approximation theory.

A common way to handle this situation is to use the Matlab command polyfit. We will also see how the Matlab command vander can be used to obtain the same set of coefficients.

clear all;
close all;

Back to the top

Function approximation using polynomials

In this example, we will approximate the

f = @(x) sin(2*pi*x);     % Function we wish to approximate

N = 10;                   % Degree of the polynomial

% Sample the function at a set of equally spaced points.
xdata = linspace(-1,1,N+1);
ydata = f(xdata);

Get the coefficients of the interpolating polynomial using polyfit

a = polyfit(xdata,ydata,N);

The resulting polynomial has the form $$ p(x) = a_n x^n + a_{n-1} x^{n-1} + ... + a_1 x + a_0 $$ where the coefficients $a_i$ are stored in the array a. To evaluate the polynomial, we use the Matlab routine polyval as follows.

xs = linspace(-1,1,200);   % set of points used to plot the polynomial
ys = polyval(a,xs);


hold on;
plot(xs,f(xs),'b--');    % Plot the original function

title('Function approximation using polyfit','fontsize',18);
legend({'Poly. approx.','f(x)', 'Sampled data'},'fontsize',16);

Back to the top

Approximating derivatives

To compute derivatives of the polynomial approximation, we need to be able to compute derivatives of a polynomial described by a set of coefficients. To compute the derivative of the polynomial obtained using polyfit, we use polyder as


fp = @(x) 2*pi*cos(2*pi*x);           % Derivative of f(x) = sin(2*pi*x)

a_deriv = polyder(a);                 % Differentiate coefficients

% Obtain the value of the derivative at a dense set of points
ys_deriv = polyval(a_deriv,xs);
ydata_deriv = polyval(a_deriv,xdata);

hold on;
title('First derivative','fontsize',18);
legend({'1st deriv. approx.','f''(x)'},'fontsize',16);

Notice the scale on the y-axis. The derivative is quite large.

Continuing in this fashion, we can plot the second derivatives of each of these interpolants.


fpp = @(x) -(2*pi)^2*sin(2*pi*x);    % Second derivative of f(x)

a_deriv2 = polyder(a_deriv);         % Coefficients of the second derivative

% Obtain second derivative at a dense set of points
ys_deriv2 = polyval(a_deriv2,xs);

hold on;
title('Second derivative','fontsize',18);
legend({'2nd deriv. approx.','f''''(x)'},'fontsize',16);

Back to the top

The Vandermonde system

The function polyfit solves the Vandermonde system, given by $$ \left[\begin{array}{ccccc} x_0^n & x_0^{n-1} & ... & x_0 & 1 \\ x_1^n & x_1^{n-1} & ... & x_1 & 1 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ x_n^n & x_n^{n-1} & ... & x_n & 1 \\ \end{array}\right] \left[\begin{array}{c} a_n \\ a_{n-1} \\ \vdots \\ a_1 \\ a_0 \end{array}\right] = \left[\begin{array}{c} y_0 \\ y_1 \\ \vdots \\ y_{n-1} \\ y_{n} \end{array}\right] $$ We can write this system as $$ V{\bf a} = {\bf y} $$ We can construct this system explicity using function vander.

V = vander(xdata);
a = V\ydata(:);

The set of coefficients we get are identical to those obtained using polyfit.

Back to the top

Lab exercises

For each of the following problems, use the vander function to answer the questions. Plot the data points and the curve you find.
  1. Find the slope and the y-intercept of the line that passes through the two points $(-3.2, 1.2)$ and $(5.4,-4.3)$.
  2. Find the maximum value of the parabola that passes through the three points $$(-3.2, 4.5), \quad (1.2,6.1), \quad (6.1,-3.4)$$

Back to the top

Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_9.m)

Powered by MathJax

Published with MATLAB® 8.5