Skip to content

Boundary Value Problems (BVP)

This module provides numerical methods for solving boundary value problems in differential equations.

Matrix(h, n)

Constructs a tridiagonal matrix for the finite difference method.

Parameters:

Name Type Description Default
h float

Step size.

required
n int

Number of unknowns.

required

Returns:

Type Description
ndarray

np.ndarray: Tridiagonal matrix of size (n, n).

Source code in openum/BVP.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def Matrix(h: float, n: int) -> np.ndarray:
    """
    Constructs a tridiagonal matrix for the finite difference method.

    Args:
        h (float): Step size.
        n (int): Number of unknowns.

    Returns:
        np.ndarray: Tridiagonal matrix of size (n, n).
    """
    diag_main = -(2 + 2 * h**2) * np.ones(n)
    diag_upper = np.ones(n - 1)
    diag_lower = np.ones(n - 1)
    A = np.diag(diag_main) + np.diag(diag_upper, 1) + np.diag(diag_lower, -1)
    return A

Matrix_method(x0, xn, n, y0, yn)

Solves a boundary value problem using the finite difference method.

Parameters:

Name Type Description Default
x0 float

Left boundary of the domain.

required
xn float

Right boundary of the domain.

required
n int

Number of interior grid points.

required
y0 float

Boundary condition at x0.

required
yn float

Boundary condition at xn.

required

Returns:

Type Description
ndarray

np.ndarray: Solution values at the grid points.

Source code in openum/BVP.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def Matrix_method(x0: float, xn: float, n: int, y0: float, yn: float) -> np.ndarray:
    """
    Solves a boundary value problem using the finite difference method.

    Args:
        x0 (float): Left boundary of the domain.
        xn (float): Right boundary of the domain.
        n (int): Number of interior grid points.
        y0 (float): Boundary condition at x0.
        yn (float): Boundary condition at xn.

    Returns:
        np.ndarray: Solution values at the grid points.
    """
    h = (xn - x0) / (n + 1)
    list_x = np.linspace(x0, xn, n + 2)
    list_y = np.zeros(n + 2)
    list_y[0] = y0
    list_y[-1] = yn

    A = Matrix(h, n)
    b = Vector(n, y0, yn)
    y = np.linalg.solve(A, b)

    for i in range(1, n + 1):
        list_y[i] = y[i - 1]

    # Plot solution
    plt.figure(figsize=(8, 6))
    plt.plot(list_x, list_y, color='b', label='Numerical Solution')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid(True)
    plt.legend()
    plt.show()

    return y

Vector(n, y0, yn)

Constructs the right-hand side vector for the finite difference method.

Parameters:

Name Type Description Default
n int

Number of unknowns.

required
y0 float

Boundary condition at x0.

required
yn float

Boundary condition at xn.

required

Returns:

Type Description
ndarray

np.ndarray: Right-hand side vector.

Source code in openum/BVP.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def Vector(n: int, y0: float, yn: float) -> np.ndarray:
    """
    Constructs the right-hand side vector for the finite difference method.

    Args:
        n (int): Number of unknowns.
        y0 (float): Boundary condition at x0.
        yn (float): Boundary condition at xn.

    Returns:
        np.ndarray: Right-hand side vector.
    """
    b = np.zeros(n)
    b[0] -= y0
    b[-1] -= yn
    return b

solve_by_shooting(ode, x_1, x_2, n, v_0, u_1, u_2, max_iter)

Solves a boundary value problem using the shooting method.

Parameters:

Name Type Description Default
ode callable

Function defining the ODE system.

required
x_1 float

Left boundary of the domain.

required
x_2 float

Right boundary of the domain.

required
n int

Number of interior grid points.

required
v_0 list

Initial guesses for the derivative at x_1.

required
u_1 float

Boundary condition at x_1.

required
u_2 float

Boundary condition at x_2.

required
max_iter int

Maximum number of iterations for root finding.

required

Returns:

Name Type Description
tuple tuple[float, ndarray, ndarray]

(Corrected initial derivative, solution grid points, solution values)

Source code in openum/BVP.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def solve_by_shooting(ode: Callable, x_1: float, x_2: float, n: int, v_0: list, u_1: float, u_2: float, max_iter: int) -> tuple[float, np.ndarray, np.ndarray]:
    """
    Solves a boundary value problem using the shooting method.

    Args:
        ode (callable): Function defining the ODE system.
        x_1 (float): Left boundary of the domain.
        x_2 (float): Right boundary of the domain.
        n (int): Number of interior grid points.
        v_0 (list): Initial guesses for the derivative at x_1.
        u_1 (float): Boundary condition at x_1.
        u_2 (float): Boundary condition at x_2.
        max_iter (int): Maximum number of iterations for root finding.

    Returns:
        tuple: (Corrected initial derivative, solution grid points, solution values)
    """
    def difference(v):
        y_0 = [u_1, v]
        _, y_values = runge_kutta(x_1, x_2, y_0, n, ode, False)
        return y_values[-1, 0] - u_2

    # Debug: Check function values at initial guesses
    print(f"difference({v_0[0]}) = {difference(v_0[0])}")
    print(f"difference({v_0[1]}) = {difference(v_0[1])}")

    v_corr = linear_interpolation(difference, v_0[0], v_0[1], max_iter)

    if v_corr is None:
        raise RuntimeError("Root finding failed.")

    y_0 = [u_1, v_corr]
    x, y = runge_kutta(x_1, x_2, y_0, n, ode, False)
    return v_corr, x, y