Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to computing page for the fourth course APMA0360
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to Mathematica tutorial for the fourth course APMA0360
Return to the main page for the course APMA0330
Return to the main page for the course APMA0340
Return to the main page for the course APMA0360

Preface


Out of thousand Mathematica commands, this section emphasizes the usage of two the most popular---Simplify and Expand.

Simplify and Expand


The simplify command finds the simplest form of an equation.
Simplify[expr,assum] does simplification using assumptions.
Expand[expr,patt] leaves unexpanded any parts of expr that are free of the pattern patt.
ExpandAll[expr] expands out all products and integer powers in ant part of exps.
ExpandAll[expr,patt] avoids expanding parts of expr that do not contain terms matching the pattern patt.

One of Mathematica’s most useful features for new users is algebraic manipulation. The program enables the user to avoid tedious exercises in simplification, expansion, and manipulation of algebraic expressions. For example, rather than spending odious amounts of time using the distributive property, Mathematica allows the user to quickly discover that \( (x-1)(x-7)(x+2)(x-4) = x^4 -10\,x^3 + 15\, x^2 + 50\, x -56. \)

D[Integrate[1/(x^3 + 1), x], x] (* integrate the expression *)
Out[1]= 1/(3 (1 + x)) - (-1 + 2 x)/(6 (1 - x + x^2)) + 2/( 3 (1 + 1/3 (-1 + 2 x)^2))
Simplify[%]
Out[2]= 1/(1 + x^3)
Simplify[(x - 1) (x + 1) (x^2 + 1) + 1]
Out[3]= x^4
Simplify[(E^x - E^-x)/Sinh[x]]
Out[4]= 2
Simplify command can be used on equations, and not just expressions.
Simplify[2 x - 4 y + 6 z - 10 == -8]
Out[5]= x + 3 z == 1 + 2 y
Simplify[x^2 + 7 x + 10== (x-1)^3]
Out[5]= x^3 == 11 + 4 4 + 4 x^2
The assumed features can be extremely useful in practical situations. When working with physical situations, a problem contains a number of assumptions (for example the length of the pipe is a positive, finite number). By including an assumption the user gives Mathematica more leeway in finding a simpler answer.
Simplify[1/Sqrt[x] - Sqrt[1/x], x > 0]
Out[6]= 0
Simplify[Abs[2 x + 1 ] + 2 x + x^2 == 3, 2 x +1 > 0]
Out[7]= x (4+x) == 2
Simplify[Sin[n Pi], Element[n, Integers]]
Out[8]= 0
Simplify[x^2 > 3, x > 2]
Out[9]= True
Assuming[x > 0, Simplify[Sqrt[x^2]]]
Out[10]= x
Assuming[x > 0, Simplify[Sqrt[x^2 y^2], Assumptions -> y < 0]]
Out[11]= -Sqrt[x^2] y
FullSimplify[Gamma[x] Gamma[1 - x]]
Out[12]= \[Pi] Csc[\[Pi] x]
The Simplify command can also be used to substitute one variable for another. This is of enormous usefulness, as seen in the following example:
Simplify[((R^2 - (z- (d/2)^2 )^(-3/2)) + ((R^2 + (z + (d/2))^2 ))^2)^(-3/2)), z== 0]
Out[13]= \( \frac{16}{( d^2 + 4 R^2 )^{3/2}} \)
Simplify[Out[13] , d == R]
Out[14]= \( \frac{16}{5\,\sqrt{5}\, (R^2 )^{3/2}} \)
Example 1: There are a few different ways to evaluate a function at a particular variable in Mathematica. Say we know that \( x = 2y \) in the following expression. We can substitute this in using the simplify command.
Simplify[x^3 + x^6 -3 x^2 y + 12 x^5 y + 3 x y^2 + 60 x^4 y^2 - y^3 + 160 x^3 y^3 + 240 x^2 y^4 + 192 x y^5 + 64 , y = x/2]
Out[15]= \( 64 + \frac{x^3}{8} + 63\, x^6 \)
If you have defined the expression as a function, there is another simple way to evaluate the function at a particular value. We begin by defining the expression as a function.
f[x_ , y_ ] = x^3 + x^6 -3 x^2 y + 12 x^5 y + 3 x y^2 + 60 x^4 y^2 - y^3 + 160 x^3 y^3 + 240 x^2 y^4 + 192 x y^5 + 64
We now evaluate the function at the value \( x = 2,\ y = -7 \) as follows:
f[2, -7 ]
Out[17]= 2 986 713
Mathematica also has a built in command that allows us to perform this evaluation with a bit less trouble:
Out[16] /. {x->2, y->-7}
Out[17]= 2 986 713
   ■
Example: A function f is determined by three dependent variables x, y, and z:
\[ f(x,y,z) = x^2 \cos (x+z) + 2 \, \sin^2 (x+yz) + (xz+y)^3 . \]

Simplify the expression in the following ways:
a) define a new function g(x,y) such that \( z = 2 ; \)
b) define a third function h(x) such that \( y = 1/(x+1)^2 ; \)
c) what is the value of h(x) when \( x = \pi ? \)

f[x_ , y_ , z_ ] = x^2 Cos[x+z] + 2 (Sin[x+yz])^2 + (x z+y)^3
Out[18] /. z->2
Out[19]= (2 x + y)^3 + x^2 Cos [2+x] + 2 Sin[x + 2 y]^2
Out[19] /. y -> (x+1)^-2
Out[20]= (2 x + 1/ (x+1)^2)^3 + x^2 Cos [2+x] + 2 Sin[x + 2/(x+1)^2]^2
Out[20] /. x -> Pi
Out[21]= (2 Pi + y)^3 + Pi^2 Cos [2+Pi] + 2 Sin[Pi + 2 /(1+Pi)^2]^2
N[Out[21] ]
Out[22]= 255.046 + 9.8696 Cos [5.14159] + 2 Sin[Pi + 2 /(1+Pi)^2]^2

   ■

 

The Mathematica simplify command can also be used to check the veracity of a mathematical statement. For example, if we want to confirm that x=6 is a solution to the equation \( x^3-3x^2-16x=12 \) the following Mathematica command accomplishes this.

Simplify[x^3 - 3 x^2 - 16 x == 12, x == 6]
Out[15]= True

The Expand command expands a function or an equation into a given power series. The expand command does not work across both sides of an equation, whereas the simplify command does.

Expand[(1 + x + y) (2 - x)^3]
Out[16]= 8 - 4 x - 6 x^2 + 5 x^3 - x^4 + 8 y - 12 x y + 6 x^2 y - x^3 y
ExpandAll[Sqrt[(1 + x)^2]]
Out[17]= Sqrt[1 + 2 x + x^2]
Expand[Sin[x + y], Trig -> True]
Out[18]= Cos[y] Sin[x] + Cos[x] Sin[y]
We expand the power expression \( (1+x)^{10} \) into a regular polynomial using the following command:
Expand[(1 + x)^10]
Out[19]= 11 + 10 x + 45 x^2 + 120 x^3 + 210 x^4 + 252 x^5 + 210 x^6 + 120 x^7 + 45 x^8 + 10 x^9 + x^10

However, if you want to see only odd coefficients of this expansion, use the following option:
Expand[(1 + x)^10, Modulus -> 2]
Out[20]= 1 + x^2 + x^8 + x^10

The Expand command can conveniently be used to only expand certain parts of an expression. For example, in the following expression, suppose we don’t want to break up the polynomials. We only expand the (1-sin (x)) portion.

Expand[(1 - Sin[x]) (x+1)^2 + (1 - Sin[x]) (x+6)^2 , (1 - Sin[x]) ]
Out[21]= (1 + x)^2 + (6+x)^2 - (1+x)^2 Sin[x] - (6+x)^2 Sin[x]

The Expand command together with Trig -> True will reshuffle trig expressions.
Expand[ Sin[x+y] + Cos[x] Sin[y] , Trig -> True]
Out[22]= Cos[y] Sin[x] + 2 Cos[x] Sin[y]

Expand[ Sin[x+y] + Cos[x] Sin[y] , Trig -> False ]
Out[23]= Cos[x] Sin[y] + Sin[x+y]

Or one can use just one command:
TrigExpand[Sin[x + y]]
Cos[y] Sin[x] + Cos[x] Sin[y]

 

Return to Mathematica page
Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Value Problems)