A method of determining an optimal coil shape for use in magnetic stimulation includes identifying a stimulation location and a constraint at the stimulation location. A first electromagnetic effect at the stimulation location is determined. The first electromagnetic effect is induced by a first electrical quantity assigned to a first current element at a first current element location with a first orientation. A second electromagnetic effect at the stimulation location is also determined. The second electromagnetic effect is induced by a second electrical quantity assigned to a second current element at the first current element location with a second orientation. Based on the first electromagnetic effect and the second electromagnetic effect, an optimal orientation of a current element at the first current element location is determined. The optimal orientation is such that the constraint is satisfied. Repeating the process at a plurality of locations yields the optimal coil shape.