In [1]:
# Lineárna, kvadratická a polynomiálna interpolácia
# Programové kódy: SageMath v.9.x(Python 3.0)
# Prerekvizity: Polynómy, Python, LaTex
#
# Použitá literatúra:
# [1] R. E. Mezei, An introduction to SAGE programming, John Wiley,and Sons,2016 
# ISBN: 978-1-119-12280-7.
#
# [2] Gregory V.Bard. Sage for Undergraduates:Second Edition,Compatible with Python3. 
# American Mathematical Society,2022.ISBN978-1-4704-6155-3.
#  URL https://bookstore.ams.org/mbk-143/ - Sage pre pokročilých
#
# [3] Paul, Zimmermann and Alexandre, Casamayou and Nathann, Cohen and Guillaume Con- 
# nan and Thierry Dumont and Laurent Fousse and François Maltey and Matthias Meulien 
# and Marc Mezzarobba and Clément Pernet and Nicolas M. Thiéry and Erik Bray and 
# John Cremona and Marcelo Forets and Alexandru Ghitza and Hugh Thomas: 
# Computational Mathematics with SageMath, 2018, ISBN978-1-611975-45-1.
# https://my.siam.org/Store/Product/viewproduct/?ProductId=30174951,
#  https://www.sagemath.org/sagebook/english.html 
#
# E-kurz: P. Szabó: Numerická matematika, 2022
# Spúšťanie programov: 
#    1) Otvoriť lokalitu https://sagecell.sagemath.org/ 
#    2) Kopírovať obsah príkladu In[n] do SageMath boxu 
#    3) Kliknutie na "Evaluate"
# Príloha publikácie: 
# [4] Peter Szabó a Eva Baranová: Integrálny počet a numerická matematika, 2022, TUKE
# ISBN:978-80-553-4185-9
# Modifikované: 10.12.2022 
In [2]:
# Lineárna interpolácia
# Vstupy: interaktívna množina bodov "points" [xi,yi]
# Výstup: p(x) - Lineárna lomená čiara, p(xi)=yi

def Linear_Spline(points):
    var("x")
    #first we plot the given points
    p = list_plot( points, color = "red", size = 50)
    
    #compute the number of subintervals
    n = len(points)-1

    #then compute and plot each branch of the linear spline
    for k in [1,..,n]:
        #we find the equation y=mx+b for the kth subinterval
        m = (points[k][1]-points[k-1][1])/  \
            (points[k][0]-points[k-1][0])
        b = points[k][1]-m*points[k][0]
        #then we plot that line on the interval [xk-1, xk]
        p += plot(m*x+b, x,points[k-1][0],points[k][0],
 	color = "green", title = "linear spline interpolation")
    #display the linear spline and the given points
    p.show()
@interact
def LinearSplineInteract(
    points = input_box(default = [(-3,-10),(-1,-5),(1,1),(2,10),
   (4,15)]) ):
    Linear_Spline(points)
In [3]:
# Lineárna interpolácia
# Vstupy: množina bodov "points" [xi,yi], i=0,1,..,n
# Výstup: p(x) - Lineárna lomená čiara, p(xi)=yi, i=0,1,..,n

def Linear_Spline(points):
    var("x")
    #first we plot the given points
    p = list_plot( points, color = "red", size = 50)
    
    #compute the number of subintervals
    n = len(points)-1

    #then compute and plot each branch of the linear spline
    for k in [1,..,n]:
        #we find the equation y=mx+b for the kth subinterval
        m = (points[k][1]-points[k-1][1])/  \
            (points[k][0]-points[k-1][0])
        b = points[k][1]-m*points[k][0]
        #then we plot that line on the interval [xk-1, xk]
        p += plot(m*x+b, x,points[k-1][0],points[k][0],
 	color = "green", title = "linear spline interpolation")
    #display the linear spline and the given points
    p.show()

points = [(-3,-10),(-1,-5),(1,1),(2,10),(4,15)]
Linear_Spline(points)
In [4]:
# Lagrangeova (polynomiálna) interpolácia
# Vstupy: interaktívna množina bodov "points" [xi,yi], i=0,1,..,n
# 
# Výstup: q(x) polynóm n-tého stupňa,  q(xi)=yi, i=0,1,..,n
# Lagrangeove bázické polynómy

def Lagrange_Basis(i, points):
    var("x")
    n = len(points)-1
    Li=1
    for j in [0,1,..,n] :
        if(i!=j):
            Li *= (x-points[j][0])/(points[i][0]-points[j][0])
    return Li

def Lagrange_Polynomial(points):
    var("x")
    n = len(points)-1
    p=0*x
    for i in [0,1,..,n]:
        p +=  points[i][1]*Lagrange_Basis(i,points)
    return p.full_simplify()

@interact
def LagrangeInterpolationInteract2(
    points = input_box(default = [(-3,-15),(-1,-5),(0,1),(2,10),
(3,15)]) ):
    n = len(points)-1
    a = min([points[i][0] for i in [0,1,..,n]])
    b = max([points[i][0] for i in [0,1,..,n]])
    p = plot(Lagrange_Polynomial(points) , x, a-1, b + 1)
    p = p + list_plot( points, color = "red", size = 70)
    q(x)=Lagrange_Polynomial(points)
    show (q)
In [5]:
# Lagrangeova (polynomiálna) interpolácia
# Vstupy: množina bodov "points" [xi,yi], i=0,1,..,n
# 
# Výstup: q(x) polynóm n-tého stupňa,  q(xi)=yi, i=0,1,..,n
# Lagrangeove bázické polynómy

def Lagrange_Basis(i, points):
    var("x")
    n = len(points)-1
    Li=1
    for j in [0,1,..,n] :
        if(i!=j):
            Li *= (x-points[j][0])/(points[i][0]-points[j][0])
    return Li

def Lagrange_Polynomial(points):
    var("x")
    n = len(points)-1
    p=0*x
    for i in [0,1,..,n]:
        p +=  points[i][1]*Lagrange_Basis(i,points)
    return p.full_simplify()


points = [(-3,-15),(-1,-5),(0,1),(2,10),(3,15)]
n = len(points)-1
a = min([points[i][0] for i in [0,1,..,n]])
b = max([points[i][0] for i in [0,1,..,n]])
p = plot(Lagrange_Polynomial(points) , x, a-1, b + 1)
p = p + list_plot( points, color = "red", size = 70)
q(x)=Lagrange_Polynomial(points)
show(p)
show (q)
In [6]:
# Lineárna, kvadratická a Lagrangeova (polynomiálna) interpolácia
# Vstupy: interaktívna množina bodov "points" [xi,yi], i=0,1,..,n
# Výber typu interpolácie: linear, cubic a polynomial
# Výstupy: p(x) polynóm n-tého stupňa, 
# lineárna lomená čiara l(x),
# Polynóm druhého stupňa q(x),
# p(xi)=l(xi)=q(xi)=yi, i=0,1,..,n.
# Poznámka: použitie knižnice "scipy"

import scipy
from scipy.interpolate import interp1d
def Lagrange_Basis(i, points):
    var("x")
    n = len(points)-1
    Li=1
    for j in [0,1,..,n] :
        if(i!=j):
            Li *= (x-points[j][0])/(points[i][0]-points[j][0])
    return Li

def Lagrange_Polynomial(points):
    var("x")
    n = len(points)-1
    p=0*x
    for i in [0,1,..,n]:
        p +=  points[i][1]*Lagrange_Basis(i,points)
    return p.full_simplify()


def DrawInterpolations(points, plotPoly, plotLinear, plotCubic):
    #needed if you want to use x in the input
    # like  [(x, sin(x)) for x in [0,1,..,4]]
    var("x")
    #compute the size of the given array
    n = len(points)
    #get the x values of points
    xvals = [points[i][0] for i in [0,1,..,n-1]]
    #get the y values of points
    yvals = [points[i][1] for i in [0,1,..,n-1]]
    #plot the points
    p = list_plot( points, color = "black", size = 50)
    #find the interval [a,b]
    a = min([points[i][0] for i in [0,1,..,n-1]])
    b = max([points[i][0] for i in [0,1,..,n-1]])
    if(plotPoly == True):
       fp=Lagrange_Polynomial(points)
       p += plot(fp, x, a, b, color = "red",
             legend_label = "polynomial interpolation")
    if(plotLinear == True):
       f1=interp1d(xvals, yvals, kind = "linear")
       p += plot(f1, x, a, b, color = "green",
             legend_label = "linear spline interpolation")
    if (plotCubic == True):
       f3=interp1d(xvals, yvals, kind = "cubic")
       p += plot(f3, x, a, b, color = "blue",
             legend_label = "cubic spline interpolation")
    #return the plot object
    return p

@interact
def InterpolationsInteract(
    points = input_box(default = [(-3,-10),(-1,-5),(1,1),(2,10),
    (4,15)]) ,
    plot_poly = ("Polynomial Interpolation", true),
    plot_linear = ("Linear Spline", true),
    plot_cubic = ("Cubic Spline", true)     ):
    DrawInterpolations(points, plot_poly, plot_linear, plot_cubic).show()
In [7]:
# Lineárna, kvadratická a Lagrangeova (polynomiálna) interpolácia
# Vstupy: množina bodov "points" [xi,yi], i=0,1,..,n
# Výstupy: p(x) polynóm n-tého stupňa, 
# lineárna lomená čiara l(x),
# Polynóm druhého stupňa q(x),
# p(xi)=l(xi)=q(xi)=yi, i=0,1,..,n.
# Poznámka: použitie knižnice "scipy"

import scipy
from scipy.interpolate import interp1d
def Lagrange_Basis(i, points):
    var("x")
    n = len(points)-1
    Li=1
    for j in [0,1,..,n] :
        if(i!=j):
            Li *= (x-points[j][0])/(points[i][0]-points[j][0])
    return Li

def Lagrange_Polynomial(points):
    var("x")
    n = len(points)-1
    p=0*x
    for i in [0,1,..,n]:
        p +=  points[i][1]*Lagrange_Basis(i,points)
    return p.full_simplify()


def DrawInterpolations(points, plotPoly, plotLinear, plotCubic):
    #needed if you want to use x in the input
    # like  [(x, sin(x)) for x in [0,1,..,4]]
    var("x")
    #compute the size of the given array
    n = len(points)
    #get the x values of points
    xvals = [points[i][0] for i in [0,1,..,n-1]]
    #get the y values of points
    yvals = [points[i][1] for i in [0,1,..,n-1]]
    #plot the points
    p = list_plot( points, color = "black", size = 50)
    #find the interval [a,b]
    a = min([points[i][0] for i in [0,1,..,n-1]])
    b = max([points[i][0] for i in [0,1,..,n-1]])
    if(plotPoly == True):
       fp=Lagrange_Polynomial(points)
       p += plot(fp, x, a, b, color = "red",
             legend_label = "polynomial interpolation")
    if(plotLinear == True):
       f1=interp1d(xvals, yvals, kind = "linear")
       p += plot(f1, x, a, b, color = "green",
             legend_label = "linear spline interpolation")
    if (plotCubic == True):
       f3=interp1d(xvals, yvals, kind = "cubic")
       p += plot(f3, x, a, b, color = "blue",
             legend_label = "cubic spline interpolation")
    #return the plot object
    return p

points = [(-3,-10),(-1,-5),(1,1),(2,10),(4,15)]
plot_poly = true
plot_linear = true
plot_cubic = true
DrawInterpolations(points, plot_poly, plot_linear, plot_cubic).show()