Symbolic Computation with Mathematica

Basics

When using Mathematica there are a few basic commands that are usful to know and will help in dealing with various equations. These commands are %, !, and saving an equation to a variable.

The ! operator allows you to call a Unix function. If you were to type in !ls at a prompt, Mathematica would list the files of the directory you are currently in. In this same manner you can change directories and clear the screen. This will become vary useful if you want to save or open files later as it will allow you to do so without exiting Mathematica.

The % operator allows you to call the last computed result. Likewise, typing %% allows you to call the next to last computed result. This is helpful in that you can call an equation or result that you just computed without retyping it. Shown below is an example.

Exmple:

In[1]:= 2.1^3
Out[1]= 9.261
In[2]:= 1.1^2+%
Out[2]= 10.471

In the example above, the user first computes the value of 2.1^3. Next, the user would like to add the value of 1.1^2 too this result. Instead of typing 2.1^3 over again, the user is able to simply enter 1.1^2 and then use the % command. Mathematica interprets this as 1.1^2+2.1^3 and produces the desired result. While this example is fairly simple, using % becomes more and more useful when dealing with larger numbers or equations that would be tiresome to retype.

While the % operator may be useful, an even better method is to save equations or constant to variables. This will allow you to write an equation, perform numerous other computations and then retreive the original equation by simply entering the variable with which you saved it.

Example:

In[21]:= x^7+3x^3+9x^2+22x+13
                        2      3    7
Out[21]= 13 + 22 x + 9 x  + 3 x  + x

In[22]:= f=%
                        2      3    7
Out[22]= 13 + 22 x + 9 x  + 3 x  + x
In[23]:= f
                        2      3    7
Out[23]= 13 + 22 x + 9 x  + 3 x  + x

The user first enter a lengthy equation at In[21] and Mathematica simply outputs the equation. Next, the user uses the % command to assign the equation to the variable f. Finally, the user simply enters f at In[23] and Mathematica returns the equation. IMPORTANT: Before using a variable in an equation make sure that the variable has been cleared and that nothing is associated with it. This can be done by entering

Clear[variable]

at a prompt. When variable is now called Mathmatica will not return anything. This makes sure that you do not associate or combine equations you entered a while ago with one you are entering presently. Obviously, if this is your intent then do not clear the variable.

Symbolic Computation

Factor and Expand

Sometimes the best way to gain insight into an equation is to view it in different ways. The Mathematica commands Factor and Expand allow you to do this. One thing you should know right now, arguments to functions in Mathematica are always enclosed in square brackets.

In the example below an eqution is entered and first factored, and then expanded with Mathematica.

In[1]:= y^3+3y^2+3y+1                                               

                     2    3
Out[1]= 1 + 3 y + 3 y  + y

In[2]:= Factor[%]

               3
Out[2]= (1 + y)

In[3]:= Expand[%]

                     2    3
Out[3]= 1 + 3 y + 3 y  + y

Factor and Expand do not have to be called with a % or a variable that represents an equation. In the above example the equation after In[1] could have been type within the squre brackets of the Factor command.

Solve

Solve is a very useful function when dealing with various equations. It lets you solve an equation for some constant or for some variable within the equation. By using Solve equations can be rearranged and put in terms of different variables within them. Solve takes two arguments, the first is the equation to be used and the second is the variable that the equation is to solved in terms of.

In[1]:= 1+3y+3y^2+y^3

                     2    3
Out[1]= 1 + 3 y + 3 y  + y

In[2]:= x=%

                     2    3
Out[2]= 1 + 3 y + 3 y  + y

In[3]:= Solve[x==0,y]

Out[3]= {{y -> -1}, {y -> -1}, {y -> -1}}

In the example above an equation was entered into the variable x which was then set equal to zero. This equation was then solved in terms of the variable y. Solve returned three answers for this problem.

Below are two examples of equations that have two variables within them and Solve is able to solve for one of the variables. In the first example Solve returns one answer, while in the second example it returns two.

Example:

In[10]:= 100==3x+5y+2x^2+2y^3

                         2            3
Out[10]= 100 == 3 x + 2 x  + 5 y + 2 y

In[11]:= Solve[%, x]

                                                 3
                -3 - Sqrt[9 - 8 (-100 + 5 y + 2 y )]
Out[11]= {{x -> ------------------------------------}, 
                                 4
 
                                            3
           -3 + Sqrt[9 - 8 (-100 + 5 y + 2 y )]
>    {x -> ------------------------------------}}
                            4

Example:

In[8]:= 100==x*E^y

                y
Out[8]= 100 == E  x

In[9]:= Solve[%, y]

Solve::ifun: Warning: Inverse functions are being used by Solve, so some
     solutions may not be found.

                   100
Out[9]= {{y -> Log[---]}}
                    x

In the second example there is a general warning that Solve may not find some solutions for complicated equations. Don't let the warning deter you from using Solve though, it is a very powerful function that can be very useful.

Expand, ExpandAll, Together, and Apart

The four functions in this section are also used to manipulate equations. Below is an example that uses all four of these commands to manipulate the equation stuff. This example has been taken from the reference Applied Mathematica.

In[1]:= stuff=(x^2-4)(x-3)^2/((x-2)(x-4)(x-6))

                   2        2
           (-3 + x)  (-4 + x )
Out[1]= --------------------------
        (-6 + x) (-4 + x) (-2 + x)

In[2]:= Expand[stuff]

                   -36                          24 x
Out[2]= -------------------------- + -------------------------- + 
        (-6 + x) (-4 + x) (-2 + x)   (-6 + x) (-4 + x) (-2 + x)
 
                   2                            3
                5 x                          6 x
>    -------------------------- - -------------------------- + 
     (-6 + x) (-4 + x) (-2 + x)   (-6 + x) (-4 + x) (-2 + x)
 
                  4
                 x
>    --------------------------
     (-6 + x) (-4 + x) (-2 + x)

In[3]:= ExpandAll[stuff]

                  -36                      24 x
Out[3]= ----------------------- + ----------------------- + 
                         2    3                    2    3
        -48 + 44 x - 12 x  + x    -48 + 44 x - 12 x  + x
 
                 2                         3
              5 x                       6 x
>    ----------------------- - ----------------------- + 
                      2    3                    2    3
     -48 + 44 x - 12 x  + x    -48 + 44 x - 12 x  + x
 
                4
               x
>    -----------------------
                      2    3
     -48 + 44 x - 12 x  + x

In[4]:= Together[stuff]

                2
        (-3 + x)  (2 + x)
Out[4]= -----------------
        (-6 + x) (-4 + x)

In[5]:= Apart[stuff]

              36       3
Out[5]= 6 + ------ - ------ + x
            -6 + x   -4 + x

In[6]:= Clear[stuff]

While this example may seem long and complicated, concentrate on what the commands are doing to the equation and not on the equation itself. The equation is big simply to better illustrate what is occurring with the functions.

First Expand took the equation and broke it up but only targeted the numerator. ExpandAll which came next, broke up the equation too but targeted both the numerator and the denominator.

You may have noticed that Together did not return the exact same equation as was originally saved in the variable stuff. this is because there was a common factor of (-2+x) in both the numerator and denominator the was automatically removed by Together. After Together the Apart function was used to expand the equation into partial fractions.

References

[ST94] Applied Mathematica
William T. Shaw and Jason Tigg
Addison-Wesley, Menlo Park CA (1994).

[Rob95] Engineering Mathematics with Mathematica
John S. Robertson
McGraw-Hill, New York NY (1995).

[GG91] Exploring Mathematics with Mathematica
Theodore W. Gray and Jerry Glynn
Addison-Wesley, Menlo Park CA (1991).

Last Modified: 97.08.15 Michael Carreno <mcarreno@cs.princeton.edu>