High frequency computation in wave equations and optimal design for a cavity