Computational Tools

Introduction to Mathematica: Working with ExpressionsPage 1 of 4

Introduction to Mathematica: Working with Expressions

Overview

This document will task you with defining, differentiating, integrating, and plotting expressions in Mathematica. A Mathematica expression is a statement that defines operations on symbolic variables or real numbers. Expressions can be integrated, differentiated, plotted, and more. This document is intended to expose you to basic operations in Mathematica.. After today’s work you should be able to

  • Create expressions
  • Integrate and Differentiate expressions
  • Plot expressions
  • Defining variables

To begin, start Mathematica from the Windows Start menu.

Basic Computations

Once the program is running, type the following:

1 3

1*3 [shift-enter]

What are the results? Mathematic assumed the space meant multiplication and did it for you. Did you see the small x?All basic mathematics operators can be used *,+,- and /. Next type

1 / 3

1. / 3 [shift-enter]

How did the results differ? Mathematica interprets the decimal to mean a floating point number and therefore performs floating point calculations. Next type

Pi / 3

Pi / 3.

pi / 3 [shift-enter]

Are the results what you expected? Notice that the pi turned blue since it was an unknown or symbolic variable. Mathematica capitalizes the first letter of expression names it knows already. Next, begin to work with symbolicexpressions by typing

x^3+24 x +16 x x [shift-enter]

(x + 24) / ((x-1)*x(+10)) [shift-enter]

Notice that Mathematica does not have an issue with unknown variables; it can operate on them as symbols or placeholders. Try typing

(x*y+y^2+c*x^2) [shift-enter]

We can use any number of unknown variables in an expression. If we later invoke a command that requires their definition, the software will have a problem, but at this point the symbolic expressions do not cause any issues.

Now., make use of the % operator, last output, and the Apart[] command to expand all the terms in (x*y+y^2+x^2)^2.

Basic Derivatives and Integration

Mathematica can perform symbolic differentiation and integration. Symbolic differentiation in text format uses the D operator. Type

x^2 + 4 y x + 3 y^2

D[x^2 + 4 y x + 3 y^2,x]

D[x^2 + 4 y x + 3 y^2,y]

D[x^2 + 4 y x+ 3 y^2,x,y] [shift-enter]

The second argument denotes the variable for differentiation. Notice the simple implementation of the second derivative.

Integration can be invoked using the Integrate command which has the basic forms

Integrate[expression,var1,var2,…]

Integrate[expression, {var1,low,high},{var2,low,high},…]

Note the use of the {} structure to pass a list of information about the variables of integration to the integration command. Using the above forms, find the integral of y*x+y^2

  • indefinitely,
  • with respect to x from 0 to Pi/2, and
  • with respect tox from 0 to Pi/2 and y from 0 to 10.

NOTE: As in MathCad, you can use Palettes>BasicMathInput and Palettes>Algebraic Manipulation from the toolbar to perform all of the above command using math-type symbols.

NOTE: As in matlab, the semicolon ‘;’ suppresses output to the screen.

Basic Plotting

Mathematica also has many plotting functions. Similar to other programs you can plot lists of data. Try the following examples (remember to use shift enter to execute commands).

ListPlot[{{1,1},{4,2},{8,3},{16,4}}, PlotJoined -> True]

Here the x-y pairs are plotted with the option PlotJoined that links the points together. You can also plot simple expressions using the format Plot[expression,{var,low,high}] as in the following example:

Plot[x^2+1,{x,1,10}]

Multiple expressions with several options can be plotted as well. Try copying and executing the four-line command below.

Plot[{Sin[x],Cos[x]},{x,-,},

PlotStyle->{RGBColor[1,0,0],RGBColor[0,0,1]},

PlotRange->{{-2 ,2 },{-1.5,1.5}},

PlotLabel->"Dual Trig Functions"]

The above command is a good example of embedded Mathematica syntax and structure. Curly brackets {} are used to donate a list of arguments or parameters within a command while square brackets denote the argumentsto the command itself.

Defining Variables

Mathematic assignment works similarly to matlab assignment. The basic syntax is

<variable name> <assignment type> <expression>

Where variable name is the name given to the output, assignment type is the mode off assigning the variable, and the expression is the expression to which to assign the variable. For example, try out

var = x^3 y +z 5^x

D[var,x]

NOTE: You cannot, in general, give variable names a trailing underscore, ‘_’, since this denotes a special type of variable (a local variable or an argument to a function).

Further Exploration Questions and Activities

Mathematica can also perform some pretty amazing graphics somewhat easily (relative to the complexity of the plot). Execute the plot commands below then answer the questions that follow.

ParametricPlot[{t + Sin[t], 1+Cos[t]},{t,0,4 }]

Plot3D[(x^2 - y^2) E^(-x^2 - y^2), {x, -2, 2}, {y, -2, 2}]

ContourPlot[(x^2 - y^2) E^(-x^2 - y^2), {x, -2, 2}, {y, -2, 2}]

ParametricPlot3D[{Cos[],Sin[],},{,0,2 }]

ParametricPlot3D[{Cos[] Sin[],Sin[] Sin[],Cos[]},{,0,2 },{,0,}]

Questions

  1. Show/describe all instances of where square brackets are used to encapsulate the arguments to a command in the Dual Trig Function plot command above?
  1. Show/describe all instances where curly brackets are used to encapsulate a list of parameters or arguments to a function in the Dual Trig Function plot command above.

3. Go online and find the wildest, most impressive Mathematica visualization demonstration that you can find. Describe what it is showing and the commands that it is using. Explain the syntax of the commands. Also include a printout of the graphic.

Deliverables