Physics with Mathematica

Ninth Class

The Mercury Transit event is coming!

Mathematica as a Numerical Calculator

We’ll do an example: Calculate the value of “e”

Remove["Global`*"]

nList=Table[i,{i,0,8}]

x=10^nList

Note: The n=8 calculation in the following takes about half a minute on my computer

eVals=N[(1+1/x)^x,10]

eN=N[E,10]

eDif=10^6(eN-eVals)/eN

Grid[Transpose[{nList,x,eVals,eDif}]]

Solving Equations Numerically

Solving algebraic equations or finding zeros

Remove["Global`*"]

You can use NSolve for finding numerical solutions of algebraic equations

ab={a->0.5,b->0.25};
sol=Solve[{x^2+y^2==1,a x+b y==0},{x,y}];
{x,y}/.sol/.ab

Solve[{x^2+y^2==1,a x+b y==0}/.ab,{x,y}]

NSolve[{x^2+y^2==1,a x+b y==0}/.ab,{x,y}]

NSolve has trouble, though, if the equations aren’t polynomials

NSolve[{x^2+y^2==1,y==Tan[x]},{x,y}]

For such problems, it is better to use FindRoot

FindRoot[{x^2+y^2-1,y-Tan[x]},{x,0.5},{y,0.5}]

Make a plot to check the values

Plot[{Sqrt[1-x^2],Tan[x]},{x,0,1}]

Note that we could have made the plot first to pick good starting values for FindRoot

Databases and Units

If you want a value, Mathematica will find it for you

Remove["Global`*"]

Quantity["SpeedOfLight"]

UnitConvert[Quantity["SpeedOfLight"]]

QuantityMagnitude[UnitConvert[Quantity["SpeedOfLight"]]]

QuantityMagnitude[UnitConvert[Quantity["ProtonMass"]]]

QuantityMagnitude[UnitConvert[Quantity["HubbleConstant"]]]

PlanetData["Properties"]

PlanetData["Earth","EquatorialDiameter"]

QuantityMagnitude[UnitConvert[PlanetData["Earth","EquatorialDiameter"]]]