// Example 1.1 // Lagrange Polynomial Interpolation // // Author: Manolo Venturin // http://www.openeering.com // This code is released under a Creative Commons // Attribution - NonCommercial - NoDerivs 3.0 Unported License. // // Adapted to Scilab from the MATLABŪ examples presented // in Prof. Parviz Moin book // "Fundamentals of Engineering Numerical Analysis" // available at http://numerics.stanford.edu/ // Close all opened figures and clear workspace xdel(winsid()); clear; clc; // Import funciton exec("lagrange_interp.sci",-1); // set floating point exception mode ieee(2); // Define vectors // X : interpolation points // Y : value of f(X) // x : points where we want an evaluation of P(x), // where P is the interpolator polynomial x = linspace(-1,1,201); X = linspace(-1,1,11); y = 1 ./(1+25*(x .^2)); Y = 1 ./(1+25*(X .^2)); // Lagrange Interpolation pol = lagrange_interp(X,Y,x); // Generate plots plot(x,pol,"r", x,y,"k--", X,Y,"b."); xlabel("$x$","fontsize",4,"color","blue"); ylabel("$y$","fontsize",4,"color","blue"); title("Runge function","fontsize",5,"color","red"); xstring(-0.6,1,"$f(x) = \frac{1}{1+25 x^2}$"); e = gce(); e.font_size = 4; legend(["Lagrange Polynomial";"Expected behavior";"Data Points"]); // Evaluate Point in (.7) xval = 0.7; yval = lagrange_interp(X,Y,xval); disp(["pol("+string(xval)+") = "+string(yval)]); // Save image to png xs2png(gcf(),"image"+filesep()+"ch1ex1.png");