Polynomial Interpolation SICLAB

Embed Size (px)

Citation preview

  • 8/18/2019 Polynomial Interpolation SICLAB

    1/16

     

    www.openeering.com

     powered by 

     

    DATA FITTING IN SCILAB 

    In this tutorial the reader can learn about data fitting, interpolation andapproximation in Scilab. Interpolation is very important in industrial applicationsfor data visualization and metamodeling.

    Level

    This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

  • 8/18/2019 Polynomial Interpolation SICLAB

    2/16

     

    Data Fitting www.openeering.com   page 2/16

    Step 1: Purpose of this tutorial

    Many industrial applications require the computation of a fitting function in

    order to construct a model of the data.

    Two main data fitting categories are available:

    Interpolation which is devoted to the development of numerical

    methods with the constraint that the fitting function fits exactly all

    the interpolation points (measured data);

     Approximation which is devoted to the development of numerical

    methods where the type of function is selected and then all

    parameters are obtained minimizing a certain error indicator to

    obtain the best possible approximation.

    The first category is useful when data does not present noise, while the

    second one is used when data are affected by error and we want to

    remove error and smooth our model.

    Step 2: Roadmap

    In the first part we present some examples of polynomial interpolation and

    approximation. After we propose exercises and remarks.

    Descriptions StepsInterpolation 3-16

    Approximation or curve fitting 17-20

    Notes 21-22

    Exercise 23

    Conclusion and remarks 24-25

  • 8/18/2019 Polynomial Interpolation SICLAB

    3/16

     

    Data Fitting www.openeering.com   page 3/16

    Step 3: Interpolation

    The idea of approximation is to replace a function with a function

    selected from a given class of approximation function .

    Two main cases exist:

    Continuous funct ion : In this case the function is known

    analytically and we want to replace it with an easier function, for

    example we may want to replace a complex function with a

    polynomial for which integration or differentiation are easy;

    Discrete funct ion : In this case only some values of the function

    are known, i.e. and we want to make a mathematical

    model which is close to the unknown function such that

    it is possible to establish the value of outside the knownpoints.

    Example of approximat ion of a cont inuous funct ion using a piecewise

    approximat ion

    Step 4: Main class of interpolation function

    Several families of interpolation functions exist. The most common are:

    Polynomia l in terpo la t ion of degree : In this case, we

    approximate data with a polynomial of degree of the form

    Piecewise po lynomia l : In this case the interval is subdivided intosubintervals in which we define a polynomial approximation of low

    degree with or without continuity on the derivatives between each

    subinterval.

    Example piecewise in terpo la t ion wi th f i rs t der ivat ive cont inuous and

    discont inuous connect ions

  • 8/18/2019 Polynomial Interpolation SICLAB

    4/16

     

    Data Fitting www.openeering.com   page 4/16

    Step 5: Test case: Runge function

    The Runge function is defined as

    Typically is considered in the interval [-5,5].

    In our examples we consider 7 interpolation points, denoted with ,

    equally distributed in the interval [-5,5].

    The following code implements the Runge function and perform

    visualization.

    // Define Runge function deff('[y]=f(x)','y = 1 ./(1+x.^2)');

    // Interpolation points xi = linspace(-5,5,7)'; yi = f(xi);

    // Data xrunge = linspace(-5,5,101)'; yrunge = f(xrunge);

    // Plot Runge function scf(1); clf(1);plot(xrunge,yrunge,'b-');plot(xi,yi,'or');xlabel("x");

    ylabel("y");title("Runge function");

    In terpo lat ion fu nct ion

  • 8/18/2019 Polynomial Interpolation SICLAB

    5/16

     

    Data Fitting www.openeering.com   page 5/16

    Step 6: Piecewise constant interpolation

    Piecewise constant interpolation is the simplest way to interpolate data. It

    consists on locating the nearest data value and assigning the same value

    to the unknown point.

    Piecewise con stant in terpo la t ion

    Step 7: Piecewise constant interpolation in Scilab

    The Scilab command used to perform piecewise interpolation is"interp1" where the third argument is "nearest". The fourth argument

    specifies if an extrapolation method should be used when the evaluation

    points are outside the interval of the interpolation points.

    // Interpolation // Evaluation points xval = linspace(-6,6,101)';

    xx_c = xval;yy_c = interp1(xi,yi,xx_c,'nearest','extrap');

    // Plot scf(3);

    clf(3);plot(xrunge,yrunge,'k-');plot(xx_c,yy_c,'b-');plot(xi,yi,'or');xlabel("x");ylabel("y");title("Piecewise interpolation");legend(["Runge func";"Interp.";"Interp. val"]);

    Piecewise co nstant in terpo la t ion

  • 8/18/2019 Polynomial Interpolation SICLAB

    6/16

     

    Data Fitting www.openeering.com   page 6/16

    Step 8: Piecewise linear interpolation

    Linear interpolation is a polynomial of degree 1 that connects two points,

    , and the interpolant is given by

    Piecewise l inear interpolation (green) and extrapolation (red)

    Step 9: Linear interpolation in Scilab

    The Scilab command used to perform linear interpolation is again

    "interp1" but now the third argument is "linear". We can note that the

    code is similar to the previous one for piecewise interpolation.

    // Interpolation xx_l = xval;yy_l = interp1(xi,yi,xx_c,'linear','extrap');

    // Plot scf(4);clf(4);plot(xrunge,yrunge,'k-');plot(xx_l,yy_l,'b-');plot(xi,yi,'or');xlabel("x");

    ylabel("y");title("Linear interpolation");legend(["Runge func";"Interp.";"Interp. val"]); Piecewise l inear interpolatio n

  • 8/18/2019 Polynomial Interpolation SICLAB

    7/16

     

    Data Fitting www.openeering.com   page 7/16

    Step 10: Polynomial interpolation

    Given a set of data points , where all are different, we find

    the polynomial of degree which exactly passes through these points.

    Polynomial interpolations may exhibits oscillatory effects at the end of the

    points. This is known as Runge phenomenon . For example, the Runge

    function has this phenomenon in the interval [-5,+5] while in the interval [-

    1,1] this effect is not present.

    Polynomia l in terpo la t ion

    Step 11: Polynomial interpolation in Scilab

    The Scilab command used to perform polynomial interpolation is

    "polyfit" which is included in the zip file (see references). The syntaxrequires the interpolation points and the degree of the polynomial

    interpolation which is equal to the number of point minus one.

    Note that the command "horner" evaluates a polynomial in a given set of

    data.

    // Import function exec("polyfit.sci",-1);// Interpolation xx_p = xval;[Pn] = polyfit(xi, yi, length(xi)-1);yy_p = horner(Pn,xx_p);

    // Plot 

    scf(5);clf(5);plot(xrunge,yrunge,'k-');plot(xx_p,yy_p,'b-');plot(xi,yi,'or');xlabel("x");ylabel("y");title("Polynomial interpolation");legend(["Runge func";"Interp.";"Interp. val"]);

    Polynomia l in terpo la t ion

  • 8/18/2019 Polynomial Interpolation SICLAB

    8/16

     

    Data Fitting www.openeering.com   page 8/16

    Step 12: Cubic spline interpolation

    Cubic spline interpolation uses cubic polynomials on each interval. Then

    each polynomial is connected to the next imposing further continuity

    equations for the first and second derivatives.

    Several kinds of splines are available and depend on how degrees of

    freedom are treated. The most well-known splines are: natural, periodic,

    not-a-knot and clamped.

    Spl ine in terpo la t ion

    Step 13: Cubic spline in Scilab

    The Scilab command used to perform cubic spline interpolation is

    "splin". Several types of splines exist and can be specified by setting thethird argument of the function. The evaluation of the spline is done using

    the command "interp" where it is possible to specify the extrapolation

    strategy.

    // Splines examples d = splin(xi, yi,"not_a_knot");// d = splin(xi, yi,"natural"); // d = splin(xi, yi,"periodic"); 

    xx_s = xval;yy_s = interp(xx_s, xi, yi, d, "linear");

    // Plot scf(6);

    clf(6);plot(xrunge,yrunge,'k-');plot(xx_s,yy_s,'b-');plot(xi,yi,'or');xlabel("x");ylabel("y");title("Spline interpolation");legend(["Runge func";"Interp.";"Interp. val"]);

    Cubic sp l ine in terpo la t ion w i th l inear extrapo la tion

  • 8/18/2019 Polynomial Interpolation SICLAB

    9/16

     

    Data Fitting www.openeering.com   page 9/16

    Step 14: Radial Basis Interpolation (RBF)

    Radial basis function modeling consists of writing the interpolation function

    as a linear combination of basis functions that depends only on the

    distance of the interpolation points . This is equal to:

    Using the interpolation conditions we have to solve the

    following linear system

    where the element .

    Many radial basis functions exist, the most famous are:

    Gaussian:

    Multiquadratic:

    Inverse multiquadratic:

    Thin plate spline:

    Step 15: Gaussian RBF in Scilab

    In our example we use the Gaussian RBF.

    // Gaussian RBF  deff('[y]=rbf_gauss(r,sigma)','y = exp(-r.^2 ./(2*sigma))');

    // Plot scf(7);clf(7);r = linspace(0,3);y1 = rbf_gauss(r,0.1);y2 = rbf_gauss(r,1.0);y3 = rbf_gauss(r,2.0);plot(r,y1,'k-');plot(r,y2,'b-');

    plot(r,y3,'r-');xlabel("$r$");ylabel("$\phi(r)$");title("Gaussian rbf");legend(["$\sigma = 0.1$";"$\sigma = 1.0$";"$\sigma = 2.0$"]);

    Gaussian RBF for d i f ferent va lue of sigma

  • 8/18/2019 Polynomial Interpolation SICLAB

    10/16

  • 8/18/2019 Polynomial Interpolation SICLAB

    11/16

     

    Data Fitting www.openeering.com   page 11/16

    Step 17: Approximation or curve fitting

    When data is affected by errors, polynomial interpolation cannot be

    appropriate since the approximation function is constrained to be through

    the interpolation points. So it makes sense to fit the data starting from agiven class of functions and minimizing the difference between the data

    and the class of functions, i.e.

    The "polyfit" function computes the best least square polynomial

    approximation of data. If we choose in the "polyfit" function, we

    approximate data with linear function of the form , i.e. we

    compute the linear least squares fitting.

    Schematic example of min in terpretat ion

    Step 18: 1D approximation

    In this example we add noise to the function and then we

    make polynomial approximation of order 1 and 2.

    The critical code is reported here (only case 1):

    np = 100; noise = 0.7*(rand(np,1)-0.5);x = linspace(0,2,np)';yexact = x.^2 + x;ynoise = yexact + noise;

    // degree 1 approximation p1 = polyfit(x, ynoise, 1);p1val = horner(p1,x);scf(10); clf(10);plot(x,yexact,'k-'); plot(x,ynoise,'b-'); plot(x,p1val,'r-');

    For details download the zip file with the source codes. Compar ison of b est f i t t ing for degree 1 and 2

  • 8/18/2019 Polynomial Interpolation SICLAB

    12/16

     

    Data Fitting www.openeering.com   page 12/16

    Step 19: 2D approximation

    In this example we want to approximate scattered data with a linear least

    square fitting in two dimensions.

    Given the polynomial approximation and thescattered interpolation points , the optimal computation of the

    unknown parameters requires to solve the following

    overdetermined linear system

    The matrix is known as the Vandermonde matrix and arises in polynomial

    interpolation. The i-th row of the matrix corresponds to the polynomial

    evaluated at point i-th. In our case, the i-th row corresponds to:

    This is done using the Singular Value Decomposition (SVD) or

    equivalently using the backslash command. Here, we report only the

    solution stage. The full code can be downloaded from our web page.

    // Generating random points along a plane np = 30;noise = 0.5*(rand(np,1)-0.5);

    // Extract data x = rand(np,1);y = rand(np,1);

    znoise = -x+2*y+noise;

    // Vandermonde matrix for P(x,y) = a+b*x+c*y  V = [ones(np,1),x,y];// Find coefficient i.e. minimize error norm coeff = V\znoise;

    Example of least square approximat ion in 2D

  • 8/18/2019 Polynomial Interpolation SICLAB

    13/16

     

    Data Fitting www.openeering.com   page 13/16

    Step 20: nD linear approximation

    The previous example can be easily extended to an n-dimensional

    problem, i.e. we search for an approximation of the form

    .

    Here, we report the code for the problem where some coefficients may be

    equal to zero and could be deleted from the estimation problem.

    The idea here implemented consists of the following strategy:

    1. Perform least square approximation of data;

    2. Find the zero coefficients with some pre-fixed tolerance;

    3. Re-perform least square approximation of the reduce problemwhere we have deleted the columns of the Vandermonde matrix

    corresponding to the zero coefficients.

    The code reported on the right estimates these coefficients. The full code

    can be downloaded from our website.

    n = 10; // Problem dimensions neval = 100; // Number of evaluation points pcoeff = 1:n+1; // Problem coefficient ([a0, a1, ..., an]) pcoeff([2,5,8]) = 0; // Some zero coefficients 

    // Evaluation points [xdata,ydata] = generatedata(pcoeff, n, neval);

    // Estimate coefficients pstar = estimatecoeff(xdata, ydata);

    // Find "zero" coefficient (define tolerance) tol = 0.1;zeroindex = find(abs(pstar)

  • 8/18/2019 Polynomial Interpolation SICLAB

    14/16

     

    Data Fitting www.openeering.com   page 14/16

    Step 21: Another polyfit function

    The numerical solution of the polynomial interpolation problem requires to

    solve the linear system with the Vandermonde matrix. Numerically, this

    problem is ill-conditioned and requires an efficient strategy for a “correct”solution. This is performed by using, for example, QR factorization.

    The function “polyfit_fulldemo.sce”, provided by Konrad Km ieciak ,

    and contained in the zip file, is an alternative to the polyfit function.

    function [u]=polyfit(x, y, n) // Vandermonde for k=n:-1:0 

    if k==n then w=0;end w=w+1;Xu(:,w)=[x.^k];

    end 

    // QR [q r k]=qr(Xu,'0');s = inv(r) * (q' * y); // s = r \ (q' * y)for o=1:length(s) 

    u(find(k(:,o)>0))=s(o);end 

    endfunction 

    Step 22: Industrial applications of data fitting

    Interpolation and approximation are two major techniques for constructingmathematical models. Mathematical models can substitute complex model

    in real-case applications.

    When the original model is complex, or when it requires long and costly

    evaluations (for example with finite element analysis  – FEA), a simplified

    model of the original model is required.

    The model of the model is often called metamodel   (a.k.a. response

    surfaces ) and the metamodeling technique is widely used in industrial

    applications.

    In real-case applications, when optimizing product or services, we need to

    evaluate several times the model. This means that having a metamodel

    that can be evaluated faster is, most of the time, the only way for finding

    optimal solutions.

    Metamodeling+

    Response surface+

    Self-Organizing Maps+

    Neural Networks=

    Industrial applications

  • 8/18/2019 Polynomial Interpolation SICLAB

    15/16

     

    Data Fitting www.openeering.com   page 15/16

    Step 23: Exercise on exponential decay

     As an exercise, try to estimate the coefficient of a decay function of the

    form:

    using a linear least square fitting of data.

    Hits:  Use a logarithmic change of variable to reduce the problem in the

    following form

    Exponent ia l decay f i t t ing of data

    Step 24: Concluding remarks and References

    In this Scilab tutorial we have shown how to apply data fitting in Scilab

    starting from piece-wise interpolation, presenting polynomial fitting and

    cubic spline, and ending up with radial basis functions (RBF).

    In the case of polynomial interpolation we used the function "polyfit"

    which can be downloaded from the Scilab webpage and is included in the

    provided source codes.

    1. Scilab Web Page: Available: www.scilab.org. 

    2. Openeering: www.openeering.com. 

    3. Javier I. Carrero is the author of the polyfit function. The original

    code is available on the Scilab.org web pages and is included in the

    zip file.

    http://www.scilab.org/http://www.scilab.org/http://www.scilab.org/http://www.openeering.com/http://www.openeering.com/http://www.openeering.com/http://www.openeering.com/http://www.scilab.org/

  • 8/18/2019 Polynomial Interpolation SICLAB

    16/16

     

    Data Fitting www.openeering.com   page 16/16

    Step 25: Software content

    To report bugs or suggest improvements please contact the Openeering

    team. We thank Konrad Kmieciak for reporting us a new version of the

    polyfit function.

    www.openeering.com 

    Thank you for your attention,

    Manolo VenturinSilvia Poles

    --------------Main directory--------------ex0.sce : Plotting of the first figureex1.sce : Solution of exercise 1interpolation.sce : Interpolation examples codespolyfit.sci : Polyfit functionpolyfit_manual.pdf : Polyfit manualpolyfit_fulldemo.sce : Another version of polyfitestimate_lincoeff.sce : Estimantion of nD linear modellicense.txt : The license file

    http://www.openeering.com/http://www.openeering.com/http://www.openeering.com/