Exemplo n.º 1
0
from communs import util

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)

if __name__ == "__main__":
    x0 = -10.0
    x1 = 5.0
    approx, roots, iteration = sec(x0, x1)
    util.plotChart('Secante', approx, roots, iteration)
Exemplo n.º 2
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)


if __name__ == "__main__":

    a = -10.0
    b = 5.0

    approx, roots, iteration = fakePosition(a, b)
    util.plotChart('fake_position', approx, roots, iteration)
Exemplo n.º 3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from communs import util

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)

if __name__ == "__main__":
    x0 = -10.0
    approx, roots, iteration = newtonRaphson(x0)
    print roots
    util.plotChart('newton_raphson', approx, roots, iteration)
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)


if __name__ == "__main__":

    a = -10.0
    b = 5.0

    approx, roots, iteration = bisection(a, b)

    util.plotChart('Bisection', approx, roots, iteration)
Exemplo n.º 5
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


if __name__ == "__main__":
    a = -2.0
    b = 1.0
    i = 0
    t = 20
    d = abs(a - b)
    values = []
    for i in range(1, t):
        interval = []
        j = 0
        while j <= i:
            interval.append(j * d / i + a)
            j += 1

        values.append((i, trapezio(interval)))

    iteration = [i[0] for i in values]
    aprox = [i[1] for i in values]

    util.plotChart('trapezio', aprox, [], iteration)

    util.plotFuncChart('x^4 + 2x^3 - 13x^2 - 14x + 24', util.f, -6, 5)
Exemplo n.º 6
0
        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


if __name__ == "__main__":
    a = -2.0
    b = 1.0
    i = 0
    t = 20.0
    d = abs(a - b)
    values = []

    for i in range(1, 20):
        j = 0
        interval = []
        while j <= i:
            interval.append(j * d / i + a)
            j += 1

        values.append((i, integration(interval)))

    iteration = [i[0] for i in values]
    aprox = [i[1] for i in values]

    util.plotChart('1_3_Simpson', aprox, [], iteration)