Optimal control of spin systems with applications in Magnetic Resonance