Exemple #1
0
def square_integration(file):
    result = 0
    n = read_json(file, "number")
    start, end = read_json(file, "range")
    function = read_json(file, "function")
    dx = (end - start) / n
    for x in arange(start + dx, end + dx, dx):
        pol = polyval(function, x)
        result = result + pol
    return result * dx
Exemple #2
0
def nm_method():
    a, b = read_json("range")
    accuracy = read_json("accuracy")
    x = read_json("start")
    x1 = x - (fun(x) / pfun(x))
    while abs(fun(x)) >= accuracy and abs(x1 - x) >= accuracy:
        x = x1
        x1 = x - (fun(x) / pfun(x))

    return x1
Exemple #3
0
def trapeze_integration(file):
    result = 0
    n = read_json(file, "number")
    start, end = read_json(file, "range")
    function = read_json(file, "function")
    dx = (end - start) / n
    for x in arange(start, end, dx):
        pol = (polyval(function, x) + polyval(function, x + dx)) / 2
        result = result + dx * pol
    return result
Exemple #4
0
def monte_carlo_integration(file):
    result = 0.0
    n = read_json(file, "number")
    start, end = read_json(file, "range")
    function = read_json(file, "function")
    generated_points = np.random.uniform(start, end, n)
    # generated_points = [1.5, 2.6, 3.8, 4.5]
    for x in generated_points:
        result += polyval(function, x)

    return result / n * abs(end - start)
Exemple #5
0
def simpson_integration(file):
    result = 0.0
    n = read_json(file, "number") * 2
    start, end = read_json(file, "range")
    function = read_json(file, "function")
    h = (end - start) / n
    for x in arange(start, end, 2 * h):
        pol = polyval(function, x) + 4 * polyval(function, x + h) + polyval(
            function, x + 2 * h)
        result = result + h / 3 * pol
    return result
Exemple #6
0
def bisection():
    a, b = read_json("range")
    accuracy = read_json("accuracy")
    x = a + b / 2
    while abs(a - b) > accuracy:
        if fun(x) == 0:
            return x
        if fun(x) * fun(a) < 0:
            b = x
        else:
            a = x
        x = (a + b) / 2
    return x
Exemple #7
0
def fun(x):
    y = 0
    function = read_json("function")
    for i in range(0, len(function)):
        y = y + function[i] * (x**(len(function) - 1 - i))
    return y
Exemple #8
0
def pfun(x):
    y = 0
    function = read_json("derivative")
    for i in range(0, len(function)):
        y = y + function[i] * (x**(len(function) - 1 - i))
    return y