A spectral Galerkin method for the fractional order diffusion and wave equation