restart;
# Here are the commands to generate a Lagrange interpolating polynomial in Maple for  
# a function y=f(x).  First we define the function, then create a data set.  Next  
# we construct the polynomial, and then graph the approximation and function.

f := x->2*x^3+11*x^2+7*x+5;                           

xset := [1,2,4,6,9,11];
                           xset := [1, 2, 4, 6, 9, 11]


yset := map(f,xset);
                     yset := [25, 79, 337, 875, 2417, 4075]


Lagrange_f := interp(xset,yset,x);
                       
# As expected we get the original function since 6 data points will give a 5th degree polynomial and the third degree polynomial already does the job.
# Now let's see what happens when we have a slight error in the data:
 
yset:= [yset[1..2],yset[3]+delta,yset[4..6]];
                 yset := [25, 79, 337 + delta, 875, 2417, 4075]


Lagrange_f_delta := interp(xset,yset,x);
           
difference := expand(Lagrange_f_delta-Lagrange_f);
  
# By choosing different values for delta we can plot the error...

plot(subs(delta=2,difference),x=0..12);
# WE NOW INVESTIGATE PADE APPROXIMATION

with(numapprox);
[chebpade, chebyshev, confracform, hornerform, infnorm, laurent, minimax, pade,

    remez, taylor]

# The Pade approximation is actually constructed from the Taylor approximation. But to make sure we first compute the Taylor approximation of tan(x) and then use that to construct the Pade approximation.

taylor_tan := taylor(tan(x),x=0,8);
                           
          

taylor_tan_poly := convert(taylor_tan,polynom); 
                                                      
                                                   
pade_tan := pade(taylor_tan_poly,x,[3,4]);

plot({pade_tan,taylor_tan_poly,tan(x)},x=1..5,y=-10..10);