#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 10 09:17:36 2020

@author: suwako
"""

import scipy
import numpy as np
def dichotomie(f, a, b, epsilon=1e-15):
    g, d = a, b
    while abs(g - d) > 2*epsilon:
        m = (g+d)/2.
        if f(g)*f(m) <= 0:
            d = m
        else:
            g = m
    return (g+d)/m

def newton(f, fp, x0, nbiter):
    u1 = x0
    u2 = u1 - f(u1)/fp(u1)
    epsilon = 1e-8
    for i in range(nbiter):
        if abs(u1 - u2) < epsilon :
            return (u2, f(u2), nbiter, u1 - u2)
        u1, u2 = u2, u2 - f(u2)/fp(u2)
    raise