Mathematica/SimpleCalculations

Simple computations with Mathematica

edit
(1-1/7)*(1+2/3)^2
 
 (25)^(1/2)
 
Factorial[15]
 
15!
 
Sqrt[81]
 
Sqrt[56.81]
 
%^2
 

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=2.14
 
p - Log[p]
 

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

x = 2*a-p
 
x-Sqrt[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
 
Expand[f]
 
Factor[%]
 
%%
 
Sin[x]^2+Cos[x]^2
 
Simplify[%]
 

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

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]
 

Mathematica can also compute definite integrals:

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


NIntegrate is used for numerical integration:

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

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->Infinity]
 
Limit[(2*t-3)/(3*t+4),t->-4/3,Direction->-1] (*right-bound of the limit*)
 
Sum[i^2,{i,1,10}]
 
Product[1/i,{i,1,10}]
 

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

Solve[2*t+3==-t+6*Sqrt[2]]
 
Solve[t-15/4*u==5/2*(u-t)+3,u]
 
Solve[{a-b==2,a+3*b==7},{a,b}]
 

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]