Here is a list of things you need to know about Mathematica to avoid
frustration!
- Mathematica has very large inbuilt help pages. To access these,
just click on the Help menu item at the top of the screen.
- Mathematica's inbuilt functions, (like Exp, Sin etc) are always
capitalised. For this reason, it is always safest to use lower case
names for your own variable and function names.
- All user defined variables and functions must begin with a letter,
and must consists only of letters and numbers. So, e.g. underscores
are not allowed!!
- You can define your own function (i.e. a user-defined function!)
using the following syntax:
f[x_]:=x^2
Note the following:
- you must have square brackets [ ] around the dummy argument of the function
- you must have an underscore next to the dummy argument of the function
- you must have a := to define the function. (The usual equals sign
= won't work!)
You can define more complicated function as follows:
g[x_,y_]:= 6 Exp[x^3] Sin[y]
- To raise any function (in-built or user-defined) to a power, you use e.g.
Sin[x]^2 and not Sin^2[x]
- Important numbers are pre-defined in Mathematica:
Number |
Mathematica symbol |
|
I |
|
Pi |
e |
E |
|
Infinity |
Note that Mathematica can certainly handle complex arithmetic!
e.g.(5 + 4I)/(2 - 7I) or Exp[I Pi] etc etc.
- To give a short description of a function or a variable (whether it
be a Mathematica inbuilt one, or a user-defined one), just type
?function or ?variable. e.g. ?Sin gives you a short
description of the sin function.
- To clear the definition of a user-defined function or variable, just type
Clear[name]. e.g. if you have defined a=2, then Clear[a]
will wipe this definition.
It is always a good idea to Clear variables before you use
them, in case you've already defined them earlier in your Mathematica session.
If you want to Clear all variables, you can use:
Clear["`*"]
Note that the symbol just before * is the left-hand apostrophe.
- To avoid Mathematica printing to the screen after every time you hit
Shift-Return, just add a semi-colon. i.e. a=2; will not
echo 2 when you hit Shift-Return. You can use a semi-colon to
put many Mathematica commands on the same line, e.g. if you type:
3*8; a=2; 7a; Sin[Pi/2]; Exp[2]; Exp[5.]
then Mathematica will only display 148.413 on the screen (once you hit
Shift-Return) i.e. 148.413 = Exp[5.].
- You can add "comments" to your Mathematica content by typing in some
text, and NOT typing control-enter ...!
- The multiplication sign is not always needed. e.g. the following
are all equivalent:
a b
a*b
i.e. Mathematica understands a space (as in the first example)
as meaning multiplication as well as a * (as in the second example).
Note that you must be careful because if you leave the space out, as in
ab Mathematica understands this to be a new variable ab
rather than a times b. Also note that when one of the factors is
a number, or a bracketted expression, then no space is needed,
e.g. 7a needs no space between the 7 and the a
and 7a(b+c) requires no spaces around the a.
- There are four different types of brackets in Mathematica.
It is crucial that you understand when each are used.
Type |
Example Usage |
Description |
( ) |
7a(b+c) |
This has the same meaning as brackets in an ordinary |
|
|
mathematical expression. i.e. it just groups together items |
[ ] |
Sin[Pi/2] |
This is always required for all functions |
{ } |
Integrate[f[x],{x,0,3} ] |
This defines a list |
[[ ]] |
vector[[2]] |
This gives the access to a component of a list or array - see later. |
- There are four different types of equal signs in Mathematica.
It is crucial that you understand when each are used.
Type |
Example Usage |
Description |
= |
a=2 |
Assigns a value to the variable name |
:= |
f[x_]:=x^2 |
Used only in the definition of user-defined functions |
== |
D[g[x,y],y] == D[h[x,y],x] |
Used in equations and in relational expressions, |
|
|
i.e. one whose answer is generally True or False. |
-> |
Solve[3x==6,x] |
|
|
{ x->2 } |
This is how Mathematica displays the solution of an equation. |
- The percentage key % can be used as a short-hand for the last
command. e.g.
a=2;
%^3
(with a Shift-Return at the end of both lines) will return 8.
- If you want Mathematica to stop performing a calculation (e.g. maybe
you've made a mistake, and the calculation is taking too long to finish),
then you press Alt-comma (i.e. the Alt and the , key
at the same time!).