Skip to content

Numerical Differentiation

This module provides approximation methods for calculating the derivatives of functions numerically.

Central_diff_first_deri(x, h, func)

Function to calculate the first derivative via Central differences.

Parameters:

Name Type Description Default
x float

Point were the derivative needs to be calculated

required
h float

Stepsize

required
func function

Function that needed to be evaluated

required
Return

value (float): Value of the derivative

Source code in openum/numdiff.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def Central_diff_first_deri(x:float, h:float, func: Callable) -> float:
    """
    Function to calculate the first derivative via Central differences.

    Args:
        x (float): Point were the derivative needs to be calculated
        h (float): Stepsize
        func (function): Function that needed to be evaluated

    Return:
        value (float): Value of the derivative

    """
    # Get the value of the function
    y1 = func(x + h)
    y_1 = func(x - h)
    y_2 = func(x - 2*h)
    y2 = func(x + 2*h)

    # Apply the formula for central differnces
    value = (y_2 - y2 + 8*(y1-y_1)) / (12 * h)
    return value

Richardson(x, h, func)

Function to calculate the derivative via Richardson extrapolation.

Parameters:

Name Type Description Default
x float

Point were the derivative needs to be calculated

required
h float

Stepsize

required
func function

Function that needed to be evaluated

required
Return

value (float): Value of the derivative

Source code in openum/numdiff.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def Richardson(x:float, h:float, func: Callable) -> float:
    """
    Function to calculate the derivative via Richardson extrapolation.

    Args:
        x (float): Point were the derivative needs to be calculated
        h (float): Stepsize
        func (function): Function that needed to be evaluated

    Return:
        value (float): Value of the derivative

    """

    value = psi(h, x, func)
    return value

spline_derivative(i, x_val, x, func)

Computes the first derivative of a function using cubic spline interpolation.

Parameters:

Name Type Description Default
i int

Index of the left boundary of the interval [x[i], x[i+1]].

required
x_val float

The point at which to evaluate the derivative.

required
x ndarray

Array of grid points.

required
func Callable

The function to be interpolated.

required

Returns:

Name Type Description
float Optional[float]

The derivative of the spline at x_val, or None if evaluation fails.

Source code in openum/numdiff.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def spline_derivative(i: int, x_val: float, x: np.ndarray, func: Callable) -> Optional[float]:
    """
    Computes the first derivative of a function using cubic spline interpolation.

    Args:
        i (int): Index of the left boundary of the interval [x[i], x[i+1]].
        x_val (float): The point at which to evaluate the derivative.
        x (np.ndarray): Array of grid points.
        func (Callable): The function to be interpolated.

    Returns:
        float: The derivative of the spline at x_val, or None if evaluation fails.
    """
    n = len(x) - 1
    _, _, sol = cubic_splines(n, x, func)

    # KORREKTUR: Wir rechnen nur weiter, wenn wir EINE Lösung (nicht None) haben!
    if sol is not None:
        values = func(x)

        if x[i] <= x_val <= x[i + 1]:
            h_i = h(i, x)  # Schrittweite zwischen x[i] und x[i + 1]

            # Berechnung der Koeffizienten a, b, c für das Intervall [x[i], x[i+1]]
            a = (sol[i + 1] - sol[i]) / (6 * h_i)
            b = sol[i] / 2
            c = (values[i + 1] - values[i]) / h_i - (h_i / 6) * (2 * sol[i] + sol[i + 1])

            # Berechnung der ersten Ableitung des Splines an der Stelle x_val
            derivative = 3 * a * (x_val - x[i]) ** 2 + 2 * b * (x_val - x[i]) + c
            return float(derivative)
        else:
            print(f"x_val = {x_val} liegt außerhalb des Intervalls [{x[i]}, {x[i + 1]}]")
            return None

    else:
        print("No solution found")
        return None