11"""
2- The Laplace Transform is defined as: L{f(t)} = integral from 0 to infinity of e^(-st) * f(t) dt .
2+ This module provides a numerical implementation of the Laplace Transform .
33
4- Wiki: https://en.wikipedia.org/wiki/Laplace_transform
4+ https://en.wikipedia.org/wiki/Laplace_transform
55
66"""
77
@@ -12,52 +12,44 @@ 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 over time .
15+ Calculate the numerical Laplace Transform of a function given its values.
1616
1717 Args:
1818 function_values: A numpy array of the function values f(t).
19- s_value: The complex frequency parameter 's' (modeled here as a float) .
20- delta_t: The time step between samples.
19+ s_value: The real-valued Laplace parameter 's'.
20+ delta_t: The positive time step between samples.
2121
2222 Returns:
23- The approximate value of the Laplace transform at s_value.
23+ The approximate real-valued Laplace transform at s_value.
2424
25- Example: For f(t) = 1, the Laplace transform L{1} = 1/s.
26- If s = 2, L{1} should be 0.5.
27-
28- >>> t = np.linspace(0, 50, 10000)
29- >>> f_t = np.ones_like(t) # f(t) = 1
25+ >>> t = np.linspace(0, 50, 10000, endpoint=False)
26+ >>> f_t = np.ones_like(t)
3027 >>> res = laplace_transform(f_t, s_value=2.0, delta_t=50/10000)
3128 >>> abs(res - 0.5) < 1e-3
3229 True
33-
34- Example: For f(t) = e^(-t), the Laplace transform L{e^-t} = 1/(s+1).
35- If s = 1, L{e^-t} should be 0.5.
3630
37- >>> t = np.linspace(0, 50, 10000)
31+ >>> t = np.linspace(0, 50, 10000, endpoint=False )
3832 >>> f_t = np.exp(-t)
3933 >>> res = laplace_transform(f_t, s_value=1.0, delta_t=50/10000)
4034 >>> abs(res - 0.5) < 1e-3
4135 True
4236 """
43- if s_value < 0 :
44- raise ValueError ("s_value must be non-negative for convergence." )
37+ if delta_t <= 0 :
38+ raise ValueError ("delta_t must be a positive value." )
39+ if function_values .size == 0 :
40+ raise ValueError ("function_values array cannot be empty." )
4541
4642 # Time vector corresponding to the function values
4743 time_vector = np .arange (len (function_values )) * delta_t
48-
44+
4945 # The integrand: f(t) * e^(-s*t)
5046 integrand = function_values * np .exp (- s_value * time_vector )
51-
52- # Numerical integration using the trapezoidal rule
53- result = np .trapezoid (integrand , dx = delta_t )
54-
55- return float (result )
47+
48+ # Numerical integration using the trapezoid rule
49+ return float (np .trapezoid (integrand , dx = delta_t ))
5650
5751
5852if __name__ == "__main__" :
5953 import doctest
6054
61-
6255 doctest .testmod ()
63-
0 commit comments