Académique Documents
Professionnel Documents
Culture Documents
Lecture 1
Slide 1 of 10
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam
ECM6Lecture1Vietnam_2014.nb
10
Overview of ECM 6
In this class you will learn programming methods for solving problems in chemical engineering using
Mathematica. You will also learn how to construct numerical algorithms for finding approximate solutions to problems that cannot be solved analytically.
CONCEPTS:
Syntax of a programming language
Algorithms.
Control structure and program design
Data structures
Iteration and Recursion
Solution of algebraic and differential equations
Visualization of data - graphics programming
SKILLS:
Proficiency in using Mathematica
Programming concepts and programming styles.
Implementing numerical methods in software.
Importing and exporting data
Applying numerical methods to chemical engineering problems
Please be sure to visit the above site so that you are familiar with the grading policies, homework and
exam schedules, and course outline. TA office hours are listed on the web site
ECM6Lecture1Vietnam_2014.nb
Software Requirement
Homework assignments will require students to use Mathematica 8.0 or Mathematica 9.0. We have
also arranged for students to use the PCs in a HMUG computer lab, with PCs with Mathematica
installed.
ECM6Lecture1Vietnam_2014.nb
10
Lab Schedule
We will meet Tuesday and Thursday in HMUG Computer lab
Homework Assignments
Homework assignments will be due Friday morning and Wednesday Morning. First HW assignment
due Friday April 4.
Exam Scheduule
First exam will be held Tuesday April 8.
Second exam will be held Tuesday, April 15.
Practice exams will be posted Saturday April 5 and Saturday April 12.
ECM6Lecture1Vietnam_2014.nb
10
ECM6Lecture1Vietnam_2014.nb
10
Structure of Mathematica
Kernel/Front End
Mathematica consist of two separate programs: The Kernel and the Front End. These two programs
communicate with each other through a cross platform transport layer (communication protocol) called
MathLink. The Kernel is the computational engine, while the Front End is the user interface.
Through the Front End the user can send commands (or requests) to the Kernel. When you start Mathematica, it is the Front End that is started. The first request to the Kernel causes the Kernel to be loaded
in memory.
Consider the sequence of events that takes place when you want to plot a prescribed function.
In[1]:=
When the plot command is executed in the Front End (using the Shift-Return or Shift-Enter command),
the request is sent to the Kernel for processing. The Kernel then sends back to the Front End a
PostScript code that is displayed in the Front End. This bidirectional communication is done through
MathLink.
The Front End handles graphics, annimations, text , sound and communicates with other peripheral
devices such as a printer.
When an input cell is evaluated (highlight the cell bracket, and then press Shift-Return or Shift-Enter),
the result of the Kernel computation is sent back to the Front End and displayed in an output cell. Here
is a typical output cell that results when the previous request is sent to the Kernel:
9
The formating of cells (font type, font size, font size, cell structure, color, etc.) is determined by the
"style sheet" used for the notebook. Mathematica comes preloaded with many different style sheets,
and a user can also define additional stylesheets.The style sheets that are available to the user can be
found under the format menu: FormatStyleSheet. One can readily determine what the cell type is by
examining the top corner of a cell bracket. A text cell has two horizontal lines at the top corner; an input
cell has an angled bracket; a output cell has an angled bracket with a horizontal line.
If you examine this notebook you will see that there are various types of text cells. By grouping and
nesting cells, the user can design a hierarchical document. If you double click on a bracket that has
nested cells, the cells will open or close. In this way a particular portion of the document, can be temporarily revealed or hidden.
ECM6Lecture1Vietnam_2014.nb
If you examine this notebook you will see that there are various types of text cells. By grouping and
nesting cells, the user can design a hierarchical document. If you double click on a bracket that has
nested cells, the cells will open or close. In this way a particular portion of the document, can be temporarily revealed or hidden.
Mathematica notebooks are ASCII files. Hence a notebook created on a Linux computer and be
opened and run on a Windows computer or Macintosh computer. ASCII files an be easily sent through
email. In summary, notebooks are extremely portable and can be readily shared across computer
platforms.
ECM6Lecture1Vietnam_2014.nb
10
Functions in Mathematica
Mathematica comes with over 4100 built-in functions. The default syntax for a Mathematica function is:
FunctionName[arg1, arg2, ]
In[2]:=
Out[2]=
Length@Names@"*"DD
4626
Some Examples
Here are some examples:
Sin[z] gives the sine of z.
Plot[f, {x, xmin , xmax }] generates a plot of f as a function of x from xmin to xmax .
Solve[eqns, vars] attempts to solve an equation or set of equations for the variables vars.
Note that all built-in functions are capitalized, and the arguments of the function are wrapped with
square brackets.
Functions may be nested within each other as in the following example
Plot@Sin@3 xD, 8x, 0, 2 <D
ECM6Lecture1Vietnam_2014.nb
10
Basics
Ordinarily, in mathematics we use parentheses to define a variety of operations. In most cases Mathematica uses a different notation. It is helpful to remember the distinction for the following operations:
Mathematical Operation
grouping variables
Coordinates in n-dimensions
Function applied to a variable t
Mathematics
a(b+c)
(x1,x2, ...xn)
f(t)
Mathematica
aHb + cL
8x1, x2, ... xn<
f @tD
Mathematica
a+b, a-b
a/b
a*b, or a b
a^2
When you evaluate an Input cell (click on the cell bracket and press Shift-Return), the expression in the
Input cell is sent to the kernel where it is evaluated and the result is displayed in an Output cell. The
top corner of the Output cell glyph has a triangular notch above a horizontal line. Mathematica assigns
a number to each Input/Output exchange. In the above figure the input is labelled In[13]:= ; the output is
labelled Out[13]=. We will show later that you can refer to previous inputs and outputs using these
labels.
Also show in Figure 1 is a cell glyph that has a horizontal notch at the top corner. This is a non-evaluatable cell and is used primarily for holding text. If you try to evaluate a non-evaluatable cell, Mathematica
will beep. In a future lecture we will discuss in more detail the taxonomy cell glyphs. But for now it will
suffice to recognize what are "text" cells, Input cells and a Output cells.
10
ECM6Lecture1Vietnam_2014.nb
Also show in Figure 1 is a cell glyph that has a horizontal notch at the top corner. This is a non-evaluatable cell and is used primarily for holding text. If you try to evaluate a non-evaluatable cell, Mathematica
will beep. In a future lecture we will discuss in more detail the taxonomy cell glyphs. But for now it will
suffice to recognize what are "text" cells, Input cells and a Output cells.
The names of all built-in functions in Mathematica are begin with a capital letter, and use square brackets [ ] to enclose the arguments.
For example, the cosine function, commonly represented in mathematics texts as cos(x) is defined as
Cos[x] in Mathematica.
The combination symbolName and [ ] defines a Mathematica expression. (All built-in functions in
Mathematica are expressions). When the square brackets are absent, we have a symbol. For convenience, Mathematica has a number of short cuts for common operations i.e., + for Plus.
Consider next the multiplication operator
4*5
Times@4, 5D
For convenience, a "space" character can be used for multiplication instead of *. Thus
45
You can also use normal parentheses to change the order of evaluation in a Mathematica expression.
3 H4 - 9L
ECM6Lecture1Vietnam_2014.nb
11
10
Palettes
Input Format vs Typeset Format
Mathematica allows you to enter mathematical expressions in a one-dimensional input format or to use
a conventional two-dimensional typeset format.
For example, x^4 is the one-dimensional input format, while x4 is the two-dimensional typeset format.
Mathematica understands both formats.
With the input format the user can type any Mathematica function, or operator using a traditional ASCII
keyboard.
The two dimensional format also includes conventional mathematical operators such as . Thus, onedimensional format for the indefinite integral of x2 is represented as Integrate[x^2,x], while the twodimensional format is x2 x.
225
zi
i=1
12
ECM6Lecture1Vietnam_2014.nb
sum4i = 1 % 5a^i
or more quickly, strat with the summation operator
sumt
and then tab through the limits and argument fields.
The Mathematica Book describes numerous keyboard sequences for entering a various of two-dimensional formats. See Sec 1.10.1 and 1.10.2 in the Mathematica book. A handy reference is also on the
menu bar: Select Palettes and then scroll down to Basic Math Assistant
ECM6Lecture1Vietnam_2014.nb
13
10
Help Options
Help Browser
Mathematica's on-line help centers around the Help Browser, a hypertext help system. To access the
help browser go to the Help menu and choose Document Center or Function Navigator. If you want
to know about a particular function in Mathematica, highlight it and then choose Find Selected Function... from the Help menu.
Text-based Help
The Question Mark function ? allows you to get basic information about a particular Mathematica
function from the FE. For example, suppose we want to find out how to use the derivative function D,
then
?D
The double question mark ?? gives the same information as ? but also gives information about
attributes and options.
?? D
If you are trying to recall a function that has the word Plot in it then you can use wild card * in conjunction with the word Plot, as shown below:
? *Plot*
14
ECM6Lecture1Vietnam_2014.nb
f 10
Numerics
Numbers
In Mathematica there are 4 types of numbers:
Integer, is a number that does not contain a decimal point such as 1 , 45, 456
Real, is a number that contains an explicit decimal point such as 4.0, 34.789
Rational, is a number that has the form integer/integer such as 3/4, 23/47
Complex, is a number that has the form + I such as 4+5.6 I where I represents
and may be Real, Integer, or Rational numbers.
-1 . Note
Additional Notes
Exact Arithmetic
Mathematica is capable of doing exact arithmetic if integers are used in the calculation. Consider the
following examples:
Example 1
We ask Mathematica to divide 233 into 47:
47 233
Since 47/234 is an exact expression, and cannot be factored, Mathematica returns the original
expression.
Example 2
We ask Mathematica to find the square root of 23:
23
Again, Mathematica does not evaluate the expression since we have exact integer under the radical
sign that is not the square of an integer.
ECM6Lecture1Vietnam_2014.nb
15
Again, Mathematica does not evaluate the expression since we have exact integer under the radical
sign that is not the square of an integer.
Example 3
We ask Mathematica to evaluate 4/5 +
4
1 25 :
1
+
25
In this case Mathematica can evaluate the expression using exact arithmetic.
Approximate Arithmetic
In the following examples the expressions contain an approximate number:
Example 1
We ask Mathematica to divide 233 into 47.0 . Since the numerator is an approximate real number,
Mathematica evaluates the expression and returns an approximate number.
47.0 233
We can use the wrapper function InputForm to see what number Mathematica actually stores in
memory:
InputForm@47.0 233D
Example 2
We ask Mathematica to evaluate the square root of 23., which is an approximate number
23.
In this case it is 16 digits, which is the machine precision limit. You can determine the machine precision
of your computer by evaluating
$MachinePrecision
In Mathematica all system variables begin with a $. Thus $MachinePrecision gives the number of
decimal digits of precision of an approximate number on your computer system.
The function Accuracy[x] gives the number of significant decimal digits to the right of the decimal point.
Consider these two examples:
16
ECM6Lecture1Vietnam_2014.nb
The function Accuracy[x] gives the number of significant decimal digits to the right of the decimal point.
Consider these two examples:
Example 1
47./233
8InputForm@47. 233D, Precision@47. 233D, Accuracy@47. 233D<
In this example the result is a number with machine precision (16 digits) and an accuracy of 16 digits.
Example 2
4700.0/233
8InputForm@4700.0 233D, Precision@4700.0 233D, Accuracy@4700.0 233D<
In this example the result is a number with machine precision (16 digits) and an accuracy of 14 digits.
23 F
If we want the above result accurate to 25 digits, then we can supply a second argument to N
NB
23 , 25F
Let us try the same calculation with the earlier approximate number 47./233
Precision@N@47. 233, 25DD
We see that we still have 16 digits of precision. The reason is that the numerator of our expression was
only accurate to 16 digits-recall it's a real number. Thus Mathematica does the calculations with
machine precision, even though you asked for higher precision. Suppose we entered an exact number:
47/233 Then we can asked Mathematica to do the calulation to any desired precision. Here is the
calulation with 150 digits of precision!!
N@47 233, 150D
Of course, if we enter a numerator with 26 zeros after the decimal point, then Mathematica can do the
calculation with at least 28 digits of precision, even though we asked for higher precision.
Precision@N@47.00000000000000000000000000 233, 30DD
27.6721
A convenient method for defining a real number with n digits of precision is with the SetPrecision
function
SetPrecision@47.0, 20D
47.000000000000000000
ECM6Lecture1Vietnam_2014.nb
17
There is also a short hand version for SetPrecision that makes use of the "back quote" symbol "`". Thus
In[3]:=
Out[3]=
47.0`20
47.000000000000000000