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

Zeros
Vive como se fosse morrer amanhã.
Estude como se fosse viver para sempre.
Einstein
< Secante | Atividades 1.6: | Derivação >

Atividade #1.6-01:

Crie um arquivo, Functions4.py, implementando as seguintes funções:
  1. \(f_{1}(x)=e^{-x^2}-\cos{x}, \varphi_1(x)=\cos{x}-e^{-x^2}+x, a=x_0=1.0, b=x_1=2.0;\)
  2. \(f_{2}(x)=x^3-x-1, \varphi_2(x)=\sqrt[3]{x+1}, a=x_0=1.0, b=x_1=2.0;\)
  3. \(f_{3}(x)=4 \sin{x}-e^x, \varphi_3(x)=x-2\sin{x}+0.5e^x, a=x_0=0, b=x_1=1;\)
  4. \(f_4(x)=x \log_{10} x, \varphi_4(x)= x-1.3(x \log_{10}x-1), a=x_0=2.0, b=x1=3.0.\)
Crie um executavel, Run.All.py, executando todos os algoritmos implementados.
Python Listing: Functions4.py.
from math import *

def f1(x):
    return exp(-x**2.0)-cos(x)

def df1(x):
    return -2.0*x*exp(-x**2.0)+sin(x)

def phi1(x):
    return cos(x)-exp(-x**2.0)+x

def f2(x):
    return x**3.0-x-1.0

def df2(x):
    return 3.0*x**2.0-1.0

def phi2(x):
    return (x+1)**(1.0/3.0)

def f3(x):
    return 4.0*sin(x)-exp(x)

def df3(x):
    return 4.0*cos(x)-exp(x)

def phi3(x):
    return x-2.0*sin(x)+0.5*exp(x)

def f4(x):
    return x*log10(x)-1.0
    
def df4(x):
    return log10(x)+1.0/log(10)

def phi4(x):
    return x-1.3*(  x*log10(x)-1  )
Python Listing: 01.py.
from Functions4 import *

from Isolate        import Isolate_Root
from Bisection      import Bisection
from False_Position import False_Position
from Fixed_Point    import Fixed_Point
from Newton_Raphson import Newton_Raphson
from Secant         import Secant


fs=[f1,f2,f3,f4]
dfs=[df1,df2,df3,df4]
phis=[phi1,phi2,phi3,phi4]
x0s=[1.0,1.0,0.0,2.0]
x1s=[2.0,2.0,1.0,3.0]

n=0
for f in fs:
    print "f_"+str(n+1)+":"

    xm=0.5*(x0s[n]+x1s[n])
    
    xs=Bisection(f,x0s[n],x1s[n],1.0E-6)
    print "Bisection"+"\t"+str( len(xs) )+"\t"+str( xs[ len(xs)-1 ] )
    
    xs=False_Position(f,x0s[n],x1s[n],1.0E-6)
    print "False Position"+"\t"+str( len(xs) )+"\t"+str( xs[ len(xs)-1 ] )
    
    xs=Fixed_Point(f,phis[n], xm  ,1.0E-6)
    print "Fixed Point"+"\t"+str( len(xs) )+"\t"+str( xs[ len(xs)-1 ] )
    
    xs=Newton_Raphson(f,dfs[n], xm  ,1.0E-6)
    print "Newton Raphson"+"\t"+str( len(xs) )+"\t"+str( xs[ len(xs)-1 ] )

    xs=Secant(f,x0s[n],x1s[n]  ,1.0E-6)
    print "Met. Secant"+"\t"+str( len(xs) )+"\t"+str( xs[ len(xs)-1 ] )

    
    n+=1
Warning! Unable to pipe system command: cd /usr/local/Slides/1_Disciplines/1_CN/1_Functions/0_Zeros/99_Activities; /usr/bin/python 01.py
Output from: /usr/bin/python 01.py
< Secante | Atividades 1.6: | Derivação >
Messages:
0 secs.