Note firstly the general point that the
system is Case Sensitive: pi and Pi are different
(the latter representing ).
Different sorts of brackets are used for different purposes
() | for grouping | ![]() |
[] | for functions | ![]() ![]() |
{} | for lists | see examples later |
[[]] | for indexing |
![]() |
In the examples to be given below prompts by the computer are shown in italics, things typed by the user in typewriter font and responses from the computer in normal type.
Variables are manipulated as in algebra unless they have been given a particular value:
In[1]:= | y = (1 + x)^2 |
Out[1]= |
![]() |
In[2]:= | x = 2 |
Out[2]= |
![]() |
In[3]:= | y |
Out[3]= |
![]() |
As well as being given a permanent value variables can be given a temporary value
In[4]:= | Clear[x] |
In[5]:= | y = (1 + x)^2 |
Out[5]= |
![]() |
In[6]:= | y/. x -> 2 |
Out[6]= |
![]() |
In[7]:= | y |
Out[7]= |
![]() |
Note the command Clear[x] which makes sure that x does not have a value.
Basic algebra can be done by using the functions Expand[] and Factor[] as follows
In[8]:= | Expand[y] |
Out[8]= |
![]() |
In[9]:= | Factor[%] |
Out[9]= |
![]() |
Note the use of % as shorthand for the result of the last operation. We could also have written Factor[Out[8]] here.
There are several other commands available which do specific manipulations on such expressions, in particular:
Differentiation and integration are carried out using the D[f,x] and Integrate[f,x] operators.
In[10]:= | D[y,x] |
Out[10]= |
![]() |
In[11]:= | Integrate[%,x] |
Out[11]= |
![]() |
In[12]:= | Factor[%] |
Out[12]= |
![]() |
In[13]:= | Integrate[y,x] |
Out[13]= |
![]() |
or, using special functions,
In[14]:= | D[Sin[x],x] |
Out[14]= |
![]() |
In[15]:= | Integrate[%,x] |
Out[15]= |
![]() |
In[16]:= | x = Sin[z] |
Out[16]= |
![]() |
In[17]:= | y |
Out[17]= |
![]() |
In[18]:= | D[y,z] |
Out[18]= |
![]() |
In[19]:= | Integrate[y,z] |
Out[19]= |
![]() |
Higher derivatives are written as
Whereas differentiation always has a well-defined answer, sometimes Mathematica is unable to find an analytical result. In such cases the function N[] can be used to obtain a numerical result.
In[20]:= | Integrate[Sin[Sin[x]], {x,0,1}] |
Out[20]= |
![]() |
In[21]:= | N[%] |
Out[21]= |
![]() |
In[22]:= | N[%%,40] |
Out[22]= |
![]() |
where the last command evaluates the 2nd last (%%) result to 40 significant figures. This function can also be used in other situations such as
In[23]:= | N[Pi, 100] |
Out[23]= |
![]() |
![]() |
Mathematica has a number of built in constants (all beginning with capital letters) such as
In[24]:= | E^(I Pi) |
Out[24]= |
![]() |