Skip to content

Commit 4a5c496

Browse files
2 parents f65afeb + 92e3835 commit 4a5c496

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

maths/laplace_transformation.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,32 @@ def laplace_transform(
1212
function_values: np.ndarray, s_value: float, delta_t: float
1313
) -> float:
1414
"""
15-
Calculate the numerical Laplace Transform of a function given its values.
15+
Calculate the numerical Laplace Transform of a function given its values over time.
16+
17+
This implementation supports only real-valued, non-negative Laplace
18+
parameters ``s``.
1619
1720
Args:
1821
function_values: A numpy array of the function values f(t).
19-
s_value: The real-valued Laplace parameter 's'.
20-
delta_t: The positive time step between samples.
22+
s_value: The real-valued Laplace parameter ``s``. Must be non-negative.
23+
delta_t: The time step between samples.
2124
2225
Returns:
23-
The approximate real-valued Laplace transform at s_value.
26+
The approximate real-valued value of the Laplace transform at s_value.
27+
28+
Example: For f(t) = 1, the Laplace transform L{1} = 1/s.
29+
If s = 2, L{1} should be 0.5.
2430
25-
>>> t = np.linspace(0, 50, 10000, endpoint=False)
26-
>>> f_t = np.ones_like(t)
31+
>>> t = np.linspace(0, 50, 10000)
32+
>>> f_t = np.ones_like(t) # f(t) = 1
2733
>>> res = laplace_transform(f_t, s_value=2.0, delta_t=50/10000)
2834
>>> abs(res - 0.5) < 1e-3
2935
True
3036
31-
>>> t = np.linspace(0, 50, 10000, endpoint=False)
37+
Example: For f(t) = e^(-t), the Laplace transform L{e^-t} = 1/(s+1).
38+
If s = 1, L{e^-t} should be 0.5.
39+
40+
>>> t = np.linspace(0, 50, 10000)
3241
>>> f_t = np.exp(-t)
3342
>>> res = laplace_transform(f_t, s_value=1.0, delta_t=50/10000)
3443
>>> abs(res - 0.5) < 1e-3
@@ -45,8 +54,10 @@ def laplace_transform(
4554
# The integrand: f(t) * e^(-s*t)
4655
integrand = function_values * np.exp(-s_value * time_vector)
4756

48-
# Numerical integration using the trapezoid rule
49-
return float(np.trapezoid(integrand, dx=delta_t))
57+
# Numerical integration using the trapezoidal rule
58+
result = np.trapezoid(integrand, dx=delta_t)
59+
60+
return float(result)
5061

5162

5263
if __name__ == "__main__":

0 commit comments

Comments
 (0)