Mathematica Syntax Items for E&M  [lam21]

Clear["Global`*"]

Vectors are lists by default

x={1,2,3};

y={2,3,4};

y-x

2x

Dot product

Dot[x,y]

x.y

Cross product

Cross[x,y]

x\[Cross]y

Magnitude

Norm[x]

Sqrt[x.x]

Numerical solution of algebraic equation

Clear["Global`*"]

sol=NSolve[x^5-2x+3==0,x]

Pick relevant solution

xrel=x/.sol[[2]]

More than one equation

Solve[{5x-2y+3z==7,2x+8y-4z==3,x+y-z==1},{x,y,z}]

NSolve[{5x-2y+3z==7,2x+8y-4z==3,x+y-z==1},{x,y,z}]

Define a function

f[x_]=(x+a)*Exp[-(x^2+a^2)]

g[x_,y_]=1/(x^2+y^3)

Derivative

f'[x]

D[f[x],x]

Second derivative

f''[x]

D[f[x],{x,2}]

Partial derivative

D[g[x,y],y]

D[g[x,y],{x,2},{y,1}]

Indefinite integral

Integrate[x^2*Exp[-x],x]

Definite integral

Integrate[x^2*Exp[-x],{x,0,Infinity}]

Series expansion

Series[f[x],{x,0,2}]

Series[g[x,y],{y,Infinity,4}]

Clear["Global`*"]

Gradient of scalar function

f[x_,y_,z_]=x^3y^4z^5

g[x_,y_,z_]=Grad[f[x,y,z],{x,y,z}]

Curl of vector function

Curl[g[x,y,z],{x,y,z}] (*curl of gradient vanishes*)

h[x_,y_,z_]={3 x (y^2) (z^3),4 (x^2) y (z^4),5 (x^3) (y^2) z}

i[x_,y_,z_]=Curl[h[x,y,z],{x,y,z}]

Divergence of a vector function

Div[g[x,y,z],{x,y,z}]

Div[i[x,y,z],{x,y,z}] (*divergence of curl vanishes*)

Laplacian of a scalar function

Laplacian[f[x,y,z],{x,y,z}] (*divergence of gradient*)

Contour plot, density plot, line plot

ContourPlot[x^2+ y,{x,-2,2},{y,-2,2},ContourStyle->Black,Contours->15,ContourShading->Automatic,FrameLabel->{" x"," y"},LabelStyle->15,ContourLabels->Automatic,Axes->False,Frame->True,AspectRatio->1.0,FrameTicks->{{Automatic,None},{Automatic,None}}]

ContourPlot[ssigma[x,y,1],{x,-2,2},{y,-2,2},ContourStyle->Black,Contours->8,ContourShading->Automatic,FrameLabel->{" x"," y"},LabelStyle->15,ContourLabels->Automatic,Axes->False,Frame->True,AspectRatio->1,FrameTicks->{{Automatic,None},{Automatic,None}},PlotRange->{{-2,2},{-2,2}},Epilog->{Inset[Text[Style[Framed["σ(x,y)"],Background->White,FontSize->15]],{1.4,1.6}]}]

DensityPlot[ssigma[x,y,1],{x,-2,2},{y,-2,2},FrameLabel->{" x"," y"},LabelStyle->15,Axes->False,Frame->True,AspectRatio->1,FrameTicks->{{Automatic,None},{Automatic,None}},PlotRange->{{-2,2},{-2,2}},Epilog->{Inset[Text[Style[Framed["σ(x,y)"],Background->White,FontSize->15]],{1.4,1.6}]}]

Plot[ssigma[x,0,1],{x,0,4},Frame->True,AspectRatio->0.7,FrameLabel->{Style["r",Italic],Style["σ(r)"]},LabelStyle->13,Axes->True,FrameTicks->{{{-0.2,-0.15,-0.1,-0.05,0},None},{{0,1,2,3},None}},PlotStyle->{Thickness[0.007]},PlotRange->{{0,3},{-0.2,0.03}},Epilog->{Inset[Text[Style["induced radial",FontSize->13]],{2,-0.1}],Inset[Text[Style["charge density profile",FontSize->13]],{2,-0.13}]}]

Assumptions: tell Mathematica what you assume to be the case (random example)

$Assumptions=λ>0&&k>0&&l>0&&x∈Reals&&y∈Reals&&z∈Reals;

Analytic solution of simple sets of equations

Clear["Global`*"]

Solve[{x1+x2+x3==0,x1-x2==a,x3-x2==b},{x1,x2,x3}]

Performing an infinite sum

Sum[1/n^2,{n,1,Infinity}]

Mathematical identity

$Assumptions=x∈Reals&&y∈Reals&&z∈Reals&&xp∈Reals&&yp∈Reals&&zp∈Reals;

xv={x,y,z}

xpv={xp,yp,zp}

Grad[1/Norm[xv-xpv],{x,y,z}]//FullSimplify

Grad[1/Norm[xv-xpv],{x,y,z}]==-(xv-xpv)/Norm[xv-xpv]^3//FullSimplify

Laplacian[1/Norm[xv-xpv],{x,y,z}]//FullSimplify

Div[-(xv-xpv)/Norm[xv-xpv]^3,{x,y,z}]//FullSimplify