# Mathematica/SimpleCalculations

### Simple computations with Mathematica

(1-1/7)*(1+2/3)^2

${\displaystyle {\frac {50}{21}}}$
 (25)^(1/2)

${\displaystyle 5\,}$
Factorial[15]

${\displaystyle 1307674368000\,}$
15!

${\displaystyle 1307674368000\,}$
Sqrt[81]

${\displaystyle 9\,}$
Sqrt[56.81]

${\displaystyle 7.537240874\,}$
%^2

${\displaystyle 56.81\,}$

### Using variables with Mathematica

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 ${\displaystyle p-\ln \left(p\right)}$  so that p is evaluated (replaced by) as 2.14:

p=2.14

${\displaystyle 2.14\,}$
p - Log[p]

${\displaystyle 1.379194171\,}$

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

x = 2*a-p

${\displaystyle 2\,a-2.14}$
x-Sqrt[a*p]

${\displaystyle 2\,a-2.14-1.462873884\,{\sqrt {a}}}$
expr = a*x^2 + b*x + c == 0

${\displaystyle {a{x}}^{2}+bx+c==0\,}$

### Commands to manipulate expressions in Mathematica

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

f = (a+b)^6

${\displaystyle f:=\left(a+b\right)^{6}}$
Expand[f]

${\displaystyle {a}^{6}+6\,{a}^{5}b+15\,{a}^{4}{b}^{2}+20\,{a}^{3}{b}^{3}+15\,{a}^{2}{b}^{4}+6\,a{b}^{5}+{b}^{6}}$
Factor[%]

${\displaystyle \left(a+b\right)^{6}}$
%%

${\displaystyle {a}^{6}+6\,{a}^{5}b+15\,{a}^{4}{b}^{2}+20\,{a}^{3}{b}^{3}+15\,{a}^{2}{b}^{4}+6\,a{b}^{5}+{b}^{6}}$
Sin[x]^2+Cos[x]^2

${\displaystyle \left(\sin(x)\right)^{2}+\left(\cos(x)\right)^{2}}$
Simplify[%]

${\displaystyle 1\,}$

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

${\displaystyle 7\,}$

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

N[Sqrt[3]]

${\displaystyle 1.732050808\,}$
N[Sqrt[3], 50]

${\displaystyle 1.7320508075688772935274463415058723669428052538104\,}$

### Defining a function with Mathematica

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

f[t_]:=Sin[t] - t

f[2*x + 2]

${\displaystyle -2x+\sin(2x+2)-2}$

### Calculus with Mathematica

Derivatives of expressions can be taken with the D command:

D[f[t],t]


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

D[g[u,v,w],u,v]


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

Integrate[1/(1+x^3),x]

${\displaystyle {\frac {\tan ^{-1}\left({\frac {2x-1}{\sqrt {3}}}\right)}{\sqrt {3}}}+{\frac {1}{3}}\log(x+1)-{\frac {1}{6}}\log \left(x^{2}-x+1\right)}$

Mathematica can also compute definite integrals:

Integrate[f[t],{t,0,Pi}]

${\displaystyle 2-{\frac {1}{2}}\pi ^{2}}$

NIntegrate is used for numerical integration:

NIntegrate[f[t],{t,0,Pi}]

${\displaystyle 1.57079633\,}$

### Limits, sums and products with Mathematica

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->Infinity]

${\displaystyle {\frac {2}{3}}}$
Limit[(2*t-3)/(3*t+4),t->-4/3,Direction->-1] (*right-bound of the limit*)

${\displaystyle -\infty }$
Sum[i^2,{i,1,10}]

${\displaystyle 385\,}$
Product[1/i,{i,1,10}]

${\displaystyle ={\frac {1}{3628800}}}$

### Solving equations with Mathematica

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,...}]

Solve[2*t+3==-t+6*Sqrt[2]]

${\displaystyle \left\{\left\{t\to -1+2{\sqrt {2}}\right\}\right\}}$
Solve[t-15/4*u==5/2*(u-t)+3,u]

${\displaystyle \left\{\left\{u\to {\frac {2}{25}}(7t-6)\right\}\right\}}$
Solve[{a-b==2,a+3*b==7},{a,b}]

${\displaystyle \left\{\left\{a\to {\frac {13}{4}},b\to {\frac {5}{4}}\right\}\right\}}$

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 ${\displaystyle \mathrm {Li} _{s}\left(z\right)}$ .

Symbolic sums can be calculated. For example:

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