You are on page 1of 6

Complex variables in Mathematica

An intro for Math 398


We will have a few Mathematica assignments scattered throughout the term. This first assignment should bring everyone up to speed on the use of Mathematica and serve as an introduction to complex variables in Mathematica. Most of this document is intended as a Mathematica reference for you. There are three homework problems at the end worth 5 points apiece for a total of 15 points. They are due to me as an email attachment by Wednesday, September 10. You should make sure that all your commands work sequentially from a fresh kernel and then delete all your output using the Delete All Output command from the Kernel menu before emailing the notebook

The basics
This section introduces the very basics of Mathematica ; it is by no means comprehensive, since the topics have been chosen largely for our immediate needs. There is nothing particularly specific to the study of complex variables in this section and no homework directly from the section.

Mathematica as a calculator
The bold print below is Mathematica input. You should type the inputs exactly as shown and hit the key to examine the outputs. Please take the phrase exactly as shown seriously. Items such as case (upper or lower), spacing, and brackets tend to be important. In some of the commands below, the order of execution is important. Also, the key is distinct from the key.
Pi * H42 - 28L 3

Note that Mathematica returns an exact value rather than a decimal approximation. This is an important distinction in this class which you should understand. Examples of exact numbers are 1,
% N

2 , p, and p2 6, while 1.0, 1.414, 3.141, and 1.645 are

numerical approximations to these numbers. If you want a decimal approximation, you can get it by passing the result to the numerical evaluator function N as follows:

In this command, % refers to the previous output and // passes that result to the function which follows, in this case, N. Here's a numerical approximation to p.
N@PiD

And here's a more precise approximation.


N@Pi, 100D

Note that if an approximate number is used in a computation, then the result will be approximate. Thus the following two inputs yield different results.
HPi ^ 2L 6 HPi ^ 2L 6.

Mathematica has extensive knowledge about mathematical functions. Note: built in function names are always capitalized and the arguments are enclosed in square brackets.

MathematicaHW1.nb

Mathematica has extensive knowledge about mathematical functions. Note: built in function names are always capitalized and the arguments are enclosed in square brackets.
Sin@Pi 3D

Brackets, braces, and parentheses: [],

{}, ()

These all have distinct purposes in Mathematica ; they cannot be mixed and matched. Brackets [] are used to enclose the argument of a function such as:
N@Tan@1D, 20D H2 * 5 - 1L 3

Parentheses () are used for grouping in mathematical expressions such as:

Braces {} are used to form lists. Lists play a very important role in Mathematica as they are a fundamental data structure. Many functions can act on lists.
N@8Sqrt@2D, E<D

And some functions return lists. One function which is convenient for constructing lists is Table. Table[expr, {i,imin, imax}] constructs the list obtained by letting the variable i run from imin to imax in expr. For example, here is a list of the first 100 prime numbers.
Table@Prime@iD, 8i, 1, 100<D

Defining and using functions


Here's how to define a simple function in Mathematica.
f@x_D := x ^ 2; f@2D

We may now do simple computations with the function.


f '@8D

Again, the syntax is quite important. In particular, the underscored variable x_ let's Mathematica know what's going on. Also, you usually want to use a colon-equals := rather than just an equals = when defining functions. There's a subtle reason for this which we might discuss later. There are situations where this rule of thumb is violated and one of these situations occurs when defining functions arising from solutions of differential equations. Be careful to mimic my syntax in this document correctly.

Substitution rules
Functions are nice since they provide a convenient way to plug values into expressions. Another way to do this is to use a rule of the form var -> value together with the substitution operator /.. For example, here's another way to plug x = 2 into the expression x2 .
x ^ 2 . x 2 r * E ^ Hk * tL . 8r 2, k 3<

We will frequently do this when we want to plug in a value for a parameter. For example:

Solve

and NSolve

MathematicaHW1.nb

Solve

and NSolve

Mathematica has two main functions for solving algebraic equations; we can use these to reinforce our understanding of exact numbers versus numerical approximation. We can use Solve to find the exact roots of x2 - 2 = 0.
Solve@x ^ 2 - 2 0, xD

We can get the numerical approximations using NSolve.


NSolve@x ^ 2 - 2 0, xD

This example was pretty easy. Of course, Solve works with somewhat harder examples.
Solve@x ^ 3 - 3 x + 1 0, xD NSolve@x ^ 3 - 3 x + 1 0, xD

You can see why we might want numerical approximations.

We will see a similar dichotomy of approach when we meet the analogs for solving differential equations, DSolve and NDSolve.

Plotting functions
Of course, we love graphics. One major graphical command is the Plot command; it plots graphs of real valued functions of a single variable.
Plot@x ^ 2, 8x, - 2, 2<D

We can plot in 3D as well.

Plot3D@x ^ 2 - y ^ 2, 8x, - 2, 2<, 8y, - 2, 2<D

and plots the image of p in 2 . For example, the standard parameterization of a circle is pHtL = HcosHtL, sinHtLL; the circle is traced out for 0 t 2 p. Here's how we illustrate this.
ParametricPlot@8Cos@tD, Sin@tD<, 8t, 0, 2 Pi<D

More important for us is the ParametricPlot command. There are two possible forms. The first accepts a function p : 2

The second form of ParametricPlot accepts a function f : 2 2 and again plots the image of p. For example, we could a second parameter to the previous example to generate a filled disk.
ParametricPlot@8r * Cos@tD, r * Sin@tD<, 8r, 0, 1<, 8t, 0, 2 Pi<D

This is a fairly important example to understand, since we'll frequently visualize complex functions using a similar technique.

Random numbers
There are several functions for random number generation. We'll use the RandomInteger function in just a bit. Here's a random integer in @0, 10D.
RandomInteger@80, 10<D

MathematicaHW1.nb

Here's a list of 20 random integers in @1, 5D.


RandomInteger@81, 5<, 820<D RandomComplex@80, 1 + I<D

And here's a random complex number in the unit square.

Getting help
The help browser if great. It contains the entire text of The Mathematica Book - about 1500 pages! You can get help on a specific command using ?. For example, here's how to get info on the Plot command.
? Plot

This gives you just a brief synopsis. If you click on the More... hyperlink, you'll go to the Plot page in the help browser. You might also notice that Plot can plot more than one graph at a time from the help. We'll use that later.

Some complex variables


Complex variables are built in to Mathematica , even without trying. Look what happens if we find the roots of a simple polynomial.
Solve@x ^ 2 + 1 0, xD

The roots are . Note that you can enter the imaginary unit as I. Here's proof.
I^2

Of course, Mathematica can quickly do complicated arithmetic with complex numbers.


H1 + IL ^ 100 + 2 ^ 50

Remember that??

Roots of polynomials
We've learned that polynomials of degree n always have n complex roots, although we'll learn why this is true later. Here we'll see how to use Mathematica to find and plot these roots in the complex plane. Let's start with an example we understand, yet still leads to a neat graphic. We'll find the roots of pHxL = x20 - 1; these should be equally distributed around the unit circle, right? First, let's define the polynomial.
f@z_D := z ^ 20 - 1;

Now let's find the roots.

sols = NSolve@f@zD 0, zD numbers = z . sols

The solution has been returned as a list of substitution rules. It's easy to get from here to the actual numbers.

We want to translate them into points that we can plot in the complex plane. We can do this using the Re and Im functions that return real and imaginary parts. We can use a Table to do this for all z in numbers.

MathematicaHW1.nb

points = Table@8Re@zD, Im@zD<, 8z, numbers<D

Finally, we use ListPlot to see the picture.

ListPlot@points, AspectRatio AutomaticD f@z_D := z ^ 100 - 1; sols = NSolve@f@zD 0, zD; numbers = z . sols; points = Table@8Re@zD, Im@zD<, 8z, numbers<D; ListPlot@points, AspectRatio AutomaticD

Isn't that cool!! We can combine the commands and do it with a higher degree polynomial.

Uh-oh, something looks wrong. The problem is numerical error. It's not a bug, but rather a well understood property of numerical approximation. The exact details are the subject of a numerical analysis class, but you should be aware of the possibility. One quick fix is to use arbitrary precision arithmetic by setting a WorkingPrecision option in NSolve.
f@z_D := z ^ 100 - 1; sols = NSolve@f@zD 0, z, WorkingPrecision 12D; numbers = z . sols; points = Table@8Re@zD, Im@zD<, 8z, numbers<D; ListPlot@points, AspectRatio AutomaticD

Visualizing functions
Recall that we visualize a complex function mapping by examining its effect various domains in . This is exactly what
ParametricPlot does for functions mapping 2 2 ; we just need to translate the our complex function to a function on the

real plane. We translate the input by explicitly writing z = x + y and we translate the output using Re and Im. Here's an example.
f@z_D := H1 + IL z; ParametricPlot@8Re@f@x + I * yDD, Im@f@x + I * yDD<, 8x, - 1, 1<, 8y, 0, 1<D

We can emphasize the polar aspect by plugging in r q , rather than x + y.

f@z_D := z ^ 2; ParametricPlot@8Re@f@r * Exp@I * tDDD, Im@f@r * Exp@I * tDDD<, 8r, 0, 2<, 8t, 0, Pi 2<D

MathematicaHW1.nb

HW
Here's three problems worth five points apiece. 1. Here's a polynomial of degree 100 whose coefficients are randomly chosen to be zero or one.
degree = 100; poly = RandomInteger@80, 1<, 8degree + 1<D.Table@z ^ n, 8n, 0, degree<D

Plot the roots of the polynomial. Try the same thing for polynomials of various degrees and types of randomly generated coefficients. Formulate a conjecture on the distribution of the roots for large degrees. 2. Plot the image of the unit disk under the function f HzL = 2 z - z2 . Prove that the boundary is, in fact, a cardioid. 3. On Friday, we played with the following Mbius transformation: T HzL = 1+z 1-z .

We proved that this function maps the unit disk onto the right half plane. In the course of the proof, we needed the inverse of the function, although Sam publicly doubted my inverse. a) Use Solve to find the inverse of the function. b) Use ParametricPlot to visualize the image of a relatively large rectangle of the form @0, M D @- M , M D. Explain why the image makes sense.

You might also like