<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Departamento de Física - Faculdade de Ciências e Tecnologia da Universidade de Coimbra
Física Computacional - Ficha 2 - Zeros de Funções
Rafael Isaque Santos - 2012144694 - Licenciatura em Física
Step1:  Define-se a nossa função
Step2:  Método da Bisecção
Step3:  Método da Falsa Posição
Step4:  Método de Newton-Raphson
Step5:  Método da Secante
Step6:  Exercício 2
$f(x) = x + e^{-k x^{2}}$
<ASSISTANT_TASK:>
Python Code:
import numpy as np
%matplotlib inline
Explanation: Departamento de Física - Faculdade de Ciências e Tecnologia da Universidade de Coimbra
Física Computacional - Ficha 2 - Zeros de Funções
Rafael Isaque Santos - 2012144694 - Licenciatura em Física
End of explanation
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
Explanation: Define-se a nossa função:
$f(x) = \sin (x)$
e a sua derivada:
$f'(x) = \cos (x)$
End of explanation
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)
bi(2, 4, f, my_stop, my_nitmax)
Explanation: Método da Bisecção
End of explanation
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)
regfalsi(2, 4, f, my_stop, my_nitmax)
Explanation: Método da Falsa Posição:
End of explanation
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)
newtraph(2, f, df, my_stop, my_nitmax)
Explanation: Método de Newton-Raphson:
End of explanation
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)
secant(2, 4, f, my_stop, my_nitmax)
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)
bi2(2, 4, f, my_stop, my_nitmax, my_cdif)
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)
regfalsi2(2, 4, f, my_stop, my_nitmax, my_cdif)
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)
newtraph2(2, f, df, my_stop, my_nitmax, my_cdif)
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)
secant2(2, 4, f, my_stop, my_nitmax, my_cdif)
Explanation: Método da Secante
End of explanation
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)
f2 = lambda x, k: x + np.e ** (-k * x**2) * np.cos(x)
f2_k1 = lambda x: f2(x, 1)
newtraph(0, f2_k1, df2_k1, 1e-4, my_nitmax)
f2_k50 = lambda x: f2(x, 50)
for i in range(1, 10+1): print(newtraphd(0, f2_k50, 1e-4, i))
for i in range(1, 10+1): print(newtraphd(-0.1, f2_k50, 1e-4, i))
Explanation: Exercício 2
$f(x) = x + e^{-k x^{2}}$
End of explanation
<END_TASK>