In [1]:
import numpy as np
%matplotlib inline
Define-se a nossa função:
$f(x) = \sin (x)$
e a sua derivada:
$f'(x) = \cos (x)$
In [2]:
f = lambda x: np.sin(x)
df = lambda x: np.cos(x)
my_stop = 1.e-4
my_nitmax = 100000
my_cdif = 1.e-6
Método da Bisecção
In [3]:
def bi(a, b, fun, eps, nitmax):
c = (a + b) / 2
it = 1
while np.abs(fun(c)) > eps and it < nitmax:
if fun(a)*fun(c) < 0: b = c
else: a = c
c = (a + b) / 2
it += 1
return it, c, fun(c)
In [4]:
bi(2, 4, f, my_stop, my_nitmax)
Out[4]:
Método da Falsa Posição:
In [5]:
def regfalsi(a, b, fun, eps, nitmax):
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
it = 1
while np.abs(fun(c)) > eps and it < nitmax:
if fun(a) * fun(c) < 0: b = c
else: a = c
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
it += 1
return it, c, fun(c)
In [6]:
regfalsi(2, 4, f, my_stop, my_nitmax)
Out[6]:
Método de Newton-Raphson:
In [7]:
def newtraph(c0, fun, dfun, eps, nitmax):
c = c0
it = 1
while np.abs(fun(c)) > eps and it < nitmax:
c = c - fun(c) / dfun(c)
it += 1
return it, c, fun(c)
In [8]:
newtraph(2, f, df, my_stop, my_nitmax)
Out[8]:
Método da Secante
In [9]:
def secant(a, b, fun, eps, nitmax):
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
it = 1
while np.abs(fun(c)) > eps and it < nitmax:
a = b
b = c
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
it += 1
return it, c, fun(c)
In [10]:
secant(2, 4, f, my_stop, my_nitmax)
Out[10]:
In [11]:
def bi2(a, b, fun, eps, nitmax, cdif):
c = (a + b) / 2
c_prev = a
it = 1
while not((np.abs(fun(c)) < eps and np.abs(c - c_prev) < cdif) or it > nitmax):
if fun(a)*fun(c) < 0: b = c
else: a = c
c_prev = c
c = (a + b) / 2
it += 1
return it, c, fun(c), c_prev, fun(c_prev), (c - c_prev)
In [12]:
bi2(2, 4, f, my_stop, my_nitmax, my_cdif)
Out[12]:
In [13]:
def regfalsi2(a, b, fun, eps, nitmax, cdif):
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
c_prev = c + cdif/2
it = 1
while not((np.abs(fun(c)) < eps and np.abs(c - c_prev) < cdif) or it > nitmax):
if fun(a) * fun(c) < 0: b = c
else: a = c
c_prev = c
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
it += 1
return it, c, fun(c), c_prev, fun(c_prev), (c - c_prev)
In [14]:
regfalsi2(2, 4, f, my_stop, my_nitmax, my_cdif)
Out[14]:
In [15]:
def newtraph2(c0, fun, dfun, eps, nitmax, cdif):
c = c0
c_prev = c + cdif/2
it = 1
while not((np.abs(fun(c)) < eps and np.abs(c - c_prev) < cdif) or it > nitmax):
c_prev = c
c = c - fun(c) / dfun(c)
it += 1
return it, c, fun(c), c_prev, fun(c_prev), (c - c_prev)
In [16]:
newtraph2(2, f, df, my_stop, my_nitmax, my_cdif)
Out[16]:
In [17]:
def secant2(a, b, fun, eps, nitmax, cdif):
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
c_prev = c + cdif/2
it = 1
while not((np.abs(fun(c)) < eps and np.abs(c - c_prev) < cdif) or it > nitmax):
a = b
b = c
c_prev = c
c = (a * fun(b) - b * fun(a)) / (fun(b) - fun(a))
it += 1
return it, c, fun(c), c_prev, fun(c_prev), (c - c_prev)
In [18]:
secant2(2, 4, f, my_stop, my_nitmax, my_cdif)
Out[18]:
In [107]:
from scipy.misc import derivative
def newtraphd(c0, fun, eps, nitmax):
c = c0
dfun = lambda x: derivative(fun, x, 0.0001)
it = 1
while np.abs(fun(c)) > eps and it < nitmax:
c = c - (fun(c) / dfun(c))
it += 1
return it, c, fun(c), dfun(c)
In [97]:
f2 = lambda x, k: x + np.e ** (-k * x**2) * np.cos(x)
In [105]:
f2_k1 = lambda x: f2(x, 1)
newtraph(0, f2_k1, df2_k1, 1e-4, my_nitmax)
Out[105]:
In [122]:
f2_k50 = lambda x: f2(x, 50)
for i in range(1, 10+1): print(newtraphd(0, f2_k50, 1e-4, i))
In [123]:
for i in range(1, 10+1): print(newtraphd(-0.1, f2_k50, 1e-4, i))