Based on the variational principle, a new method for appoximately solving partial differential equation with an arbitrary boundary shape is proposed. Accordingly, the problem of solving a partial differential equation is reduced to one of solving a series of ordinary differential equations. This method proves to be more powerful when the boundary shape gets complicated. An application is made to the problem of the tilting mode instability for spheromak without restriction in ellipticity. The following physical conclusion is arrived at, namely, the oblate spheromak is better than elongated one as far as suppressing the tilting mode instability is concerned.