Vous êtes sur la page 1sur 17

ECM 6 Computational Methods

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

Class Web Site


A detail outline for the course is on the ECM6 course web site.
https://sites.google.com/site/ekayasolutionsvietnam2/home

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

Class Hours, Homework, Exams


Lecture Schedule
We will meet Monday-Friday from 9:00 am-12:00 noon

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

Topics for Lecture 1


In this lecture we give a brief over view of the following topics
Structure of Mathematica
Functions in Mathematica
Notation and arithmetic operations
Palettes
Help Options
Numerics

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]:=

Plot@Sin@3 xD, 8x, 0, 3 <D

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.

The Mathematica Notebook


The Mathematica notebook is a graphical Front End program (text based Front end programs are also
available). It is an interactive document that allows you to edit, manipulate type setting, display graphics. One can think of the notebook as an interactive word processor. The document you are looking at
now is a notebook.
Information in a notebook is organized in "cells". The square bracket on the right hand side defines a
cell. There are three types of cells: text, input, and output. The input cell is where you enter commands
that you want to send to the Kernel. Here is a input cell
4+5

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

Notation and Arithmetic Operations


This is a text cell.

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

Arithmetic operations in Mathematica are the same as in FORTRAN:


Mathematical Operation
Addition/subtraction
Division
Multiplication
Exponentiation

Mathematica
a+b, a-b
a/b
a*b, or a b
a^2

Mathematica translates standard arithmetic operations into symbols. Thus


Plus +, Minus -,
Divide /,
Times *,
Power ^
The arithmetic symbols are simply a short-hand notation for a Mathematica built-in function.

Cells: Input cells versus Non-input Cells


When you open up a notebook for the first time, and then type something from the keyboard, a blue
angle bracket appears on the RHS of the page. This is called the cell bracket glyph. Its initial height is
roughly the height of the text you type.
If you use the Return key to create a new line, the Cell bracket glyph increases in height.The default cell
that is opened in the Mathematica notebook is called an Input cell. Its cell bracket glyph has a triangular
notch at the top end. In Figure 1 below the Input cell contains " 3. ".

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.

Examples of Input/Output Cell Exchanges


In this section we will demontrate Input/Output exchanges involving various arithmetic operations.
Consider the operations: Plus(+), Divide(/), Multiply(*) or Multiply( ), Power(^).
Consider first the Plus operator
4+5
Plus@4, 5D

Here is the Divide operator


10 6.8
Divide@10, 6.8D

Here is the exponent operator


5 ^ 10
Power@5, 10D

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.

Two-Dimensional Format using Palettes


To help the user enter two-dimensional (typeset) formats, Mathematica has supplied a set of palettes.
Under the File menus, choose Palettes, and then BasicInput. A palette of buttons appears on the righthand side of the screen. To enter the two-dimensional format for an indefinite integral select the
button, and then use the tab button to navigate through the various place holders. Here are some
examples
(i) Indefinite Integral using the palette button :
2
x x

(ii) Radical expression using the palette button

225

(iii) Summation using the palette button= L


6

zi
i=1

Short Cuts for Entering Expressions


Although Mathematica provides a palette for entering expressions in 2D format, there are also numerous key board operations that allow one to enter complex expressions. Here are some examples:
(i) To enter the integral sign, one types the following three key sequence:
int
and to type in the integral operator
intt

12

ECM6Lecture1Vietnam_2014.nb

and to type in a definite integral operator


dintt.
(ii) To type the summation symbol, you type the following three key sequence:
sum
and to type in a summation operator
sumt
(iii) To type x y you type the following key sequence
x ^ y
You type ^by holding down the Control key, then hitting the ^ key. As soon as you do this,your
cursor will jump to a superscript position.You can then type anything you want and it will appear in that
position.
When you have finished, press to move back down from the superscript position. The quantity
stands for Control-Space; you type it by holding down the Control key, then press the space bar.
5

(iv) To type ai you type the following key sequence


i=1

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

Approximate vs Exact Numbers


Numbers in Mathematica may be exact or approximate. An exact number does not contain a decimal
point
Exact numbers include:
Integers such as 1, 45, 456
Rationals such as 4/57, 2/3
Complex numbers such as 2+7 I, where the real and imaginary parts may be either Integer or
Rational.

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.

Precision versus Accuracy


The function Precision[x] determines the number of significant decimal digits in the number x. Thus the
precision of the following calculation is
Precision@47. 233D

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.

Accuracy and Precision


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.
Note: numbers that are integers or rationals have infinite precision:
8Precision@23D, Precision@4 23D<

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.

Additional Comments of Precision and Accuracy


Note that the functions Precision and Accuracy do not give integer values.
You can force Mathematica to approximate an exact number with the function N. Thus N[x] which gives
the numerical value of x. We saw earlier that 23 is not evaluated by Mathematica . We can force
Mathematica to evaluate the expression by wrapping the expression with N:
NB

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

Vous aimerez peut-être aussi