Multipole expansion of 2-dim (transverse) magnetic fields

1.) cylindrical coordinates

Coordinates :     z=r (e^(i ϕ))

We expand the complex vector potential into an infinite series as

acomp = Sum[(Subscript[κ, n]) (z^n),{n,0,∞}]

The complex coefficients κ=((Subscript[λ, n])+i (Subscript[μ, n])) are normalized such that we finally get dimensionless expansion coefficients (Subscript[a, n]) and (Subscript[b, n]), 
main field (Subscript[B, 0]) and the reference radius (Subscript[r, 0]). 
Both are in principle arbitrary but typically the problem offers a preferential choice.

rule1 ={(Subscript[λ, n])->-(Subscript[B, 0])/(n  ((Subscript[r, 0])^((n-1))))*(Subscript[b, n]),(Subscript[μ, n])->(Subscript[B, 0])/(n  ((Subscript[r, 0])^((n-1))))*(Subscript[a, n])}

summand = ((Subscript[λ, n])+I (Subscript[μ, n]))(((rr*Exp[I ϕ]))^n) /.rule1

vvc[rr_,ϕ_] =FullSimplify[Im[ComplexExpand[summand]],{(Subscript[r, 0])>0,n>0,rr>0,0<ϕ<π,Element[{ϕ,(Subscript[B, 0]),(Subscript[r, 0]),(Subscript[a, n]),(Subscript[b, n])},Reals]}]

With this definition, the scalar potential V(r,ϕ) = Im(OverTilde[A]) becomes

TraditionalForm[HoldForm[V[r,ϕ]==Sum[-(((r^n) (Cos[n ϕ] (Subscript[a, n])-Sin[n ϕ] (Subscript[b, n])) (Subscript[B, 0]) (Subscript[r, 0]^(1-n)))/n),{n,1,∞}]]]

Notice that we have omitted the n=0 term since this would be a constant potential (independent of r and ϕ) and thus not contributing to the B - field given by the gradient of V.

Each of the summands represents the 2-pole contribution to the magnetig field, its
 two components are given by

bϕ[rr_,ϕ_] = -1/rr*D[vvc[rr,ϕ],ϕ]  //FullSimplify

TraditionalForm[(Subscript[B, ϕ,n])==bϕ[r,ϕ]]

br[rr_,ϕ_] = -D[vvc[rr,ϕ],rr]  //Simplify

TraditionalForm[(Subscript[B, r])==br[r,ϕ]]

The absolute strength of the n-component is given by

babs[r_,ϕ_] = FullSimplify[(Sqrt[((bϕ[r,ϕ])^2) +((br[r,ϕ])^2) ]),{(Subscript[B, 0])>0,(Subscript[r, 0])>0,r>0,n>1}]

TraditionalForm[(Subscript[B, n])==babs[r,ϕ]]

the representation in cylindrical coordinates tells us the follwing two facts : 
 - the components of the multipole order n have a 2π/n fold symmetry in ϕ
 - the absolute strength of the order n is independent on ϕ and increases with (r^(n-1)).

The components are named n=1 : dipol, n=2 : quadrupole, n=3 : sextupole...., in general  2n - pole.

2.) cartesian coordinates

We follow exactly the same line but with .

z=x+i y

The field components (Subscript[B, n]) cannot be given in closed form. Therefore we show n= 1... 3 as an example.

nmax = 3

ss = Sum[((Subscript[λ, n])+I (Subscript[μ, n]))(((x+I y))^n) /. {(Subscript[λ, m_])->-(Subscript[B, 0])/(n  ((Subscript[r, 0])^((n-1))))*(Subscript[b, m]),(Subscript[μ, m_])->+(Subscript[B, 0])/(n  ((Subscript[r, 0])^((n-1))))*(Subscript[a, m])},{n,1,nmax}] //Simplify

vv[x_,y_] = Simplify[Im[ComplexExpand[ss]],{(Subscript[r, 0])>0,Element[{(Subscript[a, n_]),(Subscript[b, n_]),x,y,(Subscript[B, 0])},Reals]}]

bx[x_,y_] = -D[vv[x,y],x]

by[x_,y_] = -D[vv[x,y],y]

byseries  = Evaluate[Collect[by[x,y] , (Subscript[r, 0]),Simplify]]

bxseries  = Collect[bx[x,y],(Subscript[r, 0]),Simplify]

TraditionalForm[(Subscript[B, y])==byseries]

TraditionalForm[(Subscript[B, x])==bxseries]

constant (n=1 dipole)
linear in x and y (n=2 Quadrupole)
quadratic in x and y (n=3 Sextupole)

“normal” and “skew” components

Each multipole order n has two coefficients : (Subscript[a, n]) and (Subscript[b, n]). 
The (Subscript[a, n]) are called “skew components” 
the (Subscript[b, n]) are called “normal” or “upright” components.

(Coefficient[{bx[x,y],by[x,y],0},(Subscript[r, 0]),-1] \[Cross]{0,0,1})[[{1,2}]]/. (Subscript[a, n_])->0

normalcomponents = TableForm[FullSimplify@Reverse@Table[Coefficient[{bx[x,y],by[x,y]}/(Subscript[B, 0]),(Subscript[r, 0]),n] /. (Subscript[a, n_])->0,{n,-nmax+1,0}],TableHeadings->{{"dipole","quadrupole","sextupole","octupole"},{"(Subscript[B, x])/(Subscript[B, 0])","(Subscript[B, y])/(Subscript[B, 0])"}}]//TraditionalForm

skewcomponents = TableForm[FullSimplify@Reverse@Table[Coefficient[{bx[x,y],by[x,y]}/(Subscript[B, 0]),(Subscript[r, 0]),n] /. (Subscript[b, n_])->0,{n,-nmax+1,0}],TableHeadings->{{"dipole","quadrupole","sextupole","octupole"},{"(Subscript[B, x])/(Subscript[B, 0])","(Subscript[B, y])/(Subscript[B, 0])"}}]//TraditionalForm

fieldlines and lines of force

Manipulate[GraphicsRow[{StreamPlot[{-by[x,y],bx[x,y]}/. {(Subscript[B, 0])->1,(Subscript[r, 0])->5,(Subscript[a, 1])->a1,(Subscript[a, 2])->a2,(Subscript[a, 3])->a3,(Subscript[b, 1])->b1,(Subscript[b, 2])->b2,(Subscript[b, 3])->b3,Xx->x,Yy->y},{x,-3,3},{y,-3,3},StreamColorFunction->"Rainbow", PlotLabel->"lines of force",ImageSize->300,FrameLabel->{"horizontal, x","vertical y"},PlotTheme->"Presentation"],StreamPlot[{bx[x,y],by[x,y]}/. {(Subscript[B, 0])->1,(Subscript[r, 0])->5,(Subscript[a, 1])->a1,(Subscript[a, 2])->a2,(Subscript[a, 3])->a3,(Subscript[b, 1])->b1,(Subscript[b, 2])->b2,(Subscript[b, 3])->b3,Xx->x,Yy->y},{x,-3,3},{y,-3,3},StreamColorFunction->"Rainbow", PlotLabel->"B-field", ImageSize->300, FrameLabel->{"horizontal, x","vertical y"},PlotTheme->"Presentation"]}],Grid[{{Control[{{a1,0,"(Subscript[a, 1]):horizontal dipol"},{-.2,0,.2}}],Control[{{a2,0,"(Subscript[a, 2]):skew quadrupol"},{-.2,0,.2}}],Control[{{a3,0,"(Subscript[a, 3]):skew sextupol"},{-.2,0,.2}}]},{Control[{{b1,0,"(Subscript[b, 1]):vertical dipol"},{-.2,0,.2}}],Control[{{b2,0,"(Subscript[b, 2]):normal quadrupol"},{-.2,0,.2}}],Control[{{b3,0,"(Subscript[b, 3]):normal sextupol"},{-.2,0,.2}}]}}],SaveDefinitions->True]