Introduction to Mathematica 
and Mathematica graphics

by  
 Zafer Gurdal   © Copyright 1995
 Department of Engineering Science and Mechanics 
 Virginia Polytehnic Institute and State University
 Blacksburg, VA 24061-0219

The "Composite Materials Design and Optimization" course makes heavy use of the Mathematica program for demonstrating various concepts through Mathematica note books such as this one.  Several Mathematica programs will also be used throughout the semester to provide a tools that are easily accessible by the students.  Hence, this first tutorial provided in the following is to demonstrate the use of Mathematica and Mathematica graphics as a classroom teaching and tutorial environment for the  course.   Only elementary information about Mathematica is provided in this first tutorial. For more detailed coverage of description and capabilities of this computer based system refer to the book Mathematica by Stephen Wolfram.  As we introduce more subject specific tutorials througout the semester, necessary Mathematica information will be provided to enable the studentsr to perform the required tasks.

Introduction to Mathematica environment

This is a Mathematica front-end file typically called a Notebook file.  Mathematica notebooks can handle text, numerical and symbolic calculations, and graphics.  Few of the features of a notebook environment differ from one platform to another, but the appearance of the notebook is generally uniform over a vide variety of platforms.

Each of these units of text, or numerical or symbolic calculations, or graphics seperated by a right hand bracket is called a cell.  A cell is initiated by clicking in a space where the cursor turns into a horizontal I-beam.   The default cell style is the Input cell where mathematica is expected to act on the cell to do an operation.  Watch for the little bar on the upper end of the bracket to see if the cell is an inactive text cell or an active Input cell.

Style, attributes, font,  allignment, and various characteristics of the cells can be viewed and manipulated using the Style menu. button.  Cell Style under the Style menu lists various cell types available.  Cells can be arranged so that they can be grouped together, in which case a bracket appears to the right of the cell brackets showing the groupings.  By double clicking the larger bracket, grouped cells may be closed or opened.  Watch the rectangular box that appears when you group cells.  The length of the box indicates the amount of material covered by the grouped cells. The menu item Cell may be used to manipulate the cell groupings.

Mathematica operations

Operations in an active cell are performed by a segment of the program called Kernel.  The kernel is activated upon execution of the first active cell which is initiated by putting the cursor at any location in that cell (of course by clicking) and then hitting the enter key (not the return key).  Note that, the initial activation of the kernel takes time.

2 2

2 56975

Mathematica always remembers the results of the output cells.  Anytime they can be retreived by using the %# where # is the number of the output cell.  Using % without the number # always retrieves the output of the previous cell.

%1

Numbers can be assigned to variables.  They will be remembered until they are Unset using =.

five = 2 + 2

five =.

five

Symbolic variables can be operated on, and assigned to another variable forming large symbolic expressions.

five = two + three

fivesqr = five five

two = 2.0three = 2.0

fivefivesqr

Various Mathematica operators (such as Expand[],  Factor[], Simplify[]) may be used to manipulate symbolic expressions.

two =. three =.

fivesqr = Expand[fivesqr]

express = fivesqr + five fivesqr - (((five fivesqr))^2)

Simplify[express]

Expand[%]

Simplify[%]

Rules may be defined by using -> and the replacement operator /. allows you to apply transformation rules to expressions.  Information on the usage of any Mathematica operation may be obtained by putting a ? in front of the operator.   Putting two question marks  ??provides a more detailed information.

?Rule

Mathematica generally has a long and a short form of every command.  Long form of ? is Information[ ] in which the Mathematica command is typed inside the brackets in quoatation marks.   In order to receive more detailed information  (such as attributes, options, and their defaults),  the optional  LongForm -> True is used.    For example,  ??Rule may be replaced by

Information["Rule", LongForm -> True]

Note the semicolon at the end of some of the input lines.

rule1 = x1 -> 5; rule2 = x2 -> 3.0rule3 = x3 -> 2.5rule4 = x4 -> 7.0;

newExp = 4 (x1^2) + (((x1 + x2))^2) + x3 + 6 (x1 + (x4^2)) 2

newExp /. {rule1, rule2, rule3, rule4}

newExp

Rules can be in various forms.

newExp /. {rule1, rule2, x3 -> x1 + x2}

Note that the third rule does not know about rule1 and rule2.  In order to have the rule1 and rule2 to recognize the introduction of x1 and x1 through the third rule, they have to be applied after the rule3 is implemented.

{newExp /. {x3 -> x1 + x2}} /. {rule1, rule2}

newExp /. {x3 -> x1 + x2, x4 -> ((((x1^2) + x2))^2)}

Within the notebooks, there is also graphics capabilities.    Information on  Mathematica operations may be searched by using * as wild character along with the  ? .

?*Plot

?Plot

Graphics Examples

Consider the following two functions.  The two variables for the problem, the w and the h, are limited to be in the range 0.5<w<5.0 , 0.5<h<5.0 units.

fobj1 = w h

fobj2 = (3/(2 w h))

Plot the variation of fobj1 and fobj2 as a function of each of the variables around a nominal design, {wnom, hnom} = {2.5, 2.5}.

wnom = 2.5; hnom = 2.5;

{wnom, hnom} = {2.5, 2.5}

Wnom

fobj1

hnom

Plot[fobj1, {w, 0.5, 5.0}]

fobj1nomh = fobj1 /. h -> hnom

Plot[fobj1nomh, {w, 0.5, 5.0}]

Plot[fobj2 /. h -> hnom, {w, 0.5, 5.0}]

Show[%16, %17, AxesLabel -> {" w ", "f1 and f2"}]

Use  Epilog to put labels on the plot .

Show[%16, %17, AxesLabel -> {" w ", "f1 and f2"}, 
      Epilog -> {Text["fobj1", {3.7, 8.0}], 
          Text["fobj2", {1.4, 1.0}]}]

Clearing Values and Definitions

Often one would like to move from one tutorial to another, or restart the same tutorial from the beginning with no traces of definitions left from the previous notebook executions.  Clear and ClearAll functions in  Mathematica  serve for these purposes, and will be included as the first line in all the subsequent tutorials.

ClearAll["`*"]

?Clear

Information["ClearAll", LongForm -> True]

?Remove