PHYS 2063 Wave Physics:
Mathematica Introduction

Cells, expressions, replacements, lists, solving, calculus, and plotting

Simple Things

It is handy to always start something new with a clean slate

Remove["Global`*"]

A cell is a collection of commands. To execute a cell, use “Shift- Carriage Return”

1+2
(x+y)^2

Built in functions will do most anything you want

Exp[2]
Expand[(x+y)^2]
Sin[Pi/6]

A semi-colon at the end of a command suppresses output. A “%” means “last result calculated”

(x+y)^2;
Expand[%]

It is handy to work by defining expressions

u=(x+y)^2;
Expand[u^2]

Assignments and Replacements

You can assign something to a variable using “=”

x=a;
y=b;
u

However, that changes the variable forever! Start over and redefine “u”

Remove["Global`*"]
u=(x+y)^2;

Now do the same thing using “Replace” (as shorthand). The assignment “subs” is called a “List”. We’ll get to that in a minute.

subs={x->a,y->b};
w=u/.subs
u

Lists

Lists are very versatile constructs in Mathematica. They get used a lot.

aList={a,b,f,3,u};
3 aList

It’s not hard to extract parts of list. The shorthand for Part is common.

Part[aList,3]
aList[[3]]

Lists can be nested

{aList,3,q,t,{i,j}}

Anything can be in a list

{a,b,"Some text",4==2^2}

Vectors and matrices are represented as lists

v1={x1,y1,z1};
v2={x2,y2,z2};
Dot[v1,v2]
Cross[v1,v2]
Outer[Times,v1,v2]
MatrixForm[%]

Solving algebraic equations is easy

Note that “equations” are logical constructs.

Solve[2x+3==7,x]

Why is the solution a list inside a list? Keep watching...

Solve[x^2==1,x]
Solve[x^2==-1,x]

Solve[{x+2y==4,x-2y==0},{x,y}]

Solve[{x^2+y^2==4,x+y==0},{x,y}]

Calculus is also possible, of course

Derivatives, indefinite integrals, and definite integrals are straightforward

Remove["Global`*"]

y=a x^2+b Sin[c x];

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

Integrate[y,x]

Integrate[y,{x,0,Pi/(2 c)}]

Integrate[1/x,{x,1,E}]

There are many ways to plot things

y=x^2-x^3/3;
Plot[y,{x,0,3.5}]

x=r Cos[Pi t];
y=r Sin[Pi t];
ParametricPlot[{x,y}/.r->2,{t,0,1.5}]

A physics example: Free fall trajectories

Remove["Global`*"]

Write down the x and y positions as a function of time (starting from the origin, with velocity v0 at angle θ)

x=v0 Cos[θ] t;
y=v0 Sin[θ]t-g t^2/2;

Find the “range”

tHit=t/.Solve[y==0,t][[2]];
range=x/.t->tHit

Define some  values

vals={v0->30,θ->Pi/6};

Now use the data bases to find the value of “g”

G=QuantityMagnitude[UnitConvert[Quantity["GravitationalConstant"]]];
M=QuantityMagnitude[UnitConvert[Quantity["EarthMass"]]];
R=QuantityMagnitude[UnitConvert[Quantity["EarthMeanRadius"]]];
g=a/.Solve[m a==G m M/R^2,a][[1]]

Plot the trajectory

ParametricPlot[{x,y}/.vals,{t,0,tHit/.vals}]