Math 291: Intro to Mathematica (for Linear Algebra)

with dusty wilson, mathematics instructor, highline community college
week 1

Contents

- Basic syntax and evaluations
- Built in functions
- Creating your own functions
- Linear Algebra

Basic Syntax and evaluation

- To evaluate an input line, use shift-enter

2+3 (*basic numeric operations*)

(Sqrt[-25]) (*works using complex numbers by default*)

- All functions are spelled out and begin with a capital letter.  Functions use brackets "[ ]" rather than parenthesis "( )" because the program uses parenthesis for mathematical operations and brackets for functions arguments.  Braces "{}" are used to notate lists and arrays.

Sin[(π/3)] (* exact mathematics*)

Cos[1] (*sometimes exact work isn't helpful*)

Tan[1]//N (*use "//N" to get numerical results*)

- Key board shortcuts are either automatic ("->" becomes ->) or are created using the escape key ("esc ee esc" becomes e and "esc p esc" becomes π.  This also works for symbols where "esc int esc" becomes ∫ and "esc pd esc" becomes ∂.  Fractions: numerator ctrl / denominator.  Exponents: base ctrl ^ exponent.  Subscripts: base ctrl - subscript.

Built in functions

Numeric result

Pi//N

N[Pi,10000]

2D Graphs - Plot

Plot[x Sin[x],{x,-2π,6π}]

Plot[{x,x Sin[x]},{x,-2π,6π}]

3D Graphs - Plot3D, ParametricPlot3D, and ListPlot

Plot3D[(x^2)-(y^2),{x,-2,2},{y,-2,2}]

ParametricPlot3D[{Cos[t],Sin[t],t},{t,0,4π},BoxRatios->2,Ticks->None,AxesLabel->{x,y,z}]

L:={Table[{i,(i^2)},{i,-.75,.75,(1/10)}],Table[{i,-Abs[i]+1.25},{i,-.25,.25,(1/10)}],Table[{(1/10)Cos[i]-(1/2),(1/10)Sin[i]+(3/2)},{i,0,2π,(π/5)}],Table[{(1/10)Cos[i]+(1/2),(1/10)Sin[i]+(3/2)},{i,0,2π,(π/5)}]}

ListPlot[L,Axes->False]

Creating your own functions

Notes:
- use brackets "[ ]"
- indicate each argument with an underscore " _ "
- use a colon-equals when defining functions " := "  This is not an absolute - you can do the research as to why it is usually done this way.

Example 1

f[x_]:=(x^2);

f[-3]

f[☺]

Example 2

g[n_]:=Table[i!,{i,0,n}]

g[3]

g[30]//TableForm

Example 3

f=. (*this clears any previous occurances of the h - very handy on occasion*)

f[x_,c_]:=Sin[x-c]; (*redefine f*)

Plot[f[x,1],{x,0,2π},Ticks->{{1,π+1}},AxesLabel->{x,y}]

Notes:
- it is tempting to use letters such as E, I, C, and D.  However, these have other uses.

Linear Algebra

Example 1: Entering and manipulating matrices

A={{1,2},{3,4}} (*enter as a list of lists*)

A//MatrixForm (*express in our usual notation*)

A[[2,1]] (*extract an element*)

A[[2]] (*extracts the second row*)

Transpose[A]//MatrixForm (*switches rows and columns*)

Det[A] (*finds the determinant of a square matrix*)

Inverse[A]//MatrixForm (*the matrix inverse*)

Tr[A] (*the trace of a matrix*)

Dimensions[A] (*the dimensions of a matrix*)

Example 2: Manipulating multiple matrices

a.) Note: Mathematica "remembers" matrix A from the previous example.

B={{5,6},{7,8}}; (*matrix defined as above*)

A+B//MatrixForm

b.) The key here is that you need to use MatrixForm on results and not when defining matrices

F={{9,10},{11,12}}//MatrixForm ;(*matrix defined in MatrixForm*)

B+F//MatrixForm

Example 3: Adding Matrices

a.) Matrices are added by adding entries in equivalent locations.  This is a good chance to learn the function Table and/or Array (much cleaner).

A.B//MatrixForm

Example 4: Row Reducing Matrices

a.) Row reduction is pretty straightforward

G = {{1, 2, 3}, {4, 5, 6}}; G // MatrixForm
RowReduce[G]//MatrixForm

b.) Two ways to extract the solutions from the resulting matrix.

Transpose[RowReduce[G]][[3]]//MatrixForm (*There has to be a better way to pull out the solutions ...*)
RowReduce[G][[All,3]]//MatrixForm (*Now that is a nice solution*)

c.) Non-numeric matrices may also be row reduced

(J={{1,2,-3},{a,-2,5}})//MatrixForm
RowReduce[J]//MatrixForm

d.) Sometimes we want to extract the square portion of an augmented matrix out ... there has gotta be a better way to do this, but here is what I came up with.

J//MatrixForm
Transpose[Take[Transpose[J],{1,2}]]//MatrixForm (*there has gotta be a better way to do this,but here is what I came up with.*)
J[[All,{1,2}]]//MatrixForm (*or an alternative method*)

e.) Looking back to part (c.), notice that the determinant is zero at the same time as the system associated with matrix J would have no solution.

Det[%] (*Finds the determinant of the previous result*)
Solve[%==0,a]

Example 5: Multiplying Matrices

a.) Write a command to multiply the matrices G and H together.  This is a good chance to learn the functions: Array and Dimensions

(H={{9,8,7,6},{5,4,3,2},{1,3,5,9}})//MatrixForm
G.H//MatrixForm