我正在尝试-无济于事-在以下代码中广播以下代码Python:
import numpy as np
from scipy.linalg import expm
def ExactSol(c, omega0, x0, v0, T, p):
# Docstring
"""
Returns the solution of
x'' + c x' + w0^2 x = 0
embodied with initial condition x(0) = x0 and x'(0) = v0
i.e. the function X(t) = exp(tA) * X0
with X = (x, v), X0 = (x0, v0), A = (0 1)
(-w0^2 -c)
over [0,T] cut in p sub-intervals
Positional arguments:
c -- damping parameter
omega0 -- pulsation
x0 -- initial position
v0 -- initial speed
T -- final time
p -- the number of samples in [0,T]
Keyword arguments:
"""
# Body
time = np.linspace(0, T, p + 1)
time = time[:, np.newaxis, np.newaxis]
X0 = np.array([x0, v0])
A = np.array([[0, 1], [- omega0**2, - c]])
B = time * A
E = np.zeros(B.shape)
for i in range(0, B.shape[0]):
E[i,:,:] = expm(B[i,:,:])
return time[:,0,0], E@X0
我想避免使用for循环。