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

Provas
Even a stopped clock is right once a day.
English proverb
< 2017.2.0 | 2017.2.1 | 2017.2.2 >

Cálculo Numérico, Semestre 2017.2, Prova I

PDF
  • N01.py:
    Python Listing: N01.py.
    from math import *
    
    def f_1(x):
        return x**4-x**3+2.0*x**2-4.0*x-7.0
    
    def df_1(x):
        return 4.0*x**3-3.0*x**2+4.0*x-4.0
    
    def d2f_1(x):
        return 12.0*x**2-6.0*x+4.0
    
    def f_2(x):
        return x*cos(x)+2.0
    
    def df_2(x):
        return cos(x)-x*sin(x)
    
    def d2f_2(x):
        return -2.0*sin(x)-x*cos(x)
    
    def f_3(x):
        return exp(x)-cos(x)-1.0
    
    def df_3(x):
        return exp(x)+sin(x)
    
    def d2f_3(x):
        return exp(x)+cos(x)
    
    def f_4(x):
        return (x-1)*exp(x)-2.0
    
    def df_4(x):
        return x*exp(x)
    
    def d2f_4(x):
        return (x+1.0)*exp(x)
    
    def f_5(x):
        return sin(x)-x*cos(x)-1.0
    
    def df_5(x):
        return x*sin(x)
    
    def d2f_5(x):
        return sin(x)+x*cos(x)
    
    
    
    Output from: /usr/bin/python N01.py
  • N02.py:
    Python Listing: N02.py.
    from math import *
    
    from N01 import *
    
    
    xs=[-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0 ]
    
    fs=[
        f_1,f_2,f_3,f_4,f_5
    ]
    
    titles=["$x$","$f_1(x)$","$f_2(x)$","$f_3(x)$","$f_4(x)$","$f_5(x)$"]
    
    print '\\begin{tabular}{c|rrrrr}'
    print "\t",' & '.join(titles)+'\\\\\n\t\\hline'
    
    for x in xs:
        text=[ "%.6f" % x  ]
        for f in fs:
            text.append( "%.6f" % f(x) )
        print "\t"," & ".join(text)+'\\\\'
    print '\\end{tabular}'
    
    \begin{tabular}{c|rrrrr}
        $x$ & $f_1(x)$ & $f_2(x)$ & $f_3(x)$ & $f_4(x)$ & $f_5(x)$\\
        \hline
        -3.000000 & 131.000000 & 4.969977 & 0.039780 & -2.199148 & -4.111097\\
        -2.000000 & 33.000000 & 2.832294 & -0.448518 & -2.406006 & -2.741591\\
        -1.000000 & 1.000000 & 1.459698 & -1.172423 & -2.735759 & -1.301169\\
        0.000000 & -7.000000 & 2.000000 & -1.000000 & -3.000000 & -1.000000\\
        1.000000 & -9.000000 & 2.540302 & 1.177980 & -2.000000 & -0.698831\\
        2.000000 & 1.000000 & 1.167706 & 6.805203 & 5.389056 & 0.741591\\
        3.000000 & 53.000000 & -0.969977 & 20.075529 & 38.171074 & 2.111097\\
    \end{tabular}
    
    
    Output from: /usr/bin/python N02.py
  • N03.py:
    Python Listing: N03.py.
    from math import *
    
    from N01 import *
    from Derivatives import *
    from Residual import *
    
    xs=[-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0 ]
    
    fs=[
        f_1,f_2,f_3,f_4,f_5
    ]
    
    dfs=[
        df_1,df_2,df_3,df_4,df_5
    ]
    
    d2fs=[
        d2f_1,d2f_2,d2f_3,d2f_4,d2f_5
    ]
    
    names=["f1","f2","f3","f4","f5"]
    
    titles=[
        "$x$",
        "$d^2f_{ana}$","$d^2f_{num}$","$r^2_a$","$r^2_r$",
    ]
    for n in range( len(dfs) ):
        f=fs[n]
        df=dfs[n]
        d2f=d2fs[n]
        print 'Derivative: '+names[n]
        
        print '\\begin{tabular}{c|rrrr}'
        print ' & '.join(titles)+'\\\\\n\\hline'
        for x in xs:
            text=[ "%.6f" % x ]
        
            d2f_ana=d2f(x)
            d2f_num=d2_f(f,x)
    
            r2_a=R_Abs(d2f_ana,d2f_num)
            r2_r=R_Rel(d2f_ana,d2f_num)
            
            text.append( "%.6f" % d2f_ana )
            text.append( "%.6f" % d2f_num )
            text.append( "%.6e" % r2_a )
            text.append( "%.6e" %  r2_r)
            
            print " & ".join(text)+'\\\\'
            
        print '\\end{tabular}'
    
    Derivative: f1
    \begin{tabular}{c|rrrr}
    $x$ & $d^2f_{ana}$ & $d^2f_{num}$ & $r^2_a$ & $r^2_r$\\
    \hline
    -3.000000 & 130.000000 & 130.000899 & 8.989347e-04 & 6.914882e-06\\
    -2.000000 & 64.000000 & 64.007466 & 7.465994e-03 & 1.166562e-04\\
    -1.000000 & 22.000000 & 21.999513 & 4.866778e-04 & 2.212172e-05\\
    0.000000 & 4.000000 & 3.999912 & 8.848688e-05 & 2.212172e-05\\
    1.000000 & 10.000000 & 10.000001 & 8.274037e-07 & 8.274037e-08\\
    2.000000 & 40.000000 & 40.002224 & 2.223756e-03 & 5.559389e-05\\
    3.000000 & 94.000000 & 93.997699 & 2.301486e-03 & 2.448390e-05\\
    \end{tabular}
    Derivative: f2
    \begin{tabular}{c|rrrr}
    $x$ & $d^2f_{ana}$ & $d^2f_{num}$ & $r^2_a$ & $r^2_r$\\
    \hline
    -3.000000 & -2.687737 & -2.687850 & 1.124689e-04 & 4.184521e-05\\
    -2.000000 & 0.986301 & 0.986655 & 3.540214e-04 & 3.589385e-04\\
    -1.000000 & 2.223244 & 2.223277 & 3.284248e-05 & 1.477232e-05\\
    0.000000 & -0.000000 & -0.000056 & 5.551115e-05 & 0.000000e+00\\
    1.000000 & -2.223244 & -2.223333 & 8.835363e-05 & 3.974086e-05\\
    2.000000 & -0.986301 & -0.986655 & 3.540214e-04 & 3.589385e-04\\
    3.000000 & 2.687737 & 2.687850 & 1.124689e-04 & 4.184521e-05\\
    \end{tabular}
    Derivative: f3
    \begin{tabular}{c|rrrr}
    $x$ & $d^2f_{ana}$ & $d^2f_{num}$ & $r^2_a$ & $r^2_r$\\
    \hline
    -3.000000 & -0.940205 & -0.940137 & 6.857098e-05 & 7.293191e-05\\
    -2.000000 & -0.280812 & -0.280775 & 3.615038e-05 & 1.287354e-04\\
    -1.000000 & 0.908182 & 0.908218 & 3.619826e-05 & 3.985794e-05\\
    0.000000 & 2.000000 & 2.000011 & 1.126771e-05 & 5.633855e-06\\
    1.000000 & 3.258584 & 3.258505 & 7.955705e-05 & 2.441461e-05\\
    2.000000 & 6.972909 & 6.973977 & 1.067689e-03 & 1.531196e-04\\
    3.000000 & 19.095544 & 19.094060 & 1.484760e-03 & 7.775426e-05\\
    \end{tabular}
    Derivative: f4
    \begin{tabular}{c|rrrr}
    $x$ & $d^2f_{ana}$ & $d^2f_{num}$ & $r^2_a$ & $r^2_r$\\
    \hline
    -3.000000 & -0.099574 & -0.099587 & 1.286857e-05 & 1.292361e-04\\
    -2.000000 & -0.135335 & -0.135336 & 9.034652e-07 & 6.675755e-06\\
    -1.000000 & 0.000000 & 0.000000 & 0.000000e+00 & 0.000000e+00\\
    0.000000 & 1.000000 & 1.000089 & 8.890058e-05 & 8.890058e-05\\
    1.000000 & 5.436564 & 5.436540 & 2.354993e-05 & 4.331768e-06\\
    2.000000 & 22.167168 & 22.168933 & 1.765059e-03 & 7.962492e-05\\
    3.000000 & 80.342148 & 80.342843 & 6.957965e-04 & 8.660417e-06\\
    \end{tabular}
    Derivative: f5
    \begin{tabular}{c|rrrr}
    $x$ & $d^2f_{ana}$ & $d^2f_{num}$ & $r^2_a$ & $r^2_r$\\
    \hline
    -3.000000 & 2.828857 & 2.828848 & 9.214997e-06 & 3.257498e-06\\
    -2.000000 & -0.077004 & -0.077161 & 1.567465e-04 & 2.035569e-03\\
    -1.000000 & -1.381773 & -1.381784 & 1.028577e-05 & 7.443893e-06\\
    0.000000 & 0.000000 & 0.000000 & 0.000000e+00 & 0.000000e+00\\
    1.000000 & 1.381773 & 1.381784 & 1.028577e-05 & 7.443893e-06\\
    2.000000 & 0.077004 & 0.077161 & 1.567465e-04 & 2.035569e-03\\
    3.000000 & -2.828857 & -2.828959 & 1.018073e-04 & 3.598884e-05\\
    \end{tabular}
    
    
    Output from: /usr/bin/python N03.py
  • N04.py:
    Python Listing: N04.py.
    from math import *
    
    from N01 import *
    from Derivatives import *
    from Isolate import *
    from Bisection import *
    from False_Position import *
    from Newton_Raphson import *
    from Secant import *
    
    xs=[-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0 ]
    
    fs=[
        f_1,f_2,f_3,f_4,f_5
    ]
    
    dfs=[
        df_1,df_2,df_3,df_4,df_5
    ]
    
    #Intervals deduced from Q2.
    Is=[
        [-1.0, 0.0 ],
        [ 2.0, 3.0 ],
        [ 0.0, 1.0 ],
        [ 1.0, 2.0 ],
        [ 1.0, 2.0 ],
    ]
    names=["f1","f2","f3","f4","f5"]
    
    titles1=[
        "\\multicolumn{3}{c}{}",
        "\\multicolumn{3}{c}{Bisection}",
        "\\multicolumn{3}{c}{False Position}",
        "\\multicolumn{3}{c}{Newton Raphson}",
        "\\multicolumn{3}{c}{Secant}",
    ]
    titles2=[
        "$f$",
        "$a$","$b$",
        "$I$","$x$","$f(x)$",
        "$I$","$x$","$f(x)$",
        "$I$","$x$","$f(x)$",
        "$I$","$x$","$f(x)$",
    ]
    
    print '\\begin{tabular}{crr|rrr|rrr|rrr|rrr}'
    print ' & '.join(titles1)+'\\\\'
    print ' & '.join(titles2)+'\\\\\n\\hline'
    
    for n in range( len(dfs) ):
        f=fs[n]
        df=dfs[n]
    
        a=Is[n][0]
        b=Is[n][1]
        
    
        text=[
            names[n],
            "%.6f" % a,
            "%.6f" % b,
        ]
    
        xs=Bisection(f,a,b)
        x=xs[ len(xs)-1 ]
        
        text.append( "%d"   % len(xs) )
        text.append( "%.6f" % x)
        text.append( "%.6e" % abs(f(x)) )
        
        xs=False_Position(f,a,b)
        x=xs[ len(xs)-1 ]
        
        text.append( "%d"   % len(xs) )
        text.append( "%.6f" % x)
        text.append( "%.6e" % abs(f(x)) )
    
        xs=Newton_Raphson(f,df,a)
        x=xs[ len(xs)-1 ]
        
        text.append( "%d"   % len(xs) )
        text.append( "%.6f" % x)
        text.append( "%.6e" % abs(f(x)) )
    
        xs=Secant(f,a,b)
        x=xs[ len(xs)-1 ]
        
        text.append( "%d"   % len(xs) )
        text.append( "%.6f" % x)
        text.append( "%.6e" % abs(f(x)) )
    
        print " & ".join(text)+'\\\\'
            
    print '\\end{tabular}'
    
    \begin{tabular}{crr|rrr|rrr|rrr|rrr}
    \multicolumn{3}{c}{} & \multicolumn{3}{c}{Bisection} & \multicolumn{3}{c}{False Position} & \multicolumn{3}{c}{Newton Raphson} & \multicolumn{3}{c}{Secant}\\
    $f$ & $a$ & $b$ & $I$ & $x$ & $f(x)$ & $I$ & $x$ & $f(x)$ & $I$ & $x$ & $f(x)$ & $I$ & $x$ & $f(x)$\\
    \hline
    f1 & -1.000000 & 0.000000 & 20 & -0.929837 & 1.564530e-07 & 6 & -0.929837 & 1.100318e-07 & 5 & -0.929837 & 0.000000e+00 & 7 & -0.929837 & 2.791980e-10\\
    f2 & 2.000000 & 3.000000 & 19 & 2.498755 & 7.070456e-07 & 2 & 2.498756 & 9.697914e-07 & 5 & 2.498756 & 0.000000e+00 & 6 & 2.498756 & 5.012699e-09\\
    f3 & 0.000000 & 1.000000 & 18 & 0.601347 & 4.825563e-07 & 8 & 0.601347 & 4.480312e-07 & 7 & 0.601347 & 2.220446e-16 & 8 & 0.601347 & 1.633400e-10\\
    f4 & 1.000000 & 2.000000 & 19 & 1.463056 & 6.147964e-07 & 5 & 1.463055 & 9.453087e-08 & 7 & 1.463056 & 8.881784e-16 & 8 & 1.463055 & 1.278058e-07\\
    f5 & 1.000000 & 2.000000 & 19 & 1.570796 & 4.930988e-07 & 5 & 1.570796 & 7.479757e-07 & 6 & 1.570796 & 1.110223e-16 & 7 & 1.570796 & 2.239831e-09\\
    \end{tabular}
    
    
    Output from: /usr/bin/python N04.py
< 2017.2.0 | 2017.2.1 | 2017.2.2 >
Messages:
0 secs.