Пример #1
0
def func(t, args):
    # args=[f,y0,x,h,arg,i]
    cop = [_ for _ in args[1]]
    i = args[5]
    cop[i] = t
    c = call_func(args[0], args[4], cop, args[2] + args[3])
    if type(c) != tuple and type(c) != list: c = [c]
    return -t + args[1][i] + args[3] * c[i]
Пример #2
0
def rk2(f, y0, x0, xstop, N, args=None):
    h = (xstop - x0) / N
    result = [y0]
    c = [x0]
    iter = 0
    while iter < N:
        iter += 1
        d = call_func(f, args, result[-1], x0)
        k1_by_2 = h * d / 2
        yn = result[-1] + k1_by_2
        d = call_func(f, args, yn, x0 + h / 2)
        k2 = h * d
        yn = result[-1] + k2
        result.append(yn)
        x0 += h
        c.append(x0)
    return c, result
Пример #3
0
def rk2(f, y0, x0, xstop, N=1000, args=None):
    h = (xstop - x0) / N
    if type(y0) != list and type(y0) != tuple: y0 = [y0]
    result = [y0]
    c = [x0]
    while abs(x0 - xstop) >= abs(h):
        d = call_func(f, args, result[-1], x0)
        if type(d) != list and type(d) != tuple: d = [d]
        k1_by_2 = mul(h / 2, d)
        yn = add(result[-1], k1_by_2)
        d = call_func(f, args, yn, x0 + h / 2)
        if type(d) != list: d = [d]
        k2 = mul(h, d)
        yn = add(result[-1], k2)
        result.append(yn)
        x0 += h
        c.append(x0)
    return result, c
Пример #4
0
def predictor_corrector(f, y0, x0, xstop, N, args=None):
    h = (xstop - x0) / N
    result = [y0]
    c = [x0]
    iter = 0
    while iter < N:
        iter += 1
        d = call_func(f, args, result[-1], x0)
        k1 = h * d
        yp = result[-1] + k1  #predicted
        d = call_func(f, args, yp, x0 + h)
        k2_p = h * d  #predicted k2
        k = (k2_p + k1) / 2  # h(f(y0,x0) +f(y1_p,x0+h)
        yc = result[-1] + k  #corrected
        result.append(yc)
        x0 += h
        c.append(x0)
    return c, result
Пример #5
0
def predictor_corrector(f, y0, x0, xstop, N=1000, args=None):
    h = (xstop - x0) / N
    if type(y0) != list: y0 = [y0]
    result = [y0]
    c = [x0]
    while abs(x0 - xstop) >= abs(h):
        d = call_func(f, args, result[-1], x0)
        if type(d) != list and type(d) != tuple: d = [d]
        k1 = mul(h, d)
        yp = add(result[-1], k1)  #predicted
        d = call_func(f, args, yp, x0 + h)
        if type(d) != list: d = [d]
        k2_p = mul(h, d)
        k_into_2 = add(k2_p, k1)  # h(f(y0,x0) +f(y1_p,x0+h)
        k = mul(0.5, k_into_2)
        yc = add(result[-1], k)  #corrected
        result.append(yc)
        x0 += h
        c.append(x0)
    return result, c
Пример #6
0
def rk4(f, y0, x0, xstop, N, args=None):
    h = (xstop - x0) / N
    if type(y0) != list and type(y0) != tuple: y0 = [y0]
    prev_result = y0
    y = [[prev_result[i]] for i in range(len(y0))]
    c = [x0]
    iter = 0
    while iter < N:
        iter += 1
        #finding k1/2 and k1/6
        d = call_func(f, args, prev_result, x0)
        if type(d) != list and type(d) != tuple: d = [d]
        k1_by_2 = mul(h / 2, d)
        k1_by_6 = mul(h / 6, d)
        #finding k2/2 and k2/3
        yn = add(prev_result, k1_by_2)
        d = call_func(f, args, yn, x0 + h / 2)
        if type(d) != list: d = [d]
        k2_by_2 = mul(h / 2, d)
        k2_by_3 = mul(h / 3, d)
        #finding k3
        yn = add(prev_result, k2_by_2)
        d = call_func(f, args, yn, x0 + h / 2)
        if type(d) != list: d = [d]
        k3 = mul(h, d)
        k3_by_3 = mul(h / 3, d)
        #finding k4
        yn = add(prev_result, k3)
        d = call_func(f, args, yn, x0 + h)
        if type(d) != list: d = [d]
        k4_by_6 = mul(h / 6, d)
        #finding the correct yn
        yn = add(prev_result, k1_by_6, k2_by_3, k3_by_3, k4_by_6)
        prev_result = yn
        for i in range(len(y0)):
            y[i].append(prev_result[i])
        x0 += h
        c.append(x0)
    return c, y
Пример #7
0
def euler_forward(f, y0, x0, xstop, N=1000, args=None):
    h = (xstop - x0) / N
    if type(y0) != list: y0 = [y0]
    result = [y0]
    c = [x0]
    while abs(x0 - xstop) >= abs(h):
        d = call_func(f, args, result[-1], x0)
        if type(d) != list: d = [d]
        k1 = mul(h, d)
        yn = add(result[-1], k1)
        result.append(yn)
        x0 += h
        c.append(x0)
    return result, c
Пример #8
0
def euler_forward(f, y0, x0, xstop, N, args=None):
    h = (xstop - x0) / N
    result = [y0]
    c = [x0]
    iter = 0
    while iter < N:
        iter += 1
        d = call_func(f, args, result[-1], x0)
        k1 = h * d
        yn = result[-1] + k1
        result.append(yn)
        x0 += h
        c.append(x0)
    return c, result
Пример #9
0
def rk4(f, y0, x0, xstop, N=1000, args=None):
    h = (xstop - x0) / N
    if type(y0) != list and type(y0) != tuple: y0 = [y0]
    result = [y0]
    c = [x0]
    while abs(x0 - xstop) >= abs(h):
        #finding k1 and k1/2
        d = call_func(f, args, result[-1], x0)
        if type(d) != list and type(d) != tuple: d = [d]
        k1 = mul(h, d)
        k1_by_2 = mul(h / 2, d)
        k1_by_6 = mul(h / 6, d)
        #finding k2 and k2/2
        yn = add(result[-1], k1_by_2)
        d = call_func(f, args, yn, x0 + h / 2)
        if type(d) != list: d = [d]
        k2 = mul(h, d)
        k2_by_2 = mul(h / 2, d)
        k2_by_3 = mul(h / 3, d)
        #finding k3
        yn = add(result[-1], k2_by_2)
        d = call_func(f, args, yn, x0 + h / 2)
        if type(d) != list: d = [d]
        k3 = mul(h, d)
        k3_by_3 = mul(h / 3, d)
        #finding k4
        yn = add(result[-1], k3)
        d = call_func(f, args, yn, x0 + h)
        if type(d) != list: d = [d]
        k4 = mul(h, d)
        k4_by_6 = mul(h / 6, d)
        #finding the correct yn
        yn = add(result[-1], k1_by_6, k2_by_3, k3_by_3, k4_by_6)
        result.append(yn)
        x0 += h
        c.append(x0)
    return result, c
Пример #10
0
def euler_backward(f, y0, x0, xstop, N, args=None):
    h = (xstop - x0) / N
    result = [y0]
    xpar = [x0]
    iter = 0
    while iter < N:
        iter += 1
        list_of_args = [f, result[-1], x0, h, args]
        sol = newton_raphson(func, result[-1], 0.0001, list_of_args)
        d = call_func(f, args, sol, x0 + h)
        d1 = h * d
        d2 = d1 + result[-1]
        result.append(d2)
        x0 += h
        xpar.append(x0)
    return xpar, result
Пример #11
0
def euler_backward(f, y0, x0, xstop, N=1000, args=None):
    h = (xstop - x0) / N
    if type(y0) != tuple and type(y0) != list: y0 = [y0]
    result = [y0]
    xpar = [x0]
    while (abs(xstop - x0) >= abs(h)):
        list_of_args = [f, result[-1], x0, h, args, 0]
        newtonn = []
        for i in range(len(y0)):
            list_of_args[5] = i
            sol = newton_raphson(func, result[-1][i], 0.0001, list_of_args)
            newtonn.append(sol)

        #result.append(result[-1]+h*call_func(f,args,newton,x0+h))
        d = call_func(f, args, newtonn, x0 + h)
        if type(d) != tuple and type(d) != list: d = [d]
        d1 = mul(h, d)
        d2 = add(d1, result[-1])
        result.append(d2)
        x0 += h
        xpar.append(x0)
    return result, xpar
Пример #12
0
def func(t, args):
    # args=[f,y0,x,h,arg]
    c = call_func(args[0], args[4], t, args[2] + args[3])
    return -t + args[1] + args[3] * c