def solve_equation(k, m, x0, xk, n, h, t, w, x, solver, method_name,
                   draw_flag):
    solver(k, m, x0, xk, n, h, t, w, x)
    xx = arange(x0, xk, 0.1)
    f_y = []
    for i in x:
        f_y.append(calculate_f_x_1(i, k, m))
    mean_square_error = calculate_mean_square_error(x, f_y, w)
    max_error = calculate_max_norm(x, f_y, w)
    mean_format = "{:.4f}"
    max_format = "{:.4f}"
    if mean_square_error < 0.0001:
        mean_format = "{:.3e}"
    elif mean_square_error > 100:
        mean_format = "{:.0f}"
    if max_error < 0.0001:
        max_format = "{:.3e}"
    elif max_error > 100:
        max_format = "{:.0f}"
    print(";".join((str(n), mean_format.format(mean_square_error),
                    max_format.format(max_error))))
    if draw_flag:
        add_to_plot(xx, calculate_f_x_1(xx, k, m), "Given function")
        add_to_plot(x, w, method_name + " differential")
        file_name = method_name + "_" + str(n)
        title = method_name + "  n = " + str(
            n) + "\n mean square error = " + mean_format.format(
                mean_square_error) + "\n max error = " + max_format.format(
                    max_error)
        draw_plot(title, file_name)
def solve_equation(k, m, x0, xk, n, h, t, w, x, solver, method_name, draw_flag):
    solver(k, m, x0, xk, n, h, t, w, x)
    xx = arange(x0, xk, 0.1)
    f_y = []
    for i in x:
        f_y.append(calculate_f_x_1(i, k, m))
    mean_square_error = calculate_mean_square_error(x, f_y, w)
    max_error = calculate_max_norm(x, f_y, w)
    mean_format = "{:.4f}"
    max_format = "{:.4f}"
    if mean_square_error < 0.0001:
        mean_format = "{:.3e}"
    elif mean_square_error > 100:
        mean_format = "{:.0f}"
    if max_error < 0.0001:
        max_format = "{:.3e}"
    elif max_error > 100:
        max_format = "{:.0f}"
    print(";".join((str(n), mean_format.format(mean_square_error), max_format.format(max_error))))
    if draw_flag:
        add_to_plot(xx, calculate_f_x_1(xx, k, m), "Given function")
        add_to_plot(x, w, method_name + " differential")
        file_name = method_name + "_" + str(n)
        title = (
            method_name
            + "  n = "
            + str(n)
            + "\n mean square error = "
            + mean_format.format(mean_square_error)
            + "\n max error = "
            + max_format.format(max_error)
        )
        draw_plot(title, file_name)
def solve_equation(p, q, r, x_a, x_b, y_a, y_b, n, method_name, draw_flag, m):
    h = (x_b-x_a) / float(n)
    xs = map(lambda j: x_a + h*j, range(1, n))
    # create matrix
    b = map(lambda x: -h*h*r(x), xs)
    b[0] += (1.0 + h/2.0 * p(xs[0])) * y_a
    b[-1] += (1.0 - h/2.0 * p(xs[-1])) * y_b

    a = map(lambda x: -1.0 + h*p(x)/2.0, xs[:-1])
    d = map(lambda x: 2 + h*h*q(x), xs)
    c = map(lambda x: -1.0 - h*p(x)/2.0, xs[1:])

    # solve (thomas)

    for k in range(1, n-1):
        quot = a[k-1] / d[k-1]
        d[k] -= quot * c[k-1]
        b[k] -= quot * b[k-1]

    x0 = map(lambda x: 0,range(n-1))
    x0[n-2] = b[n-2] / d[n-2]

    for k in range(n-3, -1, -1):
        x0[k] = (b[k] - c[k] * x0[k+1]) / d[k]

    xs.insert(0,x_a)
    xs.append(x_b)

    x0.insert(0,y_a)
    x0.append(y_b)

    f_y = []
    xx = arange(x_a, x_b, 0.1)
    for i in xs:
        f_y.append(calculate_f_x_2b(i, k, m))
    mean_square_error = calculate_mean_square_error(xs,f_y, x0)
    max_error = calculate_max_norm(xs, f_y, x0)
    mean_format = "{:.4f}"
    max_format = "{:.4f}"
    if mean_square_error < 0.0001:
        mean_format = "{:.3e}"
    elif mean_square_error > 100:
        mean_format = "{:.0f}"
    if max_error < 0.0001:
        max_format = "{:.3e}"
    elif max_error > 100:
        max_format = "{:.0f}"
    print(";".join((str(n),mean_format.format(mean_square_error), max_format.format(max_error))))
    if draw_flag:
        add_to_plot(xs, calculate_f_x_2b(xs, k, m), "given f")
        add_to_plot(xs, x0, "Finite difference method")
        file_name = method_name + "_"+str(n)
        title = method_name + "  n = "+str(n)+"\n mean square error = " + mean_format.format(mean_square_error) + "\n max error = " + max_format.format(max_error)
        draw_plot(title, file_name)