Preface


This tutorial was made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica and programming before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel. The Mathematica commands in this tutorial are all written in bold black font, while Mathematica output is in normal font.

Finally, you can copy and paste all commands into your Mathematica notebook, change the parameters, and run them because the tutorial is under the terms of the GNU General Public License (GPL). You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. The tutorial accompanies the textbook Applied Differential Equations. The Primary Course by Vladimir Dobrushkin, CRC Press, 2015; http://www.crcpress.com/product/isbn/9781439851043

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

Collect


The Collect command allows the user to direct Mathematica to collect like powers of a variable together. We begin with a simple mathematical example: Mathematica can help us write the expression \( yx^2+x^2+3y^2 x+xy^4+(x+1+y)^2 . \) Upon expansion we see this is equivalent to :
Expand[ y*x^2 + x^2 + 3 y^2 x + x y^4 + (x+1+y)^2 ]
Out[1]= 1 + 2 x + 2 x^2 + 2 y + 2 x y + x^2 y + y^2 + 3 x y^2 + x y^4
We can collect terms of like x powers as follows:
Collect[ 1 + 2 x + 2 x^2 + 2 y + 2 x y + x^2 y + y^2 + 3 x y^2 + x y^4 , x]
Out[2]= 1 + 2 y + y^2 + x^2 (2 + y) + x (2 + 2 y + 3 y^2 + y^4 )
We can also have Mathematica collect powers of y:
Collect[ 1 + 2 x + 2 x^2 + 2 y + 2 x y + x^2 y + y^2 + 3 x y^2 + x y^4 , y]
Out[2]= 1 + 2 x + 2 x^2 + (2 + 2 x + x^2) y + (1 + 3 x ) y^2 + x y^4

Example: Suppose that in a region of wilderness, the function \( f(x,y) = 1 + x^2 - 2 y + x y + 10 y^2 - 7 x y^2 + x^2 y^2 + 2 y^3 + x y^3 + x^2 y^3 \) models the topography where f(x,y) is the height and x and y indicate the location. Suppose you want to create a simple formula for the height of the wilderness as a function of y at several points x = 𝑎. We can accomplish this most easily by using Mathematica to collect the y terms.

Collect[ 1 + 2 x + 2 x^2 + 2 y + 2 x y + x^2 y + f[x_, y_] = 1 + x^2 \[Minus] 2*y + x*y + 10*y^2 \[Minus] 7*x*y^2 + x^2 *y^2 + 2*y^3 + x*y^3 + x^2 * y^3;
g[y_] = Collect[f[a, y], y]
Collect[ 1 + 2 x + 2 x^2 + 2 y + 2 x y + x^2 y + 1 + a^2 + (-2 + a) y + (10 - 7 a + a^2) y^2 + (2 + a + a^2) y^3
   ■

 

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)