1 First Calculations: Basic Tutorial (Part 1 of 10)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

This notebook is part of a draft of a set of elementary Mathematica tutorials, as presented at the 1999 Mathematica Developer

Conference, October 20-23, 1999. This draft is provided here to encourage suggestions about these tutorials. You can send
suggestions about these tutorials to Wolfram Research Technical Support at the address support@wolfram.com.

Only the first nine parts of the Basic Tutorial are included in this draft.

Basic Tutorial (part 1 of 10)

1 First Calculations
This is the first part of the ten part Basic Tutorial for learning to use Mathematica. This tutorial is designed to provide broad
experience with Mathematica and to provide a foundation for exploring advanced features of the system.

No prior experience with Mathematica is assumed or required. The only prerequisites for this tutorial are familiarity with
common operations in elementary mathematics and familiarity with the use of menus, dialog boxes, and other general features
of modern computer software.

This tutorial includes a large number of examples and exercises. It is designed to be used in conjunction with a copy of
Version 4 of Mathematica so that you can experiment with the examples and work out solutions to the exercises. If you are not
currently working with a copy of Mathematica then many of the comments and examples will not apply to you.

In this first part of the Basic Tutorial you will learn about:

è How to use these tutorials


è Common conventions in Mathematica input
è Performing simple calculations
è Assignments and definitions
è Finding information in online help

Getting Started
To use this tutorial you should know how to start Mathematica, how to switch from one window to another, how to scroll up
and down in a window using the keyboard or a mouse, and how to select an item from a menu.

If you have had difficulties getting started with Mathematica, or if you are interested in a brief introductory tour of the system,
we recommend the Getting Started guide or the Tour of Mathematica, both of which can be found using the Help Browser,
which is available under the Help menu.

Entering and Evaluating Input


This section describes how to perform a simple calculation, such as adding 2 plus 2, using Mathematica. If you already know
how to do this then you may wish to skip ahead to the next section, Syntax Conventions. Otherwise, the remainder of this
section describes the basic steps in performing a simple calculation.

When you start Mathematica you will be presented with an empty window similar to the window shown here:
2 Basic1.nb

You can open a new window in your copy of Mathematica by selecting New from the File menu. The appearance of this
window will be different in different versions of Mathematica and on different types of computers, but it is normally an empty
window with a title bar across the top, a scroll bar across the bottom, and a scroll bar on one side.

To enter your input, select the new window by clicking on it, and start typing. Your input will appear near the top of the
window, much as in a typical word processor. Unlike most word processors, however, you will not see a blinking input cursor
until you start typing.

After entering your input, the window will look something like this:

To evaluate your input, press ˜ÎÁÏ (press the return key while holding down the shift key). When the calculation is
finished, the result is displayed just below your input, as in:

You can repeat this process indefinitely with additional inputs. For example, after a second input and a second evaluation,
your notebook might look something like this:

When you are finished with your calculations you can end your Mathematica session (which will close this window) by
selecting Quit from the File menu. Do not select Quit now unless you want to end this tutorial session.

Syntax Conventions
For a thorough discussion of Mathematica syntax, and for definitive reference information, see the Mathematica book or
online help, especially Sections 1.1.6, 2.1.3, 2.8.14, A.2, and A.3. The summary presented here is adequate for all of the
inputs in this tutorial and for most work with Mathematica.

Calculations are done in Mathematica by evaluating functions. For example, you can use the Plus function to compute the sum
of 2 and 3:

Plus@2, 3D

This calculation is rarely entered this way. This calculation is normally be entered using the more familiar notation 2 + 3.
Basic1.nb 3

2+3

In both of these examples the function that is used is the Plus function, and 2 and 3 are the arguments of the function. 2 + 3 is
just another way of entering Plus@2, 3D.

More complicated inputs can be entered using functions as arguments in other functions. For example, the product of 7 and
2 + 3 can be entered using the Times function.

Times@7, Plus@2, 3DD

35

This calculation can also be entered as 7 H2 + 3L. Parentheses are used here for grouping. Entering 7 and H2 + 3L adjacent
to one another indicates that these expressions are to be multiplied.
7 H2 + 3L

35

In this example, the arguments of the Times function are 7 and 2 + 3.

The most general way to enter Mathematica functions is by giving the name of the function followed by square brackets that
enclose the argument or arguments of the function. This notation is called FullForm notation, since the FullForm function can
be used to display expressions in this form. Plus@2, 3D and Times@7, Plus@2, 3DD are examples of FullForm
notation.

Alternatives to FullForm notation are available for many common inputs. For example, 2 + 3 is an alternative to the FullForm
input Plus@2, 3D, and 7 H2 + 3L is an alternative to the FullForm input Times@7, Plus@2, 3DD. Other arithmetic
operations can be entered using standard notations from mathematics and computer programming. For example, 3 − 2
denotes 3 minus 2, 3 ê 2 denotes 3 divided by 2, and 3 ^ 2 denotes 3 to the power 2. Multiplication can be indicated either
by adjacent expressions, as in 7 H2 + 3L, by one or more spaces between expressions, or with an asterisk, as in 7 ∗ H2 + 3L.

Adjacent expressions can be used to indicate multiplication only if the factors to be multiplied are distinct expressions even
when there is nothing between them. For example, 7 multiplied by 5 can be entered either as 7 × 5, with a space between the
7 and the 5, or as 7*5, but 75, with no space between the 7 and the 5, is the integer 75, not 7 multiplied by 5. This is also
true if the factors are symbolic variables. For example, while x y indicates x multiplied by y, the input xy, with no space
between the x and the y, is a new symbolic variable. In some situations it is useful to include an asterisk so that the intended
meaning is clear, even when the asterisk is not necessary.

These notations for arithmetic are also listed in Section 1.1.1 of The Mathematica Book. A complete list of all special nota-
tions in Mathematica can be found in Section A.2. You do not need to know all of these notations to use Mathematica. Most
of the more common notations, such as the notations for arithmetic, are probably familiar, and any other notations that you
might need can be learned when you learn about the corresponding functions.

Some Typical Inputs


In most cases the most convenient way to enter Mathematica inputs will involve a mixture of FullForm notation and other
notations. For example, here is an input that illustrates the most common syntax for using the Solve function to find the
solution of an equation.
Solve@3 x + 5  14, xD

88x → 3<<
4 Basic1.nb

Since there is no short notation for Solve, the Solve function is entered in FullForm notation. The name Solve is followed by
square brackets that enclose the arguments of the function. The first argument is the equation 3 x + 5  14, entered here
using special notation for the Equal function. The Equal function is the function that is used in Mathematica for representing
equations. The  character is entered as two = signs. The second argument, x, specifies the variable for which to solve.

The result 88x → 3<< illustrates two other special notations. The curly braces { and } are used to represent List expressions,
and the arrow → is used to represent a Rule expression. The same result in FullForm notation is
List@List@Rule@x, 3DDD. The use of this type of result will be discussed later in this tutorial.

The notations that have been introduced so far can be used to enter any system of algebraic equations. Here is an example
showing the use of Solve to solve a pair of equations in two variables. The pair of equations is entered using a List expression
in the first argument of Solve, and the two variables are specified using the List expression 8x, y< in the second argument
of Solve. The result is a List expression containing the two solutions. Each solution is a list of Rule expressions that give the
values of x and y for that solution.
Solve@8x ^ 2 − 3 y  3, 2 x − y  1<, 8x, y<D

88y → − 1, x → 0<, 8y → 11, x → 6<<

Here is an example showing the use of the Plot function to plot a sine curve. The first argument Sin@xD shows the Mathemat-
ica notation for the sine of x. The second argument 8x, 0, 2 Pi< is a List expression that gives the name and the minimum
and maximum values of the variable. In FullForm notation this input would be
Plot@Sin@xD, List@x, 0, Times@2, PiDDD. There isn't an alternate notation for Sin@xD or for the Plot function,
but this input is made significantly more readable and compact by using the alternate notations for List and for Times.
Plot@Sin@xD, 8x, 0, 2 Pi<D

0.5

1 2 3 4 5 6

-0.5

-1

 Graphics 

You may want to take some time to experiment with these examples to see the effect of simple changes. For example, you
could try typing your own equation in to the Solve example, or inserting Cos@xD or some other function in place of Sin@xD
in the Plot example. For more information about these functions, see the documentation for Solve and Plot in The Mathemat-
ica Book.

Assignments and Definitions


A single = sign, as in c = 5, is used for entering assignments. The notation c = 5 is a special notation for Set[c,5]. Evalua-
tion of c = 5 will assign 5 as the value of c, after which c will be replaced by 5 whenever c is evaluated. For example:

c=5

5
Basic1.nb 5

b+cx

b+5x

You can remove the assigned value of c by evaluating Clear[c], after which c will remain symbolic during evaluation. It is
generally a good idea to clear assignments that are no longer in use so that the assigned values do not interfere with subse-
quent calculations.
Clear@cD

b+cx

b+cx

Assignments can be used for defining constants, for keeping track of intermediate results, and for defining functions.

Here is an example in which assignments to eq1 and eq2 are used to build up the equations that are passed to Solve. This
way of using assignments to keep track of intermediate results can be invaluable in large calculations.
eq1 = x ^ 2 − 3 y  3

x2 − 3 y == 3

eq2 = 2 x − y  1

2 x − y == 1

Solve@8eq1, eq2<, 8x, y<D

88y → − 1, x → 0<, 8y → 11, x → 6<<

Here is an example of a definition for a function f.

f@p_D = p ^ 2

p2

The notation f[p_] on the left side of the definition is used to indicate that this definition should be invoked whenever an
expression of the form f[expr] is evaluated, where expr is any expression. For example:

f@9D

81

Function definitions are used extensively in Mathematica programming, and will be discussed again in Part 7 and in Part 8 of
this tutorial. For now it is necessary only to recognize the use of simple assignments to define constants and to keep track of
intermediate results.

For other examples of the use of assignments, see Sections 1.2.2 and 1.7.1 in The Mathematica Book.

Before continuing, evaluate Clear@eq1, eq2, fD to clear the definitions that were introduced in this section.

Clear@eq1, eq2, fD

Names and Symbols


Names such as Plus and Solve are names of symbols. Symbols are used in Mathematica both as variables and as functions,
and play an essential role in all Mathematica calculations.
6 Basic1.nb

The name of a symbol can include any combination of letters, numbers, or other characters, except that the first character
cannot be a number, and the name cannot include operators or other characters that already have some other meaning. For
example, the name of a symbol cannot include a plus sign or a minus sign, since those characters are used for arithmetic.
Section A.1.2 of the Mathematica book discusses valid symbol names and typical names for symbols.

Some examples of valid names for symbols are x, y, x3, yf17s, alpha, α, gβ12, and SpeedOfLight. Some examples
of invalid names for symbols are 3x, alpha_0, and file.ext. The input 3x is an invalid name because the first character
is a number. 3x will be interpreted as 3 multiplied by x, even if there is no space between the 3 and the x. The inputs
alpha_0 and file.ext are invalid names because they include characters that have other meanings. The underscore
character in alpha_0 is used in Mathematica to indicate a pattern expression, and the period (or decimal point) in file-
.ext indicates a dot product.

Mathematica input is case-sensitive, so capitalization is important. If you make an error in the spelling or capitalization of the
name of a symbol, the misspelled name will in most cases be undefined, and Mathematica will return the input unchanged.

For example, if SOLVE is used instead of Solve, the following input will normally return unchanged.

SOLVE@3 x + 5  14, xD

SOLVE@5 + 3 x == 14, xD

An error in spelling or capitalization of the name of a function is the most common reason for Mathematica returning an input
unchanged. The input above using SOLVE rather than Solve is valid Mathematica syntax, and could be useful if you had, for
example, defined your own function and used SOLVE as the name of that function. In most cases, however, this input is
probably an error.

Square Brackets, Curly Braces, and Parentheses


The use of square brackets [ and ], curly braces { and }, and parentheses in Mathematica is discussed in Sections 1.2.5 and
A.2.6 in The Mathematica Book.

Parentheses are used in Mathematica for grouping in arithmetic, square brackets are used for enclosing the arguments of
functions, and curly braces are used for entering List expressions (as in 8x, y, z< for List@x, y, zD).

One other common type of bracketing is the use of double square brackets for entering the Part function. The Part function is
used to pick out parts of expressions, such as elements in a vector or matrix. For example, 8x, y, z<@@2DD is an alternate
notation for Part@List@x, y, zD, 2D, which gives the element at position 2 in the list 8x, y, z<. For example:

Part@List@x, y, zD, 2D

8x, y, z<@@2DD

Parentheses, square brackets, and curly braces are used in a variety of ways in traditional mathematics and in other computer
languages. An occasional source of error in Mathematica is using brackets, braces, or parentheses as they might be used
somewhere else.

For example, curly braces and square brackets are sometimes used in traditional mathematics for grouping arithmetic expres-
sions, and curly braces are often used in computer programming languages for identifying blocks of statements. Curly braces
and square brackets are not used in these ways in Mathematica. For example, an input such as 7@2 + 3D , which might
represent 7 multiplied by 2 + 3 in traditional mathematical notation, would be very unusual in Mathematica, since the 7
would be interpreted as the name of a function. Similarly, 7 82 + 3< in Mathematica represents 7 multiplied by the list
82 + 3<, which is not the same as multiplying 7 by 2 + 3.
Basic1.nb 7

Probably the most prominent departure from traditional mathematical notation in Mathematica is the use of square brackets
rather than parentheses to enclose the arguments of a function. Using square brackets to enclose function arguments avoids the
ambiguity in traditional mathematical notation between parentheses that enclose function arguments and parentheses that are
used for grouping arithmetic operations. For example, in traditional mathematical notation, y Hx + 1L sometimes indicates y
multiplied by x + 1 and sometimes indicates a function y evaluated at x + 1. In default Mathematica notation, y Hx + 1L
always indicates y multiplied by x + 1 and y@x + 1D always indicates a function y evaluated at x + 1.

For further discussion of these and other aspects of Mathematica syntax, see Section 1.1.6 in The Mathematica Book.

Getting Information about Special Notations


If you know the name of a function you can find special notations, if any, for that function by finding the function in The
Mathematica Book. The special notations that have been mentioned so far in this tutorial include notations for Times, Plus,
Power, Equal, List, and Rule.

If you know the operator or character that is used in a special notation you can usually find the corresponding function by
entering that operator or character into the help browser. For example, if you enter −> (a dash followed by a "greater than"
character) or the → character in the Help Browser and click the Go To: button, the Help Browser will display documentation
for Rule.

Many notations involve special characters that are not found on a typical computer keyboard. For example, multiplication can
be indicated by a × character, as in 7 × 3, and division can be indicated by the ÷ character, as in 7 ÷ 3. The × character can
be entered using Â*Â (the escape key followed by the * key followed by the escape key) or by typing out î[Times]. The
÷ character can be entered using Âdiv or î[Divide].

All special characters have a full name, such as î[Times] or î[Divide]. Special characters can always be entered by
typing out that name. Many characters can also be entered using an alias, such as Â* for the × character or Âdiv for
the ÷ character. Some characters have several aliases.

For more information on special characters see Sections 3.10 and A.1.2 in The Mathematica Book. In the documentation,
aliases such as Âdiv are typically given using the notation div.

Special characters are not required in Mathematica input. For simplicity you may want to bypass them until you are more
familiar with basic Mathematica operations. Special characters are, however, sometimes used in output, even if you do not
use them in your input. If you see a special character in the display, you can get information about the character by selecting
the character using the mouse and choosing Find Selected Function... from the Help menu.

Looking Ahead
Here is an outline of the parts of this tutorial, and the topic to be covered in each part.

1 First calculations
2 Numerical calculations
3 Symbolic calculations
4 Plotting and graphics
5 Using notebooks
6 Typeset editing
7 Manipulating expressions
8 Programming
9 Working with files
10 Summary and exercises
8 Basic1.nb

Parts 2, 3, and 4 describe the use of standard Mathematica functions for mathematical calculations

Parts 4 and 5 describe the user interface of Mathematica, and facilities for formatting your output.

Parts 7 and 8 provide an introduction to Mathematica programming.

Part 9 describes operations such as reading data files in to Mathematica and saving results in external files.

Part 10 consists primarily of exercises designed to test your knowledge of the information in this tutorial.

The parts of this tutorial can be read in any order. The order shown in the outline is designed to provide a broad introduction to
the entire system. If you are interested in a specific part of Mathematica, you may want to skip ahead to the corresponding part
of this tutorial. Yet another approach is to start with the exercises at the end of each part of this tutorial and work back from
that point to learn about the solutions.

You should not skip the exercises, however. Much of the essential information in these tutorials is covered in the exercises.
Even if you do not work out solutions, you should still take the time to read through the exercises.

Getting Help
If you would like assistance with this tutorial, experts are available to review your solutions to the exercises or to answer any
questions that come up as you work through this tutorial. You can get information about assistance with this tutorial by
sending an email request for information about tutorial assistance to Wolfram Research Technical Support
(support@wolfram.com).

If you are a registered user of a supported version of Mathematica, you can get help with technical questions about Mathemat-
ica by sending your questions to Wolfram Research Technical Support at the email address support@wolfram.com, or using
contact information included with your copy of Mathematica.

Exercises
Note: If you are intending to send in your solutions to the exercise for review, we recommending opening a new notebook and
putting your work in the new notebook, rather than editing this tutorial notebook. You can also include any questions that you
have in the new notebook.

ü Exercise 1.1 Solving an equation

Show how to use the Solve function to get the formula for the solutions of the quadratic equation a x ^ 2 + b x + c  0 for
the variable x. The result should look like:

−b − b2 − 4 a c −b + b2 − 4 a c
::x → >, :x → >>
2a 2a

ü Exercise 1.2 Alternate notations for input

Show a shorter way to enter this input:

List@Plus@Times@2, 3D, Times@4, 5DD, Times@Plus@2, 3D, Plus@4, 5DDD

826, 45<
Basic1.nb 9

ü Exercise 1.3 Using rules

Rules, such as the Rule expressions in the result from Solve, can be used with the ReplaceAll function to make replacements
in other expressions. For example:
ReplaceAll@8x, 5, x, 7<, x → 2D

82, 5, 2, 7<

Show how you might use ReplaceAll to replace x by 2 and y by 3 in the list 8x, y<. If you wish, this input can be entered
using the alternate notation for ReplaceAll, which you can find by looking up ReplaceAll in The Mathematica Book.

ü Exercise 1.4 Making a table

A common exercise in programming textbooks for beginners is constructing a table of integers and squares of integers. There
are many ways to do this in Mathematica. Here is one way, using the Do function, the Print function, and a string of spaces to
separate the columns in the table.
Do@Print@i, " ", i ^ 2D, 8i, 1, 10<D

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

Modify this program to add a column that shows the cube of each integer.

ü Exercise 1.5 Formatting a table

One problem with the table in Exercise 1.4 is that the columns are not properly aligned. An effective solution to that problem
is to construct the entire table and let the computer align the columns. This can be done in Mathematica using the Table
function to generate a list of integers and squares of integers, and using the TableForm function to format that list.
TableForm@Table@8i, i ^ 2<, 8i, 1, 10<DD

1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

Modify this program to add a column that shows the cube of each integer.
10 Basic1.nb

Exercise 1.6 Online information

In addition to The Mathematica Book and online help (the Help Browser), you can also get information about functions and
variables by typing a question mark followed by the name of the function or variable. For example, evaluating ? Solve gives
information about the Solve function:
? Solve

Solve@eqns, varsD attempts to solve an equation or set of


equations for the variables vars. Solve@eqns, vars, elimsD attempts
to solve the equations for vars, eliminating the variables elims.

You can also include an asterisk after the question mark to indicate any sequence of characters. For example, ? *Plot* will
show all names that contain Plot.

Enter ? *Plot* to get the names of all of the functions that include Plot in the name of the function.

Now enter ? Plot and read the information about the Plot function. This information will be useful in the next exercise.

ü Exercise 1.7 Plotting more than one function at a time

Use the Plot function to plot Cos@xD and Sin@xD on the same plot, with x ranging from 0 to 2 Pi.

If you are unsure of how to proceed, you can get information about using Plot to plot more than one function by entering and
evaluating ? Plot, or by referring to the documentation for Plot or to Section 1.9.1 in The Mathematica Book.

You can find explicit examples of using Plot to plot more than one function both in Section 1.9.1 of The Mathematica Book
and under Further Examples at the bottom of the help page for Plot. To open the Further Examples section of the help
page, click on the triangle immediately to the left of the heading Further Examples.

ü Exercise 1.8 Adding a label to a plot

One way to add a label to a plot is to use the PlotLabel option. An option is an optional function argument that is entered using
a rule. The Plot function is one of the many functions that accepts options.

Add a label to a plot of your choice by including PlotLabel → "This is a plot" as the last argument in Plot.

Examples showing the use of the PlotLabel option can be found under Further Examples at the bottom of the online help
page for PlotLabel.

Other ways of labeling plots will be discussed in Part 4 of this tutorial.

ü Exercise 1.9 Solving an equation numerically

Use the FindRoot function to find the three real solutions of the equation Sin@xD  1 − x ê 4.

Here is an input that computes the smallest solution. The first argument in FindRoot is the equation to be solved and the
second argument gives the variable and the starting value for that variable. The algorithm used by FindRoot begins with the
specified starting value when it searches for a solution.
FindRoot@Sin@xD  1 − x ê 4, 8x, 1<D

8x → 0.890487<

The approximate locations of the other two solutions can be seen by plotting the two sides of the equation on the same graph,
and noting the points where the two lines intersect.
Basic1.nb 11

Plot@8Sin@xD, 1 − x ê 4<, 8x, − 1, 8<D

0.5

2 4 6 8

-0.5

-1

 Graphics 

By choosing a starting value near the solution that you want, you can cause FindRoot to compute a value for that solution.

ü Exercise 1.10 Contacting Wolfram Research

Send an electronic mail message to Wolfram Research Technical Support, and let them know that you are working through
this tutorial. Within one business day you can expect a reply providing any updates about these tutorials and information about
further assistance. The electronic mail address for Wolfram Research Technical Support is support@wolfram.com.

You might also like