Optimal shape and motion of undulatory swimming organisms