Skip to content

Commit d4a8a59

Browse files
committed
vectorize cost calculation
1 parent 2ddc9ea commit d4a8a59

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

control/optimal.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,9 @@ def _cost_function(self, coeffs):
319319
dt = np.diff(self.timepts)
320320

321321
# Integrate the cost
322-
# TODO: vectorize
323-
cost = 0
324-
for i in range(self.timepts.size-1):
325-
# Approximate the integral using trapezoidal rule
326-
cost += 0.5 * (costs[i] + costs[i+1]) * dt[i]
322+
costs = np.array(costs)
323+
# Approximate the integral using trapezoidal rule
324+
cost = np.sum(0.5 * (costs[:-1] + costs[1:]) * dt)
327325

328326
else:
329327
# Sum the integral cost over the time (second) indices

0 commit comments

Comments
 (0)