Mathematical Model of Magnetic Energy Stored in Permanent Magnets