Mathematica
Tutor
Part 3:
Variables
- Enter
x=3
y=2
- Enter
2*x+y
- The result of the
preceding step follows from the fact that the variable x
has been assigned the value 3 and the variable y
has been assigned the value 2. Check this by
entering
x
y
- Enter the
following line. Note that it ends with a semicolon.
z=5;
Now,
without using a semicolon, enter
z
Entering a
line with a semicolon has the same long-term effect as entering it
without a semicolon, but nothing is displayed afterwards.
- Use pencil and
paper to decide what number xy^{2} + 3z represents,
and then check your answer by entering
x*y^2+3*z
Use your
mouse to return to the expression just entered, replace the 3
by 4, and press SHIFT-RETURN.
- Suppose we want to
remove the identification of x with 3. Enter
x=.
Check this
by entering
x
Note:
You may also remove an identification with the Clear command.
Using this method, you would enter
Clear[x]
Now remove the identifications of y with 2 and z
with 5. Check by entering
x*y*z
You should
see
x
y z
as the output.
Note:
Mathematica allows variable names with more than one letter.
This is sometimes the cause of some confusion, because
Mathematica also lets you leave out the * for
multiplication.
- x y with a
space means the same as x*y .
- xy without
a space means a variable with the name
xy .
To avoid confusion, we
will always include the * character to indicate
multiplication.
- Mathematica
distinguishes between = and ==.
The single equals sign is used for definitions. The double equals
sign is used to enter equations. Check this by entering
Solve[s^2 - s - 1 == 0, s]
(Here the s
after the comma in the command tells Mathematica what to
solve for.)
- Enter
2+3
and then
% + 5
The percent sign has the value of the last quantity
calculated. Check this by entering
x*y
and then
% + z