Optimal trajectories for quadratic variational processes via invariant imbedding