Simple computations with Mathematica Edit


Using variables with Mathematica Edit

Like any other programming language, Mathematica allows us to assign values to named variables. This often makes programs more readable and it allows us to save results for use later in the calculation. Here, 2.14 is assigned to the variable p. We can then compute   so that p is evaluated (replaced by) as 2.14:

p - Log[p]

You can also use more than one variable in a single expression:

x = 2*a-p
expr = a*x^2 + b*x + c == 0

Commands to manipulate expressions in Mathematica Edit

Mathematica has a wide range of commands to help you manipulate expressions:

f = (a+b)^6

Here, the % symbol recalls the last computation made by Mathematica. %% means you recalls the computation made two steps back. %%% will recall the answer to the computation make three steps back. If you wish to recall answers father back, you will have to have assigned them to a variable.

You can also substitute values for the variables in an expression with the command ReplaceAll usually entered /.

x^2+x+1 /.x->2

The command N calculates a floating point (numeric) approximation of an expression. You may also specify the desired precision.

N[Sqrt[3], 50]

Defining a function with Mathematica Edit

You define a function using patterns (_) and SetDelayed ( :=)

f[t_]:=Sin[t] - t
f[2*x + 2]

Calculus with Mathematica Edit

Derivatives of expressions can be taken with the D command:


Multiple partial derivatives can be given as a list. To take the derivative of g with respect to u then v:


Mathematica can be used to find a primitive, indefinite integral or anti-derivative of an expression.


Mathematica can also compute definite integrals:


NIntegrate is used for numerical integration:


Limits, sums and products with Mathematica Edit

You can also find numeric and symbolic values of sums, limits and products using respectively the functions sum, limit and product:

Limit[(2*t-3)/(3*t+4),t->-4/3,Direction->-1] (*right-bound of the limit*)

Solving equations with Mathematica Edit

The Solve[eqn,x] function tries to find the value of x in eqn. You can also use more variables or more equations using Solve[{eqn1,eqn2,...},{x,y,...}]


The following Mathematica sequence will find the determinant of the 6×6 matrix whose i, j'th entry contains ij.

In[1]:= Det[Array[Times, {6, 6}]]
Out[1]= 0

So the determinant of such a matrix is 0 (i.e. it is singular).

The following numerically calculates the root of the equation ex = x2 + 2, starting at the point x = -1.

In[2]:= FindRoot[Exp[x] == x^2 + 2, {x, -1}]
Out[2]= {x -> 1.3190736768573652}

Mathematica can do integral and differential calculus, in particular evaluation of integrals in terms of special functions. For example:

In[3]:= Integrate[x/Sin[x], x]//OutputForm
Out[3]= x (Log[1 - EI x] - Log[1 + EI x]) + I (PolyLog[2, -EI x] - PolyLog[2, EI x])

Here, E and I are the fundamental constants e and i respectively, and PolyLog[s,z] is the polylogarithm function  .

Symbolic sums can be calculated. For example:

In[4]:= Sum[z^k/k^s, {k, 1, Infinity}]
Out[4]= PolyLog[s, z]