Exemplo n.º 1
0
def trapezio(interval):
    result = 0
    a = interval[0]
    b = interval[1]
    for i in range(0, len(interval) - 2):
        result += abs(a - b) / 2 * (util.f(a) + util.f(b))
        a = b
        b = interval[i + 2]

    return result
Exemplo n.º 2
0
def integration(interval):

    result = 0.0
    a = interval[0]
    b = interval[1]
    for i in range(0, len(interval) - 2):
        med = (a + b) / 2.0
        result += abs(a - b) / 3.0 * (util.f(a) + 4.0 * util.f(med) +
                                      util.f(b))
        a = b
        b = interval[i + 2]

    return result / 2
Exemplo n.º 3
0
def newtonRaphson(x0):
    n = 0
    x = x0
    approximation = []
    roots = []
    while n < util.MAX:
        xAux = x - util.f(x)/ util.f_(x)
        fxAux = util.f(xAux)
        approximation.append(abs(fxAux))
        roots.append(xAux)
        n += 1
        if (fxAux == 0 or abs(x - xAux) < util.EPSON):
            return approximation, roots, range(1, n+3)
        x = xAux
    return approximation, roots, range(1, n+3)
Exemplo n.º 4
0
def bisection(a, b):
    n = 0
    approximation = []
    roots = []
    while n < util.MAX:
        c = (a + b) / 2
        fc = util.f(c)
        fa = util.f(a)
        approximation.append(abs(fc))
        roots.append(c)
        n += 1
        if fc == 0 or abs(b - a) / 2 < util.EPSON:
            return approximation, roots, range(1, n + 3)
        if fa * fc > 0:
            a = c
        else:
            b = c

    return approximation, roots, range(1, n + 3)
Exemplo n.º 5
0
def sec(x0, x1):
    n = 0
    f0 = util.f(x0)
    f1 = util.f(x1)
    x = 0.0
    approximation = []
    roots = []
    while n < util.MAX:
        print n
        x = x1 - (x1 - x0) * f1 / (f1 - f0)
        fx = util.f(x)
        approximation.append(fx)
        roots.append(x)
        n += 1
        if (fx == 0 or abs(x - x1) < util.EPSON):
            return approximation, roots, range(1, n+3)
        x0 = x1
        x1 = x
        f0 = util.f(x0)
        f1 = util.f(x1)

    return approximation, roots, range(1, n+3)
Exemplo n.º 6
0
def fakePosition(a, b):
    n = 0
    approximation = []
    roots = []
    while n < util.MAX:
        fa = util.f(a)
        fb = util.f(b)
        if (fb - fa) == 0:
            return approximation, roots, range(1, n + 3)
        c = b - ((fb * (b - a)) / (fb - fa))
        fc = util.f(c)
        approximation.append(abs(fc))
        roots.append(c)
        n += 1
        print c, fc
        if abs(fc) <= util.EPSON or abs(b - a) <= util.EPSON:
            return approximation, roots, range(1, n + 3)
        if fa * fc > 0:
            a = c
        else:
            b = c

    return approximation, roots, range(1, n + 3)