Vector Spaces in Physics
8/6/2015
Appendix C. Mathematica
See Appendix B for instructions on accessing Mathematica on the Department computer
system. Here we will just present a routine for numerical addition of vectors. The
(magnitude, angle) representation for vectors is assumed.
1. Calculation of the vector sum using Mathematica. Today we rely increasingly on
"machines" to carry out laborious calculations like the preceding one for us.
Mathematica is one such machine, and we will use it in this course.
As a start, we will use Mathematica to carry out
the solution to the numerical example given in
Chapter 1, section B, illustrated in figures (1-4)
and (1-5). Here is the method. If we can
determine angle 1 of the triangle 1-2-3, we can
calculate the angle C (C = A - 1) and the
magnitude C of the sum vector (C2 = A2 + B2 – 2
A B cos 2, where 2 is determined from 2 + A
- B = 180). We will show how to do the
calculation using Mathematica. [The following
Mathematica commands are taken from the file
~bland/export/385/vectsum2.nb, also available on
the Ph 385 download page. Rather than typing
everything in, feel free to copy this file to your
directory and open it with Mathematica.] The first Figure C-1.
command defines a Mathematica function which
calculates the vector sum of the two vectors A and B ; the second shows a sample call to
this function; and the following line shows the answer by Mathematica. To run this
function yourself, just type in the commands exactly as shown, and press [Shift][Enter].
Vsum[{Amag_, Athdeg_, Bmag_, Bthdeg_}] := (
Ath = Athdeg*Pi/180. ;
Bth = Bthdeg*Pi/180. ;
Th2 = Pi - Ath + Bth;
Cmag = Sqrt[Amag^2 + Bmag^2 - 2 Amag Bmag Cos[Th2]];
CosTh1 = (Amag^2 + Cmag^2 - Bmag^2)/(2 Amag Cmag);
SinTh1 = Bmag*Sin[Th2]/Cmag;
Th1 = ArcTan[CosTh1, SinTh1];
Cth = Ath - Th1;
{Cmag, Cth*180. /Pi}
)
<shift><enter>
Vsum[{10.,48.,14.,20.}]
<shift><enter>
Out[2] = {23.3072,31.6205}
We will use this function for a variety of numerical calculations with vectors in the
(magnitude, angle) form.
C-1
Vector Spaces in Physics
8/6/2015
2. Matrix operations in Mathematica.
m={{2,4,3},{1,3,2},{1,3,1}}
MatrixForm[m]
cm
a.b
Inverse[m]
MatrixPower[m,n]
Det[m]
Tr[m]
Transpose[m]
Eigenvalues[m]
Engenvectors[m]
Eigenvalues[N[m]],Eigenvectors[N[m]]
m=Table[Random[],{3},{3}]
<<Graphics`
Solve[....,x]
Plot3D[function(x,y),{x,...},{y ...}]
Table[...,{500},{500}]
ListPlot[Abs[Eigenvalues[m]]]
N[Pi,100]
Simplify[%]
//N
defining a matrix
display as a rectangular array
multiply by a scalar
matrix product
matrix inverse
nth power of a matrix
determinant
trace
transpose
eigenvalues
eigenvectors
numerical eigenvalues and eigenvectors
3x3 matrix of random numbers
load graphics package
solve simultaneous linear equations
3-D plot
create stuff to plot
matrix eigenvalues
pi to a hundred places
simplify the preceding answer
give numerical result
Table C-1. Some useful mathematical operations which Mathematica can carry out.
3. Speed test for Mathematica. Here's a function to use to see how long Mathematica
takes to calculate the determinant of a matrix.
n=3
m=Table[Random[],{n},{n}];
Det[m]
Note: the semicolon at the end of the second line prevents printing out the matrix m. For
small values of m you could remove the semicolon, but for large values of m the printout
will take forever.
C-2
© Copyright 2026 Paperzz