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
O dia que bosta valesse ouro.
Os pobres todos nascerão sem c*.
Grafitti.

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

PDF
  • 1:
    Python Listing: N01.py.
    from math import *
    
    
    def f_1(x):
        return x*log(x)-x
    
    def f_2(x):
        return x**2*cos(x)
    
    def f_3(x):
        return x-sin(x)-1.0
    
    def f_4(x):
        return x**3-3.0*x**2+x-2
    
    def f_5(x):
        return -2.0-cos(x)+e**x
    
    def df_1(x):
        return log(x)
    
    def df_2(x):
        return 2.0*x*cos(x)-x**2*sin(x)
    
    def df_3(x):
        return 1.0-cos(x)
    
    def df_4(x):
        return 3.0*x**2-6.0*x+1.0
    
    def df_5(x):
        return sin(x)+e**x
    
    fs=[f_1,f_2,f_3,f_4,f_5]
    
    xs=[
        0.25,1.0/3.0,0.5, 1.0, 2.0, 3.0,4.0
    ]
    
    titles=["x","f\_1(x)","f\_2(x)","f\_3(x)","f\_4(x)","f\_5(x)"]
    
    print '\\begin{tabular}{cccccc}'
    print ' & '.join(titles)+'\\\\'
    
    for x in xs:
        text=[ "%.6f" % x  ]
        for f in fs:
            text.append( "%.6f" % f(x) )
        print " & ".join(text)+'\\\\'
    print '\end{tabular}'
    
    \begin{tabular}{cccccc}
    x & f\_1(x) & f\_2(x) & f\_3(x) & f\_4(x) & f\_5(x)\\
    0.250000 & -0.596574 & 0.060557 & -0.997404 & -1.921875 & -1.684887\\
    0.333333 & -0.699537 & 0.104995 & -0.993861 & -1.962963 & -1.549345\\
    0.500000 & -0.846574 & 0.219396 & -0.979426 & -2.125000 & -1.228861\\
    1.000000 & -1.000000 & 0.540302 & -0.841471 & -3.000000 & 0.177980\\
    2.000000 & -0.613706 & -1.664587 & 0.090703 & -4.000000 & 5.805203\\
    3.000000 & 0.295837 & -8.909932 & 1.858880 & 1.000000 & 19.075529\\
    4.000000 & 1.545177 & -10.458298 & 3.756802 & 18.000000 & 53.251794\\
    \end{tabular}
    
    
    Output from: /usr/bin/python N01.py
  • 2:
    Python Listing: N02.py.
    from math import *
    from Residual import *
    from Derivatives import *
    from N01 import *
    
    
    
    
    dfs=[df_1,df_2,df_3,df_4,df_5]
    
    titles=["$x$","$df_{ana}$","$df_{num}$","$r_a$","$r_r$"]
    
    
    for i in range( len(dfs) ):
        print "f_"+str(i+1)
    
        print '\\begin{tabular}{lllll}'
        print ' & '.join(titles)+'\\\\'
        
        f=fs[i]
        df=dfs[i]
        
        for x in xs:
            text=[ "%.6f" % x ]
            df_ana=df(x)
            df_num=d_f(f,x,1.0E-6)
            
            r_abs=R_Abs(df_ana,df_num)
            r_rel=R_Rel(df_ana,df_num)
            
            print '%.6f & %.6f & %.6f & %.6e & %.6e\\\\' % (x,df_ana,df_num,r_abs,r_rel)
    
        print '\end{tabular}'
    
    Warning! Unable to pipe system command: cd /usr/local/Slides/1_Disciplines/1_CN/9_Tests/2017.2.0; /usr/bin/python N02.py
    
    Output from: /usr/bin/python N02.py
  • 3:
    Python Listing: N03.py.
    from math import *
    
    from N01 import *
    
    from Isolate import *
    from Bisection import Bisection
    from False_Position import False_Position
    from Newton_Raphson import Newton_Raphson
    
    
    ass=[2.0,1.0,1.0,2.0,0.5,]
    bss=[3.0,2.0,3.0,3.0,1.0,]
    
    fs=[f_1,f_2,f_3,f_4,f_5]
    names=["f_1","f_2","f_3","f_4","f_5"]
    
    print "\n\nEx 3\n"
    
    print "\nBisection\n"
    
    eps=1.0E-3
    
    for n in range( len(fs) ):
        f=fs[n]
        a=ass[n]
        b=bss[n]
        name=names[n]
        
        xs=Bisection(f,a,b,eps)
    
        x=xs[ len(xs)-1 ]
        text=[
            name,
            "%.6f" %a,"%.6f" %b,
            "%d" %len(xs),
            "%.6f" %x,
            "%.6e" % f(x)
        ]
        print "\t".join(text)
    
    print "\nFalse Position\n"
    
    for n in range( len(fs) ):
        f=fs[n]
        a=ass[n]
        b=bss[n]
        name=names[n]
        
        xs=False_Position(f,a,b,eps)
    
        x=xs[ len(xs)-1 ]
        text=[
            name,
            "%.6f" %a,"%.6f" %b,
            "%d" %len(xs),
            "%.6f" %x,
            "%.6e" % f(x)
        ]
        print "\t".join(text)
        
    print "\nNewton Raphson\n"
    
    dfs=[df_1,df_2,df_3,df_4,df_5]
    
    for n in range( len(fs) ):
        f=fs[n]
        df=dfs[n]
        
        x0=ass[n]
        name=names[n]
        
        xs=Newton_Raphson(f,df,x0,eps)
    
        x=xs[ len(xs)-1 ]
        text=[
            name,
            "%.6f" %a,"%.6f" %b,
            "%d" %len(xs),
            "%.6f" %x,
            "%.6e" % f(x)
        ]
        print "\t".join(text)
    
    \begin{tabular}{cccccc}
    x & f\_1(x) & f\_2(x) & f\_3(x) & f\_4(x) & f\_5(x)\\
    0.250000 & -0.596574 & 0.060557 & -0.997404 & -1.921875 & -1.684887\\
    0.333333 & -0.699537 & 0.104995 & -0.993861 & -1.962963 & -1.549345\\
    0.500000 & -0.846574 & 0.219396 & -0.979426 & -2.125000 & -1.228861\\
    1.000000 & -1.000000 & 0.540302 & -0.841471 & -3.000000 & 0.177980\\
    2.000000 & -0.613706 & -1.664587 & 0.090703 & -4.000000 & 5.805203\\
    3.000000 & 0.295837 & -8.909932 & 1.858880 & 1.000000 & 19.075529\\
    4.000000 & 1.545177 & -10.458298 & 3.756802 & 18.000000 & 53.251794\\
    \end{tabular}
    
    
    Ex 3
    
    
    Bisection
    
    f_1    2.000000    3.000000    4    2.718750    4.682119e-04
    f_2    1.000000    2.000000    10    1.570801    -1.099099e-05
    f_3    1.000000    3.000000    10    1.934570    9.628553e-06
    f_4    2.000000    3.000000    11    2.893311    1.868976e-04
    f_5    0.500000    1.000000    9    0.948730    -2.861739e-04
    
    False Position
    
    f_1    2.000000    3.000000    2    2.717749    -5.326417e-04
    f_2    1.000000    2.000000    6    1.570618    4.407991e-04
    f_3    1.000000    3.000000    2    1.933856    -9.587618e-04
    f_4    2.000000    3.000000    4    2.893280    -7.936443e-05
    f_5    0.500000    1.000000    2    0.948698    -3.956837e-04
    
    Newton Raphson
    
    f_1    0.500000    1.000000    5    2.718282    2.911449e-12
    f_2    0.500000    1.000000    7    -1.570796    -1.680804e-08
    f_3    0.500000    1.000000    6    1.934563    1.545697e-11
    f_4    0.500000    1.000000    8    2.893289    1.306660e-07
    f_5    0.500000    1.000000    5    0.948815    9.193482e-10
    
    
    Output from: /usr/bin/python N03.py
Messages:
0 secs.