SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

Interpolação
A seriedade dos acontecimentos da minha época.
Me enche de esperança.
Karl Marx

Atividades 8

  1. Arquivo N01.py: Para \(n=2,3,...,10,\) escolher aleatóriamente \(n+1\) pontos: \(\underline{p}_i,\) com abscissas distintas.
  2. Arquivo N02.py:
    1. Calcular a matriz de Vandermonte das abscissas e seu determinante.
    2. Calcular o Polinômio Interpolador, \(P_n(x),\) usando Eliminação de Gauss.
    3. Calcular e verificar valores e residuos relativos do \(P_n(x)\) nas abscissas \(x_i.\)
  3. Arquivo N03.py:
    1. Calcular as Funções de Lagrange, \(H_i(x),\) como Polinômios.
    2. Calcular o Polinômio Interpolador de Lagrange, \(P_n(x).\)
    3. Calcular e verificar valores e residuos relativos do \(P_n(x)\) nas abscissas \(x_i.\)
  1. N01.py:
    Python Listing: N01.py.
    pss=[
        #Add more...
        #Last list of 11 points
        [
            [ 0.0, -8.0, ],
            [ 1.0, -5.0,],
            [ 2.0, 3.0,],
            [ 3.0, -8.0,],
            [ 4.0, 6.0,],
            [ 5.0, 2.0,],
            [ 6.0, 5.0,],
            [ 7.0, -4.0,],
            [ 8.0, 3.0,],
            [ 9.0, 1.0,],
            [10.0, -7.0],
        ],
    ]
    
    def Points_Split(ps):
        xs=[]
        ys=[]
        for i in range( len(ps) ):
            xs.append( ps[i][0] )
            ys.append( ps[i][1] )
    
        return xs,ys
    
  2. N02.py:
    Python Listing: N02.py.
    from Polynomial import *
    from Interpolation import *
    from Lagrange import *
    
    from N01 import *
    
    for n in range( len(pss) ):
        xs,ys=Points_Split(pss[n])
        
        print "---"*30,"\nList no",n,":",len(xs),"Points: Vandermonte\n","---"*30
    
        #Question a
        V=Matrix_Vandermonte(xs)
        #Matrix_Print(V)
    
        #Question b
        P=Interpolation_Gauss(xs,ys)
        print "P Gauss=",
        Polynomia_Print(P)
    
        
        norm=Vector_Norm(ys)
        
        #Question c
        text=[
            "i",
            "x",
            "\ty",
            "\tGauss",
            "\tResidual",
        ]
        
        print "\t".join(text)
    
        for i in range( len(xs) ):
            gauss=Polynomia_Calc(P,xs[i])
            gauss_res   = abs( (gauss   -ys[i])/norm )
        
            text=[
                "%d" % i,
                "%.6f" % xs[i],
                "%.6f" % ys[i],
                "%.6f" % gauss,
                "%.6e" % gauss_res,
            ]
            print "\t".join(text)
    
    ------------------------------------------------------------------------------------------ 
    List no 0 : 11 Points: Vandermonte
    ------------------------------------------------------------------------------------------
    Gauss Forward, det 6.6586065841e+27
    P Gauss= 0.000976x**10 -0.049177x**9 +1.064501x**8 -12.938269x**7 +96.825770x**6 -459.710156x**5 +1372.767028x**4 -2456.398826x**3 +2348.341726x**2 -886.903571x -8.000000
    i    x        y        Gauss        Residual
    0    0.000000    -8.000000    -8.000000    0.000000e+00
    1    1.000000    -5.000000    -5.000000    5.618220e-11
    2    2.000000    3.000000    3.000000    6.803620e-11
    3    3.000000    -8.000000    -8.000000    7.269406e-11
    4    4.000000    6.000000    6.000000    9.043581e-11
    5    5.000000    2.000000    2.000000    1.261286e-10
    6    6.000000    5.000000    5.000000    1.825464e-10
    7    7.000000    -4.000000    -4.000000    2.138953e-10
    8    8.000000    3.000000    3.000000    1.473769e-10
    9    9.000000    1.000000    1.000000    1.186970e-10
    10    10.000000    -7.000000    -7.000000    1.132541e-10
    
    
    Output from: /usr/bin/python N02.py
  3. N03.py:
    Python Listing: N03.py.
    from Polynomial import *
    from Interpolation import *
    from Lagrange import *
    
    from N01 import *
    
    for n in range( len(pss) ):
        print "---"*30,"\nList no",n,":",len(pss[n]),"Points: Lagrange\n","---"*30
        
        xs,ys=Points_Split(pss[n])
        
        #Question a
        for i in range( len(xs) ):
            Hi=Lagrange_H_P(i,xs)
            print "H_"+str(i)+"(x)=",
            Polynomia_Print(Hi)
        
    
           
        #Question b
        P=Lagrange_P(xs,ys)
        print "P Lagrange=",
        Polynomia_Print(P)
    
        #Question c
        text=[
            "i",
            "x",
            "\ty",
            "\tLagrange Pol",
            "Residual",
        ]
        print "\t".join(text)
    
        norm=Vector_Norm(ys)
        for i in range( len(xs) ):
            lagrange=Polynomia_Calc(P,xs[i])
            lagrange_res= abs( (lagrange-ys[i])/norm )
        
            text=[
                "%d" % i,
                "%.6f" % xs[i],
                "%.6f" % ys[i],
                "%.6f" % lagrange,
                "%.6e" % lagrange_res,
            ]
            print "\t".join(text)
    
    ------------------------------------------------------------------------------------------ 
    List no 0 : 11 Points: Lagrange
    ------------------------------------------------------------------------------------------
    H_0(x)= 0.000000x**10 -0.000015x**9 +0.000364x**8 -0.005002x**7 +0.043478x**6 -0.248582x**5 +0.941614x**4 -2.317433x**3 +3.514544x**2 -2.928968x +1.000000
    H_1(x)= -0.000003x**10 +0.000149x**9 -0.003489x**8 +0.046528x**7 -0.388252x**6 +2.097569x**5 -7.318574x**4 +15.855754x**3 -19.289683x**2 +10.000000x
    H_2(x)= 0.000012x**10 -0.000657x**9 +0.015055x**8 -0.194965x**7 +1.566580x**6 -8.053038x**5 +26.266567x**4 -51.751339x**3 +54.651786x**2 -22.500000x
    H_3(x)= -0.000033x**10 +0.001720x**9 -0.038492x**8 +0.484722x**7 -3.763194x**6 +18.540278x**5 -57.372884x**4 +105.973280x**3 -103.825397x**2 +40.000000x
    H_4(x)= 0.000058x**10 -0.002951x**9 +0.064583x**8 -0.792014x**7 +5.962326x**6 -28.352951x**5 +84.327199x**4 -149.352083x**3 +140.645833x**2 -52.500000x
    H_5(x)= -0.000069x**10 +0.003472x**9 -0.074306x**8 +0.888889x**7 -6.512014x**6 +30.082639x**5 -86.873611x**4 +149.625000x**3 -137.540000x**2 +50.400000x
    H_6(x)= 0.000058x**10 -0.002836x**9 +0.059375x**8 -0.694097x**7 +4.965799x**6 -22.407465x**5 +63.294213x**4 -106.895602x**3 +96.680556x**2 -35.000000x
    H_7(x)= -0.000033x**10 +0.001587x**9 -0.032540x**8 +0.372421x**7 -2.610417x**6 +11.556944x**5 -32.095106x**4 +53.426190x**3 -47.761905x**2 +17.142857x
    H_8(x)= 0.000012x**10 -0.000583x**9 +0.011706x**8 -0.131424x**7 +0.905122x**6 -3.945226x**5 +10.810838x**4 -17.797768x**3 +15.772321x**2 -5.625000x
    H_9(x)= -0.000003x**10 +0.000127x**9 -0.002497x**8 +0.027546x**7 -0.186863x**6 +0.804051x**5 -2.179685x**4 +3.557165x**3 -3.130952x**2 +1.111111x
    H_10(x)= 0.000000x**10 -0.000012x**9 +0.000240x**8 -0.002604x**7 +0.017436x**6 -0.074219x**5 +0.199427x**4 -0.323165x**3 +0.282897x**2 -0.100000x
    P Lagrange= 0.000976x**10 -0.049177x**9 +1.064501x**8 -12.938269x**7 +96.825770x**6 -459.710156x**5 +1372.767028x**4 -2456.398826x**3 +2348.341726x**2 -886.903571x -8.000000
    i    x        y        Lagrange Pol    Residual
    0    0.000000    -8.000000    -8.000000    5.110892e-17
    1    1.000000    -5.000000    -5.000000    1.303278e-14
    2    2.000000    3.000000    3.000000    2.616266e-13
    3    3.000000    -8.000000    -8.000000    1.020543e-12
    4    4.000000    6.000000    6.000000    1.360673e-12
    5    5.000000    2.000000    2.000000    3.663539e-12
    6    6.000000    5.000000    5.000000    1.816048e-11
    7    7.000000    -4.000000    -4.000000    3.584990e-11
    8    8.000000    3.000000    3.000000    1.421434e-10
    9    9.000000    1.000000    1.000000    2.776663e-10
    10    10.000000    -7.000000    -7.000000    6.651324e-10
    
    
    Output from: /usr/bin/python N03.py
Messages:
0 secs.