Mathematical Tools for Materials Scientists and Engineers

Learning to use Mathematica as a tool.

Starting up Mathematica

First you must locate or obtain Mathematica and permission to use it.
    
    As an MIT student, you have an Athena account from which you can run Mathematica.  There are a number of
ways to open and run a Mathematica session.  Of course,you must first login, and then one method would be to:
    1. open a shell
    2. Add the Mathematica locker by typing: add math
     2. At the shell prompt, type mathematica.  (An interface to mathematica's software (properly known as Mathematica's  kernel) called the Front End will open as well as menus and palettes).
     3. Solve problems by defining symbols, performing mathematical operations, employing Mathematica's functions,defining your own functions, and graphically displaying results.
    
    Also, as an MIT student, you are entitled to download a  copy and license for $30 so that you can run Mathematica on your own computer.  Instructions can be found at this URL: http://itinfo.mit.edu/article.php?id=6842
    The colors on your sceen may not appear to be the same as what is presented in the lectures. I use my own style sheet, you may download my style sheet from here.  Information on where to put the style sheet can be found below.  Let's get started with some simple Mathematica commands.
    If you are reading this in the Mathematica FrontEnd, then you can go ahead and familiarize yourself with some basics by executing the following lines which are Mathematica Input.  Typically, Mathematica's FrontEnd asks Mathematica's Kernal to do its job of evaluating by hitting [SHIFT]—[ENTER] while the mouse in an "Input Cell." Input Cells can be identified with the ↓-thingy at the right.  One can evaluate one or more cells by selecting their ↓-thingies and hitting [SHIFT]—[ENTER].

    Try executing the cells below.  Try to guess what the output might be or represent—and observe carefully whether Mathematica is doing what you would anticipate.  Notice that answers can depend on the history of commands that precede it.


Assigning  values to symbols

In[190]:=

a = 4 π 3

Out[190]=

4 π 3

In[191]:=

UnitSphereVolume = a

Out[191]=

4 π 3

In[192]:=

2 a

Out[192]=

8 π 3

In[193]:=

ANewVariable = ( 2 a + b ) ^ 2

Out[193]=

( b + 8 π 3 ) 2

In[194]:=

ANewVariable ^ 2

Out[194]=

( b + 8 π 3 ) 4

In[195]:=

b = 4 ( 3.14159265358979 ) 3

Out[195]=

4.188790204786386

In[196]:=

UnitSphereNumericalVolume = b

Out[196]=

4.188790204786386

In[197]:=

ANewVariable

Out[197]=

157.91367041742959


Differences between exact expressions and numerical expressions

In[198]:=

UnitSphereVolume - UnitSphereNumericalVolume

Out[198]=

4.440892098500626 × 10 - 15

In[199]:=

a - 4 ArcCos [ - 1 ] 3

Out[199]=

0

In[200]:=

a - 4 ArcCos [ - 1. ] 3

Out[200]=

0.

In[201]:=

2 Pi - 2 ( 3.141519 )

Out[201]=

0.00014730717958588713

In[202]:=

N [ 5 / 6 ]

Out[202]=

0.8333333333333334

Distinction between Equality (= = ) and Assignment (=)

In[203]:=

a 4 ArcCos [ - 1 ] 3

Out[203]=

True

In[204]:=

a 4 ( 3.14159 ) 3

Out[204]=

False

Clearing Variables

In[205]:=

? a

Global`a

a = 4 π 3

In[206]:=

Clear [ a ]

In[207]:=

? a

Global`a

In[208]:=

a - 4 ArcCos [ - 1 ] 3

Out[208]=

a - 4 π 3

clearing all defined variables

In[209]:=

Clear [ Global`* ]

Mathematica Functions

In[210]:=

a = 1 / Exp [ x ]

Out[210]=

- x

In[211]:=

b = Cos [ x ]

Out[211]=

Cos [ x ]

In[212]:=

c = ( a + b ) ^ 2

Out[212]=

( - x + Cos [ x ] ) 2

Alternative Syntax for Functions //

In[213]:=

AnotherVersionofb = x // Cos

Out[213]=

Cos [ x ]

In[214]:=

ANewVariable [ x ]

Out[214]=

ANewVariable [ x ]

Mathematica Operations on expressions

In[215]:=

c

AnotherVersionofC = Expand[c]

Out[215]=

( - x + Cos [ x ] ) 2

Out[216]=

- 2 x + 2 - x Cos [ x ] + Cos [ x ] 2

In[217]:=

c

Simplify[AnotherVersionofC]

Out[217]=

( - x + Cos [ x ] ) 2

Out[218]=

- 2 x ( 1 + x Cos [ x ] ) 2

In[219]:=

Simplfy [ c ]

General :: spell1 : Possible spelling error: new symbol name \" Simplfy \" is similar to existing symbol \" Simplify \". More… "Possible spelling error: new symbol name \\\"\\!\\(Simplfy\\)\\\" is similar to existing symbol \\\"\\!\\(Simplify\\)\\\". \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"General::spell1\\\"]\\)"

Out[219]=

Simplfy [ ( - x + Cos [ x ] ) 2 ]

Calculus

In[220]:=

IntegralofC = Integrate [ c , x ]

Out[220]=

1 2 ( - - 2 x + x + 2 - x Sin [ x ] + Cos [ x ] ( - 2 - x + Sin [ x ] ) )

In[221]:=

Integrate [ c / x , x ]

Out[221]=

1 2 CosIntegral [ 2 x ] + ExpIntegralEi [ - 2 x ] + ExpIntegralEi [ ( - 1 - ) x ] + ExpIntegralEi [ ( - 1 + ) x ] + Log [ x ] 2

Getting information (part 1)

In[222]:=

? ExpIntegralEi

ExpIntegralEi[z] gives the exponential integral function Ei(z). More…

Calculus, continued

In[223]:=

D [ ANewVariable [ x ] , x ]

Out[223]=

ANewVariable [ x ]

In[224]:=

Integrate [ ANewVariable [ x ] , x ]

Out[224]=

ANewVariable [ x ] x

In[225]:=

D [ ANewVariable [ x ] , z ]

Out[225]=

0

In[226]:=

tempvar = Integrate [ ANewVariable [ x ] , { x , 0 , y } ]

Out[226]=

0 y ANewVariable [ x ] x

In[227]:=

D [ tempvar , x ]

Out[227]=

0

In[228]:=

D [ tempvar , y ]

Out[228]=

ANewVariable [ y ]

In[229]:=

Clear [ tempvar ] ;

In[230]:=

Factor [ IntegralofC ]

Out[230]=

1 2 - 2 x ( - 1 + 2 x x - 2 x Cos [ x ] + 2 x Sin [ x ] + 2 x Cos [ x ] Sin [ x ] )

In[231]:=

IntegralofC

AnotherVersionofIntegralofC = Integrate[AnotherVersionofC, x]

Out[231]=

1 2 ( - - 2 x + x + 2 - x Sin [ x ] + Cos [ x ] ( - 2 - x + Sin [ x ] ) )

Out[232]=

1 2 ( - - 2 x + x + 2 - x Sin [ x ] + Cos [ x ] ( - 2 - x + Sin [ x ] ) )

In[233]:=

c

D[IntegralofC, x]

Out[233]=

( - x + Cos [ x ] ) 2

Out[234]=

1 2 ( 1 + 2 - 2 x + 2 - x Cos [ x ] + Cos [ x ] ( 2 - x + Cos [ x ] ) - 2 - x Sin [ x ] - Sin [ x ] ( - 2 - x + Sin [ x ] ) )

In[235]:=

Factor[c]

Simplify[D[IntegralofC, x]]

Out[235]=

- 2 x ( 1 + x Cos [ x ] ) 2

Out[236]=

- 2 x ( 1 + x Cos [ x ] ) 2

Plotting Functions

In[237]:=

Plot [ IntegralofC , { x , 0 , 10 } ]

[Graphics:HTMLFiles/index_11.gif]

Out[237]=

Graphics

In[238]:=

Plot [ { IntegralofC , c } , { x , 0 , 10 } ]

[Graphics:HTMLFiles/index_12.gif]

Out[238]=

Graphics

In[239]:=

Plot [ c , { x , 0 , 10 } , PlotRange { 0 , 0.0001 } ]

[Graphics:HTMLFiles/index_13.gif]

Out[239]=

Graphics

In[240]:=

Options [ Plot ]

Out[240]=

{ AspectRatio 1 GoldenRatio , Axes Automatic , AxesLabel None , AxesOrigin Automatic , AxesStyle { Thickness [ 0.01 ] } , Background Automatic , ColorOutput Automatic , Compiled True , DefaultColor Automatic , DefaultFont $DefaultFont , DisplayFunction $DisplayFunction , Epilog { } , FormatType $FormatType , Frame False , FrameLabel None , FrameStyle Automatic , FrameTicks Automatic , GridLines None , ImageSize Automatic , MaxBend 10. , PlotDivision 30. , PlotLabel None , PlotPoints 25 , PlotRange Automatic , PlotRegion Automatic , PlotStyle { { Thickness [ 0.0075 ] , PointSize [ 0.01 ] } } , Prolog { } , RotateLabel True , TextStyle $TextStyle , Ticks Automatic }

In[241]:=

Plot [ { IntegralofC , c } , { x , 0 , 10 } , PlotStyle { { RGBColor [ 1 , 0 , 0 ] , Thickness [ 0.005 ] } , { RGBColor [ 1 , 0 , 1 ] , Thickness [ 0.0075 ] } } , TextStyle { FontFamily -> Helvetica , FontSize 24 } , PlotLabel -> A Function (Purple)\nand Its Integral (Red)\n , AxesLabel { Value , Argument } , ImageSize 800 ]

[Graphics:HTMLFiles/index_14.gif]

Out[241]=

Graphics

Lists {} and Matrices {{}} (Lists of Lists)

Note  that we have already used lists in Plot and in Integrate

In[242]:=

Clear [ Global`* ]

In[243]:=

AList = { a , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

General :: spell1 : Possible spelling error: new symbol name \" AList \" is similar to existing symbol \" List \". More… "Possible spelling error: new symbol name \\\"\\!\\(AList\\)\\\" is similar to existing symbol \\\"\\!\\(List\\)\\\". \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"General::spell1\\\"]\\)"

Out[243]=

{ a , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

In[244]:=

Length [ AList ]

Out[244]=

8

In[245]:=

Cos [ AList ]

Out[245]=

{ Cos [ a ] , Cos [ b ] , Cos [ 2 ] , Cos [ 7 ] , Cos [ 9 ] , 0.26749882862458735 , 0 , 1 }

In[246]:=

AList

AList[[2]]

Out[246]=

{ a , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

Out[247]=

b

In[248]:=

AList

AList[[{3, 6}]]

Out[248]=

{ a , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

Out[249]=

{ 2 , 1.3 }

In[250]:=

AList

AList[[-2]]

Out[250]=

{ a , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

Out[251]=

π 2

In[252]:=

Sort [ AList ]

Out[252]=

{ 0 , 1.3 , 2 , 7 , 9 , a , b , π 2 }

In[253]:=

Select [ AList , NumberQ ]

Out[253]=

{ 2 , 7 , 9 , 1.3 , 0 }

In[254]:=

Reverse [ Sort [ Select [ AList , NumberQ ] ] ]

Out[254]=

{ 9 , 7 , 2 , 1.3 , 0 }

In[255]:=

Select [ AList , EvenQ ]

Out[255]=

{ 2 , 0 }

In[256]:=

Select [ AList , PrimeQ ]

Out[256]=

{ 2 , 7 }

In[257]:=

Perms = Permutations [ Select [ AList , ExactNumberQ ] ]

Out[257]=

{ { 2 , 7 , 9 , 0 } , { 2 , 7 , 0 , 9 } , { 2 , 9 , 7 , 0 } , { 2 , 9 , 0 , 7 } , { 2 , 0 , 7 , 9 } , { 2 , 0 , 9 , 7 } , { 7 , 2 , 9 , 0 } , { 7 , 2 , 0 , 9 } , { 7 , 9 , 2 , 0 } , { 7 , 9 , 0 , 2 } , { 7 , 0 , 2 , 9 } , { 7 , 0 , 9 , 2 } , { 9 , 2 , 7 , 0 } , { 9 , 2 , 0 , 7 } , { 9 , 7 , 2 , 0 } , { 9 , 7 , 0 , 2 } , { 9 , 0 , 2 , 7 } , { 9 , 0 , 7 , 2 } , { 0 , 2 , 7 , 9 } , { 0 , 2 , 9 , 7 } , { 0 , 7 , 2 , 9 } , { 0 , 7 , 9 , 2 } , { 0 , 9 , 2 , 7 } , { 0 , 9 , 7 , 2 } }

In[258]:=

Dimensions [ Perms ]

Out[258]=

{ 24 , 4 }

In[259]:=

Transpose [ Perms ] // MatrixForm

Out[259]//MatrixForm=

( 2 2 2 2 2 2 7 7 7 7 7 7 9 9 9 9 9 9 0 0 0 0 0 0 7 7 9 9 0 0 2 2 9 9 0 0 2 2 7 7 0 0 2 2 7 7 9 9 9 0 7 0 7 9 9 0 2 0 2 9 7 0 2 0 2 7 7 9 2 9 2 7 0 9 0 7 9 7 0 9 0 2 9 2 0 7 0 2 7 2 9 7 9 2 7 2 )

In[260]:=

TranPerms = Transpose [ Perms ] ;

In[261]:=

TranPerms [ [ 3 ] ]

Out[261]=

{ 9 , 0 , 7 , 0 , 7 , 9 , 9 , 0 , 2 , 0 , 2 , 9 , 7 , 0 , 2 , 0 , 2 , 7 , 7 , 9 , 2 , 9 , 2 , 7 }

In[262]:=

TranPerms [ [ 4 , 1 ] ]

Out[262]=

0

In[263]:=

TranPerms [ [ 1 , 4 ] ]

Out[263]=

2

In[264]:=

TranPerms [ [ 5 , 1 ] ]

Part :: partw : Part 5 of { { 2 , 2 , 2 , 2 , 2 , 2 , 7 , 7 , 7 , 7 , 14 } , { 7 , 7 , 9 , 9 , 0 , 0 , 2 , 2 , 9 , 9 , 14 } , { 9 , 0 , 7 , 6 , 0 , 14 } , { 0 , 9 , 0 , 7 , 9 , 7 , 0 , 9 , 0 , 2 , 14 } } does not exist. More… "Part \\!\\(5\\) of \\!\\({\\(\\({2, 2, 2, 2, 2, 2, 7, 7, 7, 7, \\(\\(\[LeftSkeleton] 14 \[RightSkeleton]\\)\\)}\\)\\), \\(\\({7, 7, 9, 9, 0, 0, 2, 2, 9, 9, \\(\\(\[LeftSkeleton] 14 \[RightSkeleton]\\)\\)}\\)\\), \\(\\({9, 0, 7, \\(\\(\[LeftSkeleton] 6 \[RightSkeleton]\\)\\), 0, \\(\\(\[LeftSkeleton] 14 \[RightSkeleton]\\)\\)}\\)\\), \\(\\({0, 9, 0, 7, 9, 7, 0, 9, 0, 2, \\(\\(\[LeftSkeleton] 14 \[RightSkeleton]\\)\\)}\\)\\)}\\) does not exist. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"General::partw\\\"]\\)"

Out[264]=

{ { 2 , 2 , 2 , 2 , 2 , 2 , 7 , 7 , 7 , 7 , 7 , 7 , 9 , 9 , 9 , 9 , 9 , 9 , 0 , 0 , 0 , 0 , 0 , 0 } , { 7 , 7 , 9 , 9 , 0 , 0 , 2 , 2 , 9 , 9 , 0 , 0 , 2 , 2 , 7 , 7 , 0 , 0 , 2 , 2 , 7 , 7 , 9 , 9 } , { 9 , 0 , 7 , 0 , 7 , 9 , 9 , 0 , 2 , 0 , 2 , 9 , 7 , 0 , 2 , 0 , 2 , 7 , 7 , 9 , 2 , 9 , 2 , 7 } , { 0 , 9 , 0 , 7 , 9 , 7 , 0 , 9 , 0 , 2 , 9 , 2 , 0 , 7 , 0 , 2 , 7 , 2 , 9 , 7 , 9 , 2 , 7 , 2 } } 5 , 1

In[265]:=

TranPerms [ [ { 1 , 2 } ] ]

Out[265]=

{ { 2 , 2 , 2 , 2 , 2 , 2 , 7 , 7 , 7 , 7 , 7 , 7 , 9 , 9 , 9 , 9 , 9 , 9 , 0 , 0 , 0 , 0 , 0 , 0 } , { 7 , 7 , 9 , 9 , 0 , 0 , 2 , 2 , 9 , 9 , 0 , 0 , 2 , 2 , 7 , 7 , 0 , 0 , 2 , 2 , 7 , 7 , 9 , 9 } }

In[266]:=

IntList = Table [ i , { i , 1 , Length [ TranPerms [ [ 1 ] ] ] } ]

Out[266]=

{ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 }

Selecting  the odd numbered columns from a matrix

In[267]:=

TranPerms//MatrixForm

TranPerms[[All, Select[IntList, OddQ]]]//MatrixForm

Out[267]//MatrixForm=

( 2 2 2 2 2 2 7 7 7 7 7 7 9 9 9 9 9 9 0 0 0 0 0 0 7 7 9 9 0 0 2 2 9 9 0 0 2 2 7 7 0 0 2 2 7 7 9 9 9 0 7 0 7 9 9 0 2 0 2 9 7 0 2 0 2 7 7 9 2 9 2 7 0 9 0 7 9 7 0 9 0 2 9 2 0 7 0 2 7 2 9 7 9 2 7 2 )

Out[268]//MatrixForm=

( 2 2 2 7 7 7 9 9 9 0 0 0 7 9 0 2 9 0 2 7 0 2 7 9 9 7 7 9 2 2 7 2 2 7 2 2 0 0 9 0 0 9 0 0 7 9 9 7 )

Rules  →   and    Replacement /.

In[269]:=

ARule = a π 3

General :: spell1 : Possible spelling error: new symbol name \" ARule \" is similar to existing symbol \" Rule \". More… "Possible spelling error: new symbol name \\\"\\!\\(ARule\\)\\\" is similar to existing symbol \\\"\\!\\(Rule\\)\\\". \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, ButtonData:>\\\"General::spell1\\\"]\\)"

Out[269]=

a π 3

In[270]:=

a

Out[270]=

a

In[271]:=

AList

AList/.ARule

Out[271]=

{ a , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

Out[272]=

{ π 3 , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

In[273]:=

SomeRules = { ARule , b -> π 12 }

Out[273]=

{ a π 3 , b π 12 }

In[274]:=

AList /. SomeRules

Out[274]=

{ π 3 , π 12 , 2 , 7 , 9 , 1.3 , π 2 , 0 }

In[275]:=

a = SomeOtherSymbol ;

In[276]:=

AList

Out[276]=

{ SomeOtherSymbol , b , 2 , 7 , 9 , 1.3 , π 2 , 0 }

In[277]:=

StrangeRule = { Rational [ x_ , y_ ] y / x }

Out[277]=

{ Rational [ x_ , y_ ] y x }

In[278]:=

( AList /. SomeRules ) /. StrangeRule

Out[278]=

{ 3 π , 12 π , 2 , 7 , 9 , 1.3 , 2 π , 0 }

Getting Help

Several  methods of getting help can be found in the above examples.
    1.  Mathematica indicated that a possible type might have occured and returned a "warning message" with a link to Mathematica's Help Browser.  Some people find typo and spelling warnings to be annoying.  Spelling warnings can be disabled by typing Off[General::spell1].  However, prudence and experience show that undiscovered typos can be even more annoying.
    
    2.  Typing ?ExpIntegralEi  returned information about the symbol  ExpIntegralEi.  Typing ??FunctionName gives even more information—try ??Plot.  Wildcards can also by used as demonstrated below.

In[279]:=

? *Exp*

System`
ButtonExpandable ExpandAll ExportAutoReplacements MantissaExponent ToHeldExpression
CofactorExpansion ExpandDenominator ExportPacket MathieuCharacteristicExponent TrigExpand
ComplexExpand ExpandNumerator ExportString MatrixExp TrigToExp
CompoundExpression ExpIntegralE Expression OutputMathEditExpression $ExportEncodings
DoubleExponential ExpIntegralEi ExpressionPacket PiecewiseExpand $ExportFormats
EllipticExp Exponent ExpToTrig PowerExpand $LicenseExpirationDate
EllipticExpPrime ExponentFunction FunctionExpand RegularExpression
EnterExpressionPacket ExponentPosition IntegerExponent ReturnExpressionPacket
Exp ExponentStep LogicalExpand StringExpression
Expand Export MakeExpression ToExpression
DiscreteMath`Combinatorica`
ExpandGraph
Statistics`DescriptiveStatistics`
ExpectedValue

Each of the above is linked to Mathematica's Help Browser.

3.  Typing Options[Plot] returned a list of options that can be adjusted by the user until the result (in this case the appearance) of the plot is satisfactory.

Mathematica's Help Browser is a very useful tool and will probably become a primary resource for students.  It contains good tutorials and demonstrations that can be copied and pasted.  It has very good and concise descriptions of mathematics; in fact, exploring the Help Browser is a good way to explore mathematics as well as Mathematica.  For instance , the discussion of "Combinatorial Functions"  under Mathematical Functions ▸ Combinitorial has summarizes some useful information about counting and parttioning of sets.


Created by Mathematica  (October 24, 2006) Valid XHTML 1.1!