• Aucun résultat trouvé

Mathematica Basics

Dans le document Second Edition (Page 22-28)

3 + 1

5 + 1 7 + 1

9 + 1

11 or 1/3 + 1/5 + 1/7 + 1/9 + 1/11 3043

3465

1.15 Compute the square root of π exactly using the Sqrt function.

SOLUTION Sqrt[Pi]

π ← This is the only way to represent the square root of π exactly.

1.16 Multiply 8 by 2 . SOLUTION

8 2 or Sqrt[8] *Sqrt[2]

4

1.17 Simplify 3+ 12+ 27+ 48 leaving your answer in radical form.

SOLUTION

3 + 12 + 27 + 48 10 3

1.5 Mathematica Basics

In this section we discuss some of the simpler concepts within Mathematica. Each will be explained in greater detail in a subsequent chapter.

Symbols are defined using any sequence of alphanumeric characters (letters, digits, and certain special characters) not beginning with a digit. Once defined, a symbol retains its value until it is changed, cleared, or removed.

Arithmetic operations are performed in the obvious manner using the symbols +, –, ∗, and /. Exponentiation is represented by a caret, ^, so x^y means xy. Just as in algebra, a missing symbol implies multiplication, so 2a is the same as 2∗a. Be careful, however, when multiplying two symbols, since ab represents the single symbol beginning with a and ending with b. To multiply a by b you must separate the two letters with ∗ or × (on the Basic Math Input palette) or a space: a∗b, a×b, or ab .

EXAMPLE 10 a = 2 b = 3 c = a + b 2 3 5

Notice that the result of each calculation is displayed. This is sometimes annoying, and can be sup-pressed by using a semicolon (;) to the right of the instruction.

EXAMPLE 11 a = 2;

b = 3;

c = a + b 5

Operations are performed in the following order: (a) exponentiation, (b) multiplication and division, (c) addition and subtraction. If the order of operations is to be modified, parentheses, ( ), must be used. Be careful not to use [ ] or { } for this purpose.

EXAMPLE 12 2 + 3 * 5 17

(2 + 3) * 5 25

Each symbol in Mathematica represents something. Perhaps it is the result of a simple numerical calculation or it may be a complicated mathematical expression.

EXAMPLE 13 a = 3;

b = x + 1 2x + 3;

2

Here, a is a symbol representing the numerical value 3 and b is a symbol representing an algebraic expression.

If you ever forget what a symbol represents, simply type ? followed by the symbol name to recall its definition.

EXAMPLE 14 (continuation of Example 13)

?a

Global`a a = 3

?b

Global`b

b x

= 1+ x 3+ 2

2

To delete a symbol so that it can be used for a different purpose, the Clear or the Remove command can be used.

Clear[symbol] clears symbol’s definition and values, but does not clear its attributes, messages, or defaults. symbol remains in Mathematica’s symbol list. Typing symbol =. will also clear the definition of symbol.

Remove[symbol] removes symbol completely. symbol will no longer be recognized unless it is redefined.

You may have noticed that when you begin to type the name of a symbol, it appears with a blue font until it is recognized as a Mathematica command or symbol (possibly user-defined) having some value. Then it turns black. If the symbol is cleared or removed, all instances of the symbol turn blue once again.

Parentheses, brackets, and braces remain purple until completed with a matching mate. Errors caused by having two left parentheses, but only one right parenthesis, for example, can be conveniently spotted.

EXAMPLE 15 (continuation of Example 13) Clear[a]

?a â ?a recalls information about the symbol a.

Global`a Remove[b]

?b

Information õ notfound : Symbol b not found.

(Clicking on  gives more information about the error.) The N command allows you to compute a numerical approximation.

N[expression] gives the numerical approximation of expression to six significant digits (Mathematica’s default).

N[expression, n] attempts to give an approximation accurate to n significant digits.

A convenient shortcut is to use //N to the right of the expression being approximated. Thus, expression//N is equivalent to N[expression]. // can be used for other Mathematica commands as well.

expression //Command is equivalent to Command[expression].

Another shortcut is to type a decimal point anywhere in the expression. This will cause Mathematica to evaluate the expression numerically.

EXAMPLE 16 1

2+1 3-1

5 19 30 1 2+1

3-1

5 . Note the decimal point after the 5.

0.633333 EXAMPLE 17

N[o] or o//N 3.14159 N[π, 50]

3.1415926535897932384626433832795028841971693993751

The Mathematica kernel keeps track of the results of previous calculations. The symbol % returns the result of the previous calculation, %% gives the result of the calculation before that, %%% gives the result of the calculation before that and so forth. Using % wisely can save a lot of typing time.

EXAMPLE 18 To construct π+ π+ π , we could type: Sqrt[Pi+Sqrt[Pi+Sqrt[Pi]]]. A less confusing way of accomplishing this is to type

Sqrt[Pi]; The semicolon suppresses the output of the intermediate calculations.

Sqrt[Pi + %];

Sqrt[Pi + %]

π+ π+ π

Using the Basic Math Input palette, we can type

1.20 Obtain a 25-decimal approximation of e, the base of the natural logarithm.

SOLUTION

(b) Obtain an approximation accurate to 15 decimal places.

SOLUTION

1.22 Compute 968 (a) exactly and (b) approximately to 25 significant digits.

1.6 Cells

Cells are the building blocks of a Mathematica notebook. Cells are indicated by brackets at the right-hand side of the notebook. (Most likely you have already noticed these brackets and were wondering what they meant.) Cells can contain sub-cells, which may in turn contain sub-sub-cells, and so forth.

The kernel evaluates a notebook on a cell-by-cell basis, so if you have several instructions within a single cell, they will all be executed with a single press of the [ENTER] key.

EXAMPLE 19

All three lines are contained within a single cell. [ENTER] is pressed only once.

3 9 12

A new cell can be formed by moving the mouse until the cursor becomes horizontal, and then clicking.

A horizontal line will appear across the screen to mark the beginning of the new cell. Existing cells can be divided by clicking on the menu Cell⇒Divide Cell. The cell will be divided into two cells, the break occurring at the point where the cursor is positioned. As a shortcut, you can divide a cell by pressing (simultaneously) [SHIFT] + [CTRL] + [D] .

Cells can be combined (merged) by selecting the appropriate cell brackets (a vertical black line should appear) and then clicking on Cell ⇒ Merge Cells. Alternatively, you can press [SHIFT] + [CTRL] + [M] .

To avoid extremely long notebooks, cells can be closed (or compressed) by double-clicking on the cell bracket. The bracket will change appearance, looking something like a fish hook. Double-clicking a second time will open the cell.

There are different types of cells for different purposes. Only input cells can be fed to the kernel for evaluation. Text cells are used for descriptive purposes. Other cell types such as Title, Subtitle, Section, Subsection, etc. can be found by clicking on the menu Format ⇒ Style. The cell type can also be seen and changed using a drop-down box located in a toolbar at the top of your notebook. If you do not see the toolbar, go to Window⇒Show Toolbar to display it.

SOLVED PROBLEMS

1.26 Let a = 2 x + 3 and b = 5 x + 6. Then compute a + b.

(a) Place each instruction in a separate cell and execute them individually.

(b) Place all three instructions in a single cell and execute them simultaneously.

SOLUTION

This is what the output looks like after execution:

(a) a = 2 x + 3

1.27 Let a = 2 x + 3 y + 4 z, b = x + 3 y + 5 z, and c = 3 x + y + z. Compute the sum of a, b, and c. Place four lines within a single cell and execute, printing only the final result.

SOLUTION

a = 2x + 3y + 4z;

b = x + 3y + 5z;

c = 3x + y + z;

a + b + c

⎥⎥

⎥⎥

⎥⎥

]

6 x + 7 y + 10 z

⎥⎥⎥

⎥⎥

⎥⎥

Dans le document Second Edition (Page 22-28)