Anda di halaman 1dari 10

Basic Tutorial (part1of 8)

1 First Calculations
This is the first part of the eight 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 conjunc-
tion with a copy of Mathematica so that you can experiment with the examples and work out solu-
tions 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 intro-
ductory tour of the system, we recommend the Getting Started guide from the Documentation
Center.

Entering and Evaluating Input


This section describes how to perform a simple calculation, such as adding 2 plus 2, using Mathe-
matica. 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 perform-
ing 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
Basic1.nb 3

Syntax Conventions
For a thorough discussion of Mathematica syntax, and for definitive reference information, see the
Documentation Center, The Mathematica Virtual Book or online help. 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.
2+3

In both of these examples the function that is used is the Plus function, and 2 and 3 are the argu-
ments 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 exam-
ple, the product of 7 and 2 + 3 can be entered using the Times function.
Times@7, Plus@2, 3DD

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

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 here in the Documentation Center. A complete list of
all special notations in Mathematica can be found under the Mathematical and other Notation
tutorial. 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 nota-
tions that you might need can be learned when you learn about the corresponding functions.

Some Typical Inputs


4 Basic1.nb

Some Typical Inputs


In most cases the most convenient way to enter Mathematica inputs will involve a mixture of Full-
Form 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

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 equa-
tions. 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

Here is an example showing the use of the Plot function to plot a sine curve. The first argument
Sin@xD shows the Mathematica 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

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.

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]. Evaluation 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

b+cx

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 subsequent calculations.
Basic1.nb 5

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 subsequent calculations.
Clear@cD

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

eq2 = 2 x - y ã 1

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

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


f@p_D = p ^ 2

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

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 Assignments
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.
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 charac-
ters 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. The Documentation
Center has further information regarding valid symbol names and typical names for symbols.
Some examples of valid names for symbols are x, y, x3, yf17s, alpha, a, gb12, and SpeedOf-
Light. 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 charac-
ter 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 unde-
fined, and Mathematica will return the input unchanged.
6 Basic1.nb

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 unde-
fined, 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

An error in spelling or capitalization of the name of a function is the most common reason for Mathe-
matica 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 here.
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 mathemat-
ics and in other computer languages. An occasional source of error in Mathematica is using brack-
ets, 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 expressions, and curly braces are often used in computer programming lan-
guages 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.
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 some-
times 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 the corresponding
guide in the Documentation Center.

Getting Information about Special Notations


Basic1.nb 7

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 input
alias, such as Â* for the µ character or Âdiv for the ¸ character. Some characters have
several aliases.
For more information on special characters see the relevant section in the documentation center.
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, how-
ever, 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 pressing F1 from the keyboard.

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
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.
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 Mathemat-
ica, 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
8 Basic1.nb

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 Mathematica 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

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

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.

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

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

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

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

Exercise 1.6 Online information


In addition to the Documentation Center and online help, you can also get information about func-
tions 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

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.
You can find explicit examples of using Plot to plot more than one function under Basic Examples
in the help page for Plot. To open the Basic Examples section of the help page, click on the
triangle immediately to the left of the heading Basic 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 in the Documentation Center.
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.
Here is an input that computes the smallest solution. The first argument in FindRoot is the equation
10 Basic1.nb
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

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.
Plot@8Sin@xD, 1 - x ê 4<, 8x, - 1, 8<D

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

Anda mungkin juga menyukai