Basic Operations on Numbers, Expressions, and Functions

Keywords

Operations on numbers; Operations on functions; Two-dimensional graphics; Three-dimensional graphics; Image processing

2.1 Numerical Calculations and Built-In Functions

2.1.1 Numerical Calculations

The basic arithmetic operations (addition, subtraction, multiplication, division, and exponentiation) are performed in the natural way with Mathematica. Whenever possible, Mathematica gives an exact answer and reduces fractions.

1.  “a plus b,” a+bImage, is entered as a+b;

2.  “a minus b,” abImage, is entered as a-b;

3.  “a times b,” ab, is entered as either a*b or a b (note the space between the symbols a and b);

4.  “a divided by b,” a/bImage, is entered as a/b. Executing the command a/b results in a fraction reduced to lowest terms; and

5.  “a raised to the bth power,” abImage, is entered as a^b.

Example 2.1

Calculate (a) 121+542Image; (b) 32319876Image; (c) (23)(76)Image; (d) (22341)(832748)(387281)Image; and (e) 46731Image.

Solution

These calculations are carried out in the following screen shot. In each case, the input is typed and then evaluated by pressing Enter. In the last case, the Basic Math template is used to enter the fraction.

Image

Image

 □

The term an/m=anm=(am)nImage is entered as a^(n/m). For n/m=1/2Image, the command Sqrt[a] can be used instead. Usually, the result is returned in unevaluated form but N can be used to obtain numerical approximations to virtually any degree of accuracy. With N[expr,n], Mathematica yields a numerical approximation of expr to n digits of precision, if possible. At other times, Simplify can be used to produce the expected results.

Remark 2.1

If the expression b in abImage contains more than one symbol, be sure that the exponent is included in parentheses. Entering a^n/m computes an/m=1manImage while entering a^(n/m) computes an/mImage.

Example 2.2

Compute (a) 27Image and (b) 823=82/3Image.

Solution

(a) Mathematica automatically simplifies 27=33Image. We use N to obtain an approximation of 27Image. (b) Mathematica automatically simplifies 82/3Image.

N[number] and number//N return numerical approximations of number.

Image □

When computing odd roots of negative numbers, Mathematica's results are surprising to the novice. Namely, Mathematica returns a complex number. We will see that this has important consequences when graphing certain functions.

Example 2.3

Calculate (a) 13(2764)2Image and (b) (2764)2/3Image.

2.1.2 Built-In Constants

Mathematica has built-in definitions of nearly all commonly used mathematical constants and functions. To list a few, e2.71828Image is denoted by E, π3.14159Image is denoted by Pi, and i=1Image is denoted by I. Usually, Mathematica performs complex arithmetic automatically.

Other built-in constants include ∞, denoted by Infinity, Euler's constant, γ0.577216Image, denoted by EulerGamma, Catalan's constant, approximately 0.915966, denoted by Catalan, and the golden ratio, 12(1+5)1.61803Image, denoted by GoldenRatio.

Example 2.4

Entering

N [ E , 50 ] Image

2.7182818284590452353602874713526624977572470937000

returns a 50-digit approximation of e. Entering

N [ π , 25 ] Image

3.141592653589793238462643

returns a 25-digit approximation of π. Entering

3 + I 4 I Image

11 17 + 7i 17 Image

performs the division (3+i)/(4i)Image and writes the result in standard form.

2.1.3 Built-In Functions

Functions frequently encountered by beginning users include the exponential function, Exp[x]; the natural logarithm, Log[x]; the absolute value function, Abs[x]; the trigonometric functions Sin[x], Cos[x], Tan[x], Sec[x], Csc[x], and Cot[x]; the inverse trigonometric functions ArcSin[x], ArcCos[x], ArcTan[x], ArcSec[x], ArcCsc[x], and ArcCot[x]; the hyperbolic trigonometric functions Sinh[x], Cosh[x], and Tanh[x]; and their inverses ArcSinh[x], ArcCosh[x], and ArcTanh[x]. Generally, Mathematica tries to return an exact value unless otherwise specified with N.

Several examples of the natural logarithm and the exponential functions are given next. Mathematica often recognizes the properties associated with these functions and simplifies expressions accordingly.

Example 2.5

Entering

E 5 Image

1 e 5 Image

E 5 // N Image

0.00673795

returns an approximation of e5=1/e5Image. Entering

N[number] or number//N return approximations of number.

Exp[x] computes exImage. Enter E to compute e2.718Image.

Log [ Exp [ 3 ] ] Image

3

computes lne3=3Image. Entering

Log[x] computes lnxImage. lnxImage and exImage are inverse functions (lnex=xImage and elnx=xImage) and Mathematica uses these properties when simplifying expressions involving these functions.

Exp [ Log [ Pi ] ] Image

π

computes elnπ=πImage. Entering

Abs [ 5 ] Image

5

computes |5|=5Image. Entering

Abs[x] returns the absolute value of x, |x|Image.

Abs [ ( 3 + 2 I ) / ( 2 9 I ) ] Image

13 85 Image

computes |(3+2i)/(29i)|Image. Entering

Cos [ Pi / 12 ] Image

1 + 3 2 2 Image

N [ Cos [ Pi / 12 ] ] Image

0.965926

computes the exact value of cos(π/12)Image and then an approximation. Although Mathematica cannot compute the exact value of tan1000Image, entering

N[number] and number//N return approximations of number.

N [ Tan [ 1000 ] ] Image

1.47032

returns an approximation of tan1000Image. Similarly, entering

N[ArcSin[1/3]]Image

0.339837

returns an approximation of sin1(1/3)Image and entering

ArcCos [ 2 / 3 ] // N Image

0.841069

returns an approximation of cos1(2/3)Image.

Mathematica is able to apply many identities that relate the trigonometric and exponential functions using the functions TrigExpand, TrigFactor, TrigReduce, TrigToExp, and ExpToTrig.

Image

Image

Example 2.6

Mathematica does not automatically apply the identity sin2x+cos2x=1Image.

Cos [ x ] 2 + Sin [ x ] 2 Image

Cos [ x ] 2 + Sin [ x ] 2 Image

To apply the identity, we use Simplify. Generally, Simplify[expression] attempts to simplify expression.

Simplify [ Cos [ x ] 2 + Sin [ x ] 2 ] Image

1

Use TrigExpand to multiply expressions or to rewrite trigonometric functions. In this case, entering

Many of the algebraic manipulation commands can be accessed from the AlgebraicManipulation palette.

Image

TrigExpand [ Cos [ 3 x ] ] Image

Cos [ x ] 3 3 Cos [ x ] Sin [ x ] 2 Image

writes cos3xImage in terms of trigonometric functions with argument x. We use the TrigReduce function to convert products to sums.

TrigReduce [ Sin [ 3 x ] Cos [ 4 x ] ] Image

1 2 ( Sin [ x ] + Sin [ 7 x ] ) Image

We use TrigExpand to write

TrigExpand [ Sin [ 3 x ] Cos [ 4 x ] ] Image

TrigExpand [ Cos [ 2 x ] ] Image

Sin [ x ] 2 + 7 2 Cos [ x ] 6 Sin [ x ] 35 2 Cos [ x ] 4 Sin [ x ] 3 + 21 2 Cos [ x ] 2 Sin [ x ] 5 Sin [ x ] 7 2 Image

Cos [ x ] 2 Sin [ x ] 2 Image

in terms of trigonometric functions with argument x. We use ExpToTrig to convert exponential expressions to trigonometric expressions.

ExpToTrig [ 1 / 2 ( Exp [ x ] + Exp [ x ] ) ] Image

Cosh [ x ] Image

Similarly, we use TrigToExp to convert trigonometric expressions to exponential expressions.

TrigToExp [ Sin [ x ] ] Image

1 2 i e i x 1 2 i e ix Image

Usually, you can use Simplify to apply elementary identities.

Simplify [ Tan [ x ] 2 + 1 ] Image

Sec [ x ] 2 Image

A Word of Caution

Remember that there are certain ambiguities in traditional mathematical notation. For example, the expression sin2(π/6)Image is usually interpreted to mean “compute sin(π/6)Image and square the result.” That is, sin2(π/6)=[sin(π/6)]2Image. The symbol sin is not being squared; the number sin(π/6)Image is squared. With Mathematica, we must be especially careful and follow the standard order of operations exactly, especially when using InputForm. We see that entering

Image

computes sin2(π/6)=[sin(π/6)]2Image while

Image

raises the symbol Sin to the power 2[π6]Image. Mathematica interprets sin2(π6)Image to be the product of the symbols sin2 and π/6Image. However, using TraditionalForm we are able to evaluate sin2(π/6)=[sin(π/6)]2Image with Mathematica using conventional mathematical notation.

Image

Be aware, however, that traditional mathematical notation does contain certain ambiguities and Mathematica may not return the result you expect if you enter input using TraditionalForm unless you are especially careful to follow the standard order of operations, as the following warning message indicates.

Image

2.2 Expressions and Functions: Elementary Algebra

2.2.1 Basic Algebraic Operations on Expressions

Expressions involving unknowns are entered in the same way as numbers. Mathematica performs standard algebraic operations on mathematical expressions. For example, the commands

1.  Factor[expression] factors expression;

2.  Expand[expression] multiplies expression;

3.  Together[expression] writes expression as a single fraction; and

4.  Simplify[expression] performs basic algebraic manipulations on expression and returns the simplest form it finds.

For basic information about any of these commands (or any other) enter ?command as we do here for Factor.

Many of the algebraic manipulation commands can be accessed from the AlgebraicManipulation palette.

Image

Image

or access the Documentation Center as we do here for Factor.

Image

When entering expressions, be sure to include a space or * between variables to denote multiplication to avoid any ambiguity or confusion. For example, Mathematica interprets 4x to be the product of x and 4. On the other hand, Mathematica interprets x4 to be a new symbol with the name “x4.” Similarly, xy is interpreted as a symbol with the name “xy” while x y and x*y are interpreted as the product of x and y, xy.

Example 2.8

(a) Factor the polynomial 12x2+27xy84y2Image. (b) Expand the expression (x+y)2(3xy)3Image. (c) Write the sum 2x2x22Image as a single fraction.

Solution

The result obtained with Factor indicates that 12x2+27xy84y2=3(4x7y)(x+4y)Image. When typing the command, be sure to include a space, or *, between the x and y terms to denote multiplication. xy represents an expression while x y or x*y denotes x multiplied by y.

Factor [ 12 x 2 + 27 x y 84 y 2 ] Image

3 ( 4 x 7 y ) ( x + 4 y ) Image

We use Expand to compute the product (x+y)2(3xy)3Image and Together to express 2x2x22Image as a single fraction.

Expand [ ( x + y ) 2 ( 3 x y ) 3 ] Image

27 x 5 + 27 x 4 y 18 x 3 y 2 10 x 2 y 3 + 7 x y 4 y 5 Image

Together [ 2 x 2 x 2 2 ] Image

4x42x2Image □

To factor an expression like x23=x2(3)2=(x3)(x+3)Image, use Factor with the Extension option.

Factor[x̂2-3] returns x23Image.

Factor [ x 2 3 , Extension { Sqrt [ 3 ] } ] Image

( 3 x ) ( 3 + x ) Image

Similarly, use Factor with the Extension option to factor expressions like x2+1=x2i2=(x+i)(xi)Image.

Factor [ x 2 + 1 ] Image

1 + x 2 Image

Factor [ x 2 + 1 , Extension { I } ] Image

( i + x ) ( i + x ) Image

Mathematica does not automatically simplify x2Image, to the expression x

Sqrt [ x 2 ] Image

x2Image

because without restrictions on x, x2=|x|Image. The command PowerExpand[expression] simplifies expression assuming that all variables are positive. Alternatively, you can use Assumptions to tell Mathematica to assume that x>0Image or to assume that x<0Image.

Simplify [ Sqrt [ x 2 ] , Assumptions x > 0 ] Image

x

Simplify [ Sqrt [ x 2 ] , Assumptions x < 0 ] Image

x

Alternatively, PowerExpand will automatically make “appropriate” assumptions when simplifying expressions.

PowerExpand [ Sqrt [ x 2 ] ] Image

x

Thus, entering

Simplify [ Sqrt [ a 2 b 4 ] ] Image

a 2 b 4 Image

returns a2b4Image but entering

PowerExpand [ Sqrt [ a 2 b 4 ] ] Image

a b 2 Image

Simplify [ Sqrt [ a 2 b 4 ] , Assumptions Image

{ a > 0 , b > 0 } ] Image

a b 2 Image

returns ab2Image. However, if a<0Image and b>0Image, a2b4=ab2Image.

Simplify [ Sqrt [ a 2 b 4 ] , Assumptions Image

{ a < 0 , b > 0 } ] Image

a b 2 Image

In general, a space is not needed between a number and a symbol to denote multiplication when a symbol follows a number. That is, 3dog means “3 times variable dog; dog3 is a variable with name dog3. Mathematica interprets 3 dog, dog*3, and dog 3 as “3 times variable dog. However, when multiplying two variables, either include a space or * between the variables.

1.  cat dog means “variable cat times variable dog.”

2.  cat*dog means “variable cat times variable dog.”

3.  But, catdog is interpreted as a variable catdog.

The command Apart[expression] computes the partial fraction decomposition of expression; Cancel[expression] factors the numerator and denominator of expression then reduces expression to lowest terms.

Example 2.9

(a) Determine the partial fraction decomposition of 1(x3)(x1)Image. (b) Simplify x21x22x+1Image.

Solution

Apart is used to see that 1(x3)(x1)=12(x3)12(x1)Image. Then, Cancel is used to find that x21x22x+1=(x1)(x+1)(x1)2=x+1x1Image. In this calculation, we have assumed that x1Image, an assumption made by Cancel but not by Simplify.

Apart [ 1 ( x 3 ) ( x 1 ) ] Image

1 2 ( 3 + x ) 1 2 ( 1 + x ) Image

Cancel [ x 2 1 x 2 2 x + 1 ] Image

1+x1+xImage □

In addition, Mathematica has several built-in functions for manipulating parts of fractions.

1.  Numerator[fraction] yields the numerator of fraction.

2.  ExpandNumerator[fraction] expands the numerator of fraction.

3.  Denominator[fraction] yields the denominator of fraction.

4.  ExpandDenominator[fraction] expands the denominator of fraction.

Example 2.10

Given x3+2x2x2x3+x24x4Image, (a) factor both the numerator and denominator; (b) reduce x3+2x2x2x3+x24x4Image to lowest terms; and (c) find the partial fraction decomposition of x3+2x2x2x3+x24x4Image.

Solution

The numerator of x3+2x2x2x3+x24x4Image is extracted with Numerator. We then use Factor together with %, which is used to refer to the most recent output, to factor the result of executing the Numerator command.

Numerator [ x 3 + 2 x 2 x 2 x 3 + x 2 4 x 4 ] Image

2 x + 2 x 2 + x 3 Image

Factor [ % ] Image

( 1 + x ) ( 1 + x ) ( 2 + x ) Image

Similarly, we use Denominator to extract the denominator of the fraction. Again, Factor together with % is used to factor the previous result, which corresponds to the denominator of the fraction.

Denominator [ x 3 + 2 x 2 x 2 x 3 + x 2 4 x 4 ] Image

44x+x2+x3Image

Factor [ % ] Image

( 2 + x ) ( 1 + x ) ( 2 + x ) Image

Cancel is used to reduce the fraction to lowest terms.

Cancel [ x 3 + 2 x 2 x 2 x 3 + x 2 4 x 4 ] Image

1 + x 2 + x Image

Finally, Apart is used to find its partial fraction decomposition.

Apart [ x 3 + 2 x 2 x 2 x 3 + x 2 4 x 4 ] Image

1+12+xImage □

You can also take advantage of the AlgebraicManipulation palette, which is accessed by going to Palettes under the Mathematica menu, followed by AlgebraicManipulation, to evaluate expressions.

Image

Example 2.11

Simplify 2(x3)2(x+1)3(x+1)4/3+2(x3)(x+1)2/3Image.

Solution

First, we type the expression.

Image

Then, select the expression.

Image

Move the cursor to the palette and click on Simplify. Mathematica simplifies the expression.

Image □

2.2.2 Naming and Evaluating Expressions

In Mathematica, objects can be named. Naming objects is convenient: we can avoid typing the same mathematical expression repeatedly (as we did in Example 2.10) and named expressions can be referenced throughout a notebook or Mathematica session. Every Mathematica object can be named – expressions, functions, graphics, and so on can be named with Mathematica. Objects are named by using a single equals sign (=).

Because every built-in Mathematica function begins with a capital letter, we adopt the convention that every mathematical object we name in this text will begin with a lowercase letter. Consequently, we will be certain to avoid any possible ambiguity with any built-in Mathematica objects.

With Mathematica 11, the default option is to display known objects in black and unknown objects in blue. Thus, in the following screen shot,

Image

Fraction, x, y, apart, pi, and e are in blue; Apart, 2, Pi, π, ?, Plot, Expand, Cancel, Exp, and E are in black.

To automatically update named variables, Dynamic[x] returns the current value of x.

Thus, Dynamic[x] returns dog.

Image

However, when we enter x=7Image afterwards, Dynamic[x] is automatically updated to the new value of x.

Image

Expressions are easily evaluated using ReplaceAll, which is abbreviated with /. and obtained by typing a backslash (/) followed by a period (.), together with Rule, which is abbreviated with -> and obtained by typing a dash (minus sign) (-) followed by a greater than sign (>). For example, entering the command

x^2 /. x->3

returns the value of the expression x2Image if x=3Image. Note, however, this does not assign the symbol x the value 3: entering x=3 assigns x the value 3.

Example 2.12

Evaluate x3+2x2x2x3+x24x4Image if x=4Image, x=3Image, and x=2Image.

Solution

To avoid retyping x3+2x2x2x3+x24x4Image, we define fraction to be x3+2x2x2x3+x24x4Image.

Of course, you can simply copy and paste this expression if you neither want to name it nor retype it.

Clear [ x ] Image

fraction = x 3 + 2 x 2 x 2 x 3 + x 2 4 x 4 Image

2 x + 2 x 2 + x 3 4 4 x + x 2 + x 3 Image

/. is used to evaluate fraction if x=4Image and then if x=3Image.

If you include a semi-colon (;) at the end of the command, the resulting output is suppressed.

fraction /. x - > 4 Image

3 2 Image

fraction /. x - > 3 Image

4 5 Image

When we try to replace each x in fraction by 2, we see that the result is undefined: division by 0 is always undefined.

Image

However, when we use Cancel to first simplify and then use ReplaceAll to evaluate,

Image

we see that the result is 3/4. The result indicates that limx2x3+2x2x2x3+x24x4=34Image. We confirm this result with Limit.

Image

Generally, Limit[f[x],x->a] attempts to compute limxaf(x)Image. The Limit function is discussed in more detail in the next chapter. □

2.2.3 Defining and Evaluating Functions

It is important to remember that functions, expressions, and graphics can be named anything that is not the name of a built-in Mathematica function or command. As previously indicated, every built-in Mathematica object begins with a capital letter so every user-defined function, expression, or other object in this text will be assigned a name using lowercase letters, exclusively. This way, the possibility of conflicting with a built-in Mathematica command or function is completely eliminated. Because definitions of functions and names of objects are frequently modified, we introduce the command Clear. Clear[expression] clears all definitions of expression, if any. You can see if a particular symbol has a definition by entering ?symbol.

In Mathematica, an elementary function of a single variable, y=f(x)=expressionImage inxImage, is typically defined using the form

f[x_]=expression in x or f[x_]:=expression in x.

Notice that when you first define a function, you must always enclose the argument in square brackets ([...]) and place an underline (or blank) “_” after the argument on the left-hand side of the equals sign in the definition of the function.

Example 2.13

Entering

f [ x _ ] = x / ( x 2 + 1 ) Image

x 1 + x 2 Image

defines and computes f(x)=x/(x2+1)Image. Entering

f [ 3 ] Image

3 10 Image

computes f(3)=3/(32+1)=3/10Image. Entering

f [ a ] Image

a 1 + a 2 Image

computes f(a)=a/(a2+1)Image. Entering

f [ 3 + h ] Image

3 + h 1 + ( 3 + h ) 2 Image

computes f(3+h)=(3+h)/((3+h)2+1)Image. Entering

n1 = Simplify [ ( f [ 3 + h ] f [ 3 ] ) / h ] Image

8 + 3 h 10 ( 10 + 6 h + h 2 ) Image

computes and simplifies f(3+h)f(3)hImage and names the result n1. Entering

n1 /. h 0 Image

2 25 Image

evaluates n1 if h=0Image. Entering

n2 = Together [ ( f [ a + h ] f [ a ] ) / h ] Image

1 a 2 a h ( 1 + a 2 ) ( 1 + a 2 + 2 a h + h 2 ) Image

computes and simplifies f(a+h)f(a)hImage and names the result n2. Entering

n2 /. h 0 Image

1 a 2 ( 1 + a 2 ) 2 Image

evaluates n2 if h=0Image.

Often, you will need to evaluate a function for the values in a list,

list = { a 1 , a 2 , a 3 , , a n } .

Once f(x)Image has been defined, Map[f,list] returns the list

{ f ( a 1 ) , f ( a 2 ) , f ( a 3 ) , , f ( a n ) }

Also,

1.  Table[f[n],{n,n1,n2}] returns the list

{ f ( n 1 ) , f ( n 1 + 1 ) , f ( n 1 + 2 ) , , f ( n 2 ) }

2.  Table[(n,f[n]),{n,n1,n2}] returns the list of ordered pairs

{ ( n 1 , f ( n 1 ) ) , ( n 1 + 1 , f ( n 1 + 1 ) ) , ( n 1 + 2 , f ( n 1 + 2 ) ) , , ( n 2 , f ( n 2 ) ) }

Example 2.14

Entering

Clear [ h ] Image

h [ t _ ] = ( 1 + t ) ( 1 / t ) Image

h [ 1 ] Image

( 1 + t ) 1 t Image

2

defines h(t)=(1+t)1/tImage and then computes h(1)=2Image. Because division by 0 is always undefined, h(0)Image is undefined.

Image

However, h(t)Image is defined for all t>0Image. In the following, we use RandomReal together with Table to generate 6 random numbers “close” to 0 and name the resulting list t1. Because we are using RandomReal, your results will almost certainly differ from those here.

RandomReal[{a,b}] returns a random real number between a and b; RandomReal[{a,b},n] returns n random real numbers between a and b.

t1=Table[RandomReal[{0,10(n)}],{n,0,5}]Image

{ 0.399958 , 0.0392505 , 0.00674767 , 0.00018339 , 0.0000760418 , 4.3836670778376435 ` * -6 } Image

We then use Map to compute h(t)Image for each of the values in the list t1.

From the result, we might correctly deduce that limt0+(1+t)1/t=eImage. We can compute the limit with the Limit function, which will be discussed in more detail in Chapter 3. For now, we remark that Limit[f[x],x->a] attempts to compute limxaf(x)Image.

Limit [ h [ t ] , t 0 ] Image

e

In each of these cases, do not forget to include the “blank” (or underline) (_) on the left-hand side of the equals sign and right bracket in the definition of each function. Remember to always include arguments of functions in square brackets.

Example 2.15

Entering

Including a semi-colon at the end of a command suppresses the resulting output because Mathematica names (or, assigns) the function a name but does not evaluate the function after the assignment.

Clear [ f ] Image

f [ 0 ] = 1 ; Image

f [ 1 ] = 1 ; Image

f [ n _ ] := f [ n 1 ] + f [ n 2 ] Image

defines the recursively-defined function defined by f(0)=1Image, f(1)=1Image, and f(n)=f(n1)+f(n2)Image. For example, f(2)=f(1)+f(0)=1+1=2Image; f(3)=f(2)+f(1)=2+1=3Image. We use Table to create a list of ordered pairs (n,f(n))Image for n=0,1,,10Image.

The fnImage we have defined here return the Fibonacci number FnImage. Fibonacci[n] also returns the nth Fibonacci number.

Table [ { n , f [ n ] } , { n , 0 , 10 } ] Image

{ { 0 , 1 } , { 1 , 1 } , { 2 , 2 } , { 3 , 3 } , { 4 , 5 } , { 5 , 8 } , { 6 , 13 } , { 7 , 21 } , { 8 , 34 } , { 9 , 55 } , { 10 , 89 } } Image

In the preceding examples, the functions were defined using each of the forms f[x_]:=... and f[x_]=.... As a practical matter, when defining “routine” functions with domains consisting of sets of real numbers and ranges consisting of sets of real numbers, either form can be used. Defining a function using the form f[x_]=... instructs Mathematica to define f and then compute and return f[x] (immediate assignment); defining a function using the form f[x_]:=... instructs Mathematica to define f. In this case, f[x] is not computed and, thus, Mathematica returns no output (delayed assignment). The form f[x_]:=... should be used when Mathematica cannot evaluate f[x] unless x is a particular value, as with recursively-defined functions or piecewise-defined functions which we will discuss shortly.

Generally, if attempting to define a function using the form f[x_]=... produces one or more error messages, use the form f[x_]:=... instead.

To define piecewise-defined functions, we usually use Condition (/;) as illustrated in the following example. In simple situations we take advantage of Piecewise.

Image

Example 2.16

Entering

Clear [ f ] Image

f [ t _ ] := Sin [ 1 / t ] /; t > 0 Image

defines f(t)=sin(1/t)Image for t>0Image. Entering

f [ 1 / ( 10 Pi ) ] Image

0

is evaluated because 1/(10π)>0Image. However, both of the following commands are returned unevaluated. In the first case, −1 is not greater than 0 (f(t)Image is not defined for t0Image). In the second case, Mathematica does not know the value of a so it cannot determine if it is or is not greater than 0.

f [ 1 ] Image

f [ 1 ] Image

f [ a ] Image

f [ a ] Image

Entering

f[t_]:=t/;t0Image

defines f(t)=tImage for t0Image. Now, the domain of f(t)Image is all real numbers. That is, we have defined the piecewise-defined function

f ( t ) = { sin ( 1 / t ) , t > 0 t , t 0 .

We can now evaluate f(t)Image for any real number t.

f [ 2 / ( 5 Pi ) ] Image

1

f [ 0 ] Image

0

f [ 10 ] Image

10

However, f(a)Image still returns unevaluated because Mathematica does not know if a0Image or if a>0Image.

f [ a ] Image

f [ a ] Image

Recursively-defined functions are handled in the same way. The following example shows how to define a periodic function.

We will discuss additional ways to define, manipulate, and evaluate functions as needed. However, Mathematica's extensive programming language allows a great deal of flexibility in defining functions, many of which are beyond the scope of this text. These powerful techniques are discussed in detail in texts like Wellin's Programming with Mathematica: An Introduction [18], Gray's Mastering Mathematica: Programming Methods and Applications [9], Wellin's Essentials of Programming in Mathematica [19] and Maeder's The Mathematica Programmer II and Programming in Mathematica [12,13].

2.3 Graphing Functions, Expressions, and Equations

One of the best features of Mathematica is its graphics capabilities. In this section, we discuss methods of graphing functions, expressions, and equations and several of the options available to help graph functions.

2.3.1 Functions of a Single Variable

The commands

Plot[f[x],{x,a,b}] and Plot[f[x],{x,a,x1,x2,...,xn,b}]

graph the function y=f(x)Image on the intervals [a,b]Image and [a,x1)(x1,x2)(xn,b]Image, respectively. Mathematica returns information about the basic syntax of the Plot command with ?Plot or use the Documentation Center to obtain detailed information regarding Plot.

Image

Remember that every Mathematica object can be assigned a name, including graphics. Use Show to combine graphics together. Show[p1,p2,...,pn] displays the graphics p1, p2, ..., pn together. Show[GraphicsRow[{p1,p2,...,pn}]] displays the graphics p1, p2, ..., pn in a row. Show[GraphicsColumn[{p1,p2,...,pn}]] displays the graphics p1, p2, ..., pn in a column.

Show[GraphicsGrid[{{p11,p12,...,p1m},{p21,p22,...,p2m},...,
    {pn1,pn2,...,pnm}}]]

displays the graphics p11, pp12, ..., pnm in an n-row by m-column array.

Example 2.18

Graph y=sinxImage and y=cosxImage for 2πx2πImage.

Solution

Entering

p1 = Plot [ Sin [ x ] , { x , 2 Pi , 2 Pi } ] Image

graphs y=sinxImage for 2πx2πImage and names the result p1. The plot is shown in Fig. 2.2 (a). With

Image
Figure 2.2 (a) y=sinxImage for −2π ⩽ x ⩽ 2π. (b) A “reddish” plot of y=cosxImage for −2π ⩽ x ⩽ 2π. (c) Combining two graphics with Epilog and Inset.

p1b = Plot [ Cos [ x ] , { x , 2 Pi , 2 Pi } , Image

ColorFunction “ValentineTones” , Image

PlotStyle Thickness [ . 025 ] ] Image

we create a slightly thicker plot of y=cosxImage and shade the plot using the ValentineTones color gradient. See Fig. 2.2 (b).

Show[p1,p2,...,pn] shows the graphics p1, ..., pn. You can also use Show to rerender graphics. Using Show with the Epilog option together with Inset, we place a small version of the cosine plot in the sine plot. See Fig. 2.2 (c).

p1c = Show [ p1 , Image

Epilog Inset [ p1b , { 0 , 0 } , Automatic , 5 ] ] Image

Multiple graphics can be shown in rows, columns, or grids using GraphicsRow (to display several graphics as a row), GraphicsColumn (to display several graphics as a column) or GraphicsGrid (to display several graphics as a grid, array or matrix), respectively. Thus,

Show [ GraphicsRow [ { p1 , p1b , p1c } ] ] Image

generates Fig. 2.2.

A different way of approaching the problem would be to graph the two functions to scale and then show them together. Include a semi-colon (;) at the end of a command to suppress the result. In the following, we graph y=sinxImage in blue and y=cosxImage in red, naming the plots p1 and p2 respectively. Neither is displayed because we include a semi-colon at the end of each command. We then combine the graphics with Show. The result is shown to scale because the option AspectRatio->Automatic is included in the Show command (see Fig. 2.3).

Image
Figure 2.3 y=sinxImage in blue and y=cosxImage in red graphed to scale on the interval [−2π,2π]. (For interpretation of the colors in this figure, the reader is referred to the web version of this chapter.)

p1 = Plot [ Sin [ x ] , { x , 2 Pi , 2 Pi } , PlotStyle Blue ] ; Image

p2 = Plot [ Cos [ x ] , { x , 2 Pi , 2 Pi } , PlotStyle Red ] ; Image

Show [ p1 , p2 , PlotRange { { 2 Pi , 2 Pi } , Automatic } , Image

AxesLabel { x , y } , Image

AspectRatioAutomatic]Image □

Be careful when graphing functions with discontinuities. Often Mathematica will catch discontinuities. In other cases, it doesn't and you might need to use the Exclusions option to generate a more accurate plot.

Example 2.19

Graph s(t)Image for 0t5Image where s(t)=1Image for 0t<1Image and s(t)=1+s(t1)Image for t1Image.

Solution

After defining s(t)Image,

Clear [ s ] Image

s [ t _ ] := 1 /; 0 t < 1 ; Image

s [ t _ ] := 1 + s [ t 1 ] /; t 1 ; Image

we use Plot to graph s(t)Image for 0t5Image in Fig. 2.4 (a).

Image
Figure 2.4 (a) s(t)=1 + s(t − 1), 0 ⩽ t ⩽ 5. (b) Catching the discontinuities.

p1 = Plot [ s [ t ] , { t , 0 , 5 } , AspectRatio Automatic , Image

AxesLabel{t,s}]Image

Of course, Fig. 2.4 (a) is not completely precise: vertical lines are never the graphs of functions. In this case, discontinuities occur at t=1,2,3,4Image, and 5. If we were to redraw the figure by hand, we would erase the vertical line segments, and then for emphasis place open dots at (1,1)Image, (2,2)Image, (3,3)Image, (4,4)Image, and (5,5)Image and then closed dots at (1,2)Image, (2,3)Image, (3,4)Image, (4,5)Image, and (5,6)Image. In cases like this where Plot does not automatically detect discontinuities you can specify them with Exclusions. See Fig. 2.4 (b).

p2 = Plot [ s [ t ] , { t , 0 , 5 } , AspectRatio Automatic , Image

Exclusions { 1 , 2 , 3 , 4 } , AxesLabel { t , s } ] Image

Show [ GraphicsRow [ { p1 , p2 } ] ] Image

To fine-tune graphics, use the Drawing Tools and Graphics Inspector palettes, which are accessed under Graphics in the menu.

Image

 □

Entering Options[Plot] lists all Plot options and their default values. The most frequently used options include PlotStyle, DisplayFunction, AspectRatio, PlotRange, PlotLabel, and AxesLabel.

1. PlotStyle controls the color and thickness of a plot. PlotStyle-> GrayLevel[w], where 0w1Image instructs Mathematica to generate the plot in GrayLevel[w]. GrayLevel[0] corresponds to black and GrayLevel[1] corresponds to white. Color plots can be generated using RGBColor. RGBColor[1,0,0] corresponds to red, RGBColor[0,1,0] corresponds to green, and RGBColor[0,0,1] corresponds to blue. You can also use any of the named colors listed on the Color Schemes palette. If you prefer CMYK color coding, use CMYKColor[c,m,y,k] to help achieve the desired effects in your graphics.

Image

PlotStyle->Dashing[{a1,a2,...,an}] indicates that successive segments be dashed with repeating lengths of a1Image, a2Image, …, anImage. The thickness of the plot is controlled with PlotStyle->Thickness[w], where w is the fraction of the total width of the graphic. For a single plot, the PlotStyle options are combined with PlotStyle->{{option1, option2, ... , optionn}].

2.  A plot is not displayed when the option DisplayFunction-> Identity is included or when a semi-colon (;) is included at the end of the command. Including the option DisplayFunction->$ DisplayFunction in Show or Plot commands instructs Mathematica to display graphics.

3.  The ratio of height to width of a plot is controlled by AspectRatio. The default is 1/GoldenRatio. Generally, a plot is drawn to scale when the option AspectRatio-> Automatic is included in the Plot or Show command.

4.  PlotRange controls the horizontal and vertical axes. PlotRange->{c,d} specifies that the vertical axis displayed corresponds to the interval cydImage while PlotRange->{{a,b}, {c,d}} specifes that the horizontal axis displayed corresponds to the interval axbImage and that the vertical axis displayed corresponds to the interval cydImage.

5.  PlotLabel->"titleofplot" labels the plot titleofplot.

6.  AxesLabel->{"xaxislabel","yaxislabel"} labels the x-axis with xaxislabel and the y-axis with yaxislabel.

Example 2.20

Graph y=sinxImage, y=cosxImage, and y=tanxImage together with their inverse functions.

Solution

In p2 and p3, we use Plot to graph y=sin1xImage and y=xImage, respectively. Neither plot is displayed because we include a semi-colon at the end of the Plot commands. p1, p2, and p3 are displayed together with Show in Fig. 2.5 (a). The plot is shown to scale; the graph of y=sinxImage is in black, y=sin1xImage is in gray, and y=xImage is dashed.

Image
Figure 2.5 (a) y=sinxImage, y=sin1xImage, and y = x. (b) y=cosxImage, y=cos1xImage, and y = x.

p1 = Plot [ Sin [ x ] , { x , 2 Pi , 2 Pi } , Image

PlotStyle Black ] ; Image

p2 = Plot [ ArcSin [ x ] , { x , 1 , 1 } , Image

PlotStyle Gray ] ; Image

p3 = Plot [ x , { x , 2 Pi , 2 Pi } , Image

PlotStyle { { Black , Dashing [ { 0.01 } ] } } ] ; Image

t1a = Show [ p1 , p2 , p3 , PlotRange { 2 Pi , 2 Pi } , Image

AspectRatio Automatic , AxesLabel { x , y } ] Image

The command Plot[{f1[x],f2[x],...,fn[x]},{x,a,b}] plots f1(x)Image, f2(x)Image, …, fn(x)Image together for axbImage. Simple PlotStyle options are incorporated with

PlotStyle->{option1,option2,...,optionn

where optioni corresponds to the plot of fi(x)Image. Multiple options are incorporated using

PlotStyle->{{options1},{options2},...,{optionsn}}

where optionsi are the options corresponding to the plot of fi(x)Image.

In the following, we use Plot to graph y=cosxImage, y=cos1xImage, and y=xImage together. The plot in Fig. 2.5 (b) is shown to scale; the graph of y=cosxImage is in black, y=cos1xImage is in gray, and y=xImage is dashed.

p1 = Plot [ Cos [ x ] , { x , 0 , 2 Pi } , Image

PlotStyle Black ] ; Image

p2 = Plot [ ArcCos [ x ] , { x , 1 , 1 } , Image

PlotStyle Gray ] ; Image

p3 = Plot [ x , { x , 2 Pi , 2 Pi } , Image

PlotStyle { { Black , Dashing [ { 0.01 } ] } } ] ; Image

t1b = Show [ p1 , p2 , p3 , PlotRange { { Pi , 2 Pi } , { Pi , 2 Pi } } , Image

AspectRatio Automatic , AxesLabel { x , y } ] Image

We use the same approach to graph y=tanxImage, y=tan1xImage, and y=xImage in Fig. 2.6.

Image
Figure 2.6 y=tanxImage, y=tan1xImage, and y = x.

p1 = Plot [ Tan [ x ] , { x , Pi / 2 , Pi / 2 } , Image

PlotStyle Black ] ; Image

p2 = Plot [ ArcTan [ x ] , { x , 2 Pi , 2 Pi } , Image

PlotStyle Gray ] ; Image

p3 = Plot [ x , { x , 2 Pi , 2 Pi } , Image

PlotStyle { { Black , Dashing [ { 0.01 } ] } } ] ; Image

t1c = Show [ p1 , p2 , p3 , PlotRange { { 2 Pi , 2 Pi } , { 2 Pi , 2 Pi } } , Image

AspectRatio Automatic , AxesLabel { x , y } ] Image

Use Show together with GraphicsRow to display graphics in rectangular arrays. Entering

Show [ GraphicsRow [ { p2 , p3 , t1c } ] ] Image

shows the three plots p2, p3, and t1c in a row as shown in Fig. 2.7. □

Image
Figure 2.7 The elementary trigonometric functions and their inverses.

Solution

When using Mathematica, Exp[x] represents the function y=exImage. Similarly, Log[x] represents the natural logarithm function, y=lnxImage.

After defining f(x)Image and g(x)Image,

f(x)Image and g(x)Image are not returned because a semi-colon is included at the end of each command.

f [ x _ ] = Exp [ x ] ; Image

g [ x _ ] = Log [ x ] ; Image

f [ g [ x ] ] Image

x

Simplify [ g [ f [ x ] ] ] Image

Log [ e x ] Image

we compute and simplify the compositions f(g(x))Image and g(f(x))Image. Because both results simplify to f(g(x))=g(f(x))=xImage, f(x)Image and g(x)Image are inverse functions. Observe that Simplify does not automatically reduce ln(ex)=xImage. To do so, we use PowerExpand.

PowerExpand [ g [ f [ x ] ] ] Image

x

To see that the graphs of f(x)Image and g(x)Image are symmetric about the line y=xImage, we use Plot to graph f(x)Image, g(x)Image, and y=xImage together in Fig. 2.8. Because Tooltip is being applied to the set of functions being plotted, you can identify each curve by sliding the cursor over the curve: when the cursor is placed over a curve, Mathematica displays its definition. In the Plot command, observe how we are able to graph all three functions, f(x)=exImage, g(x)=lnxImage, and y=xImage together as well as control how each graph is plotted using the PlotStyle option. The range (y-axis) displayed is controlled using the PlotRange option. The option AspectRatio->1 instructs Mathematica to display the axes in a 11Image (square) ratio. In this case, because the domains and ranges are the same, the resulting plot is drawn to scale. If the domains and ranges are not the same and you wish to have the plot displayed to scale, use the option AspectRatio->Automatic.

Image
Figure 2.8 f(x)=ex in black, g(x)=lnxImage in gray, and y = x dashed in light gray.

Plot [ Tooltip [ { f [ x ] , g [ x ] , x } ] , { x , 10 , 10 } , Image

PlotStyle { { Black } , { Gray } , { LightGray , Dashing [ . 01 ] } } , Image

PlotRange { 10 , 10 } , AspectRatio 1 ] Image

In the plot, observe that the graphs of f(x)=exImage and g(x)=lnxImage are symmetric about the line y=xImage. The plot also illustrates that the domain and range of a function and its inverse are interchanged: f(x)Image has domain (,)Image and range (0,)Image; g(x)Image has domain (0,)Image and range (,)Image. □

For repeated compositions of a function with itself, Nest[f,x,n] computes the composition

( f f f f ) n  times ( x ) = ( f ( f ( f ) ) ) n  times ( x ) = f n ( x ) .

Example 2.22

Graph f(x)Image, f10(x)Image, f20(x)Image, f30(x)Image, f40(x)Image, and f50(x)Image if f(x)=sinxImage for 0x2πImage.

When graphing functions involving odd roots, Mathematica's results may be surprising to the beginner. The key is to use the Surd function or remember that Mathematica follows the order of operations exactly and understand that without restrictions on x, x2=|x|Image.

Example 2.23

Graph y=x1/3(x2)2/3(x+1)4/3Image.

Solution

Entering

p1 = Plot [ x ( 1 / 3 ) ( x 2 ) ( 2 / 3 ) ( x + 1 ) ( 4 / 3 ) , Image

{ x , 3 , 5 } , PlotRange { 4 , 4 } , Image

AspectRatio Automatic , PlotStyle Black , Image

PlotLabel “(a)” ] Image

does not produce the graph we expect (see Fig. 2.10 (a)) because many of us consider y=x1/3(x2)2/3(x+1)4/3Image to be a real-valued function with domain (,)Image. Generally, Mathematica does return a real number when computing the odd root of a negative number. For example, x3=1Image has three solutions

Image
Figure 2.10 (a), (b) and (c) three plots of y = x1/3(x2)2/3(x+1)4/3.

Solve is discussed in more detail in the next section.

Clear [ x ] Image

s1 = Solve [ x 3 == 1 ] Image

{ { x 1 } , { x ( 1 ) 1 / 3 } , { x ( 1 ) 2 / 3 } } Image

N [ s1 ] Image

{ { x 1 . } , { x 0.5 + 0.866025 i } , { x 0.5 0.866025 i } } Image

When computing an odd root of a negative number, Mathematica has many choices (as illustrated above) and chooses a root with positive imaginary part – the result is not a real number.

( 1 ) ( 1 / 3 ) // N Image

0.5 + 0.866025 i Image

To obtain real values when computing odd roots of negative numbers, first let sign(x)={x/|x|, if x0,0, if x=0.Image Sign[x] returns sign(x)Image. Then, for the reduced fraction n/mImage with m odd, xn/m={sign(x)|x|n/m, if n is odd |x|n/m, if n is even Image. See Fig. 2.10 (b).

p2 = Plot [ Sign [ x ] Abs [ x ] ( 1 / 3 ) Abs [ x 2 ] ( 2 / 3 ) Abs [ x + 1 ] ( 4 / 3 ) , Image

{ x , 3 , 5 } , PlotRange { 4 , 4 } , PlotStyle Black , Image

AspectRatio Automatic , PlotLabel “(b)” ] Image

Alternatively, use the Surd function to obtain the desired results. Surd[x,n] returns the real-valued nth root of x.

Image

p3 = Plot [ Surd [ x , 3 ] Surd [ ( x 2 ) 2 , 3 ] Surd [ ( x + 1 ) 4 , 3 ] , Image

{ x , 3 , 5 } , PlotRange { 4 , 4 } , Image

AspectRatio Automatic , PlotStyle Black , Image

PlotLabel “(c)” ] Image

All three graphics are shown in Fig. 2.10 using Show together with GraphicsRow.

Show[GraphicsRow[{p1,p2,p3}]]Image □

The command

ListPlot[{{x1,y1},{x2,y2},...,{xn,yn}}]

plots the list of points {(x1,y1),(x2,y2),,(xn,yn)}Image. The size of the points in the resulting plot is controlled with the option PlotStyle->PointSize[w], where w is the fraction of the total width of the graphic. For two-dimensional graphics, the default value is 0.008. The command

ListPlot[{y1,y2,..,yn}]

plots the list of points {(1,y1),(2,y2),,(n,yn)}Image. To connect successive points with line segments, use the command ListLinePlot.

ListLinePlot[{x1,y1},{x2,y2},...,{xn,yn}}]

connects the consecutive points (x1,y1)Image, (x2,y2)Image, …, (xn,yn)Image with line segments.

Example 2.24

Plot the “principal trigonometric values.”

Solution

The “principal trigonometric values” are usually interpreted to mean the values of the functions sinxImage and cosxImage at the angles θ=0Image, π/6Image, π/4Image, π/3Image, π/2Image, 2π/3Image, 3π/4Image, 5π/6Image, π, 7Pi/6Image, 5π/4Image, 4π/3Image, 3π/2Image, 5π/3Image, 7π/4Image, 11π/6Image, and 2π.

We begin by defining these points in prinvals. These points are then plotted in lp1 with ListPlot. We illustrate the use of the PlotStyle, AspectRatio, and PlotLabel options.

prinvals = { { 1 , 0 } , { 1 / 2 , Sqrt [ 3 ] / 2 } , { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } , { Sqrt [ 3 ] / 2 , 1 / 2 } , Image

{ 0 , 1 } , { 1 / 2 , Sqrt [ 3 ] / 2 } , { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } , { Sqrt [ 3 ] / 2 , 1 / 2 } , Image

{ 1 , 0 } , { 1 / 2 , Sqrt [ 3 ] / 2 } , { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } , { Sqrt [ 3 ] / 2 , 1 / 2 } , Image

{ 0 , 1 } , { 1 / 2 , Sqrt [ 3 ] / 2 } , { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } , { Sqrt [ 3 ] / 2 , 1 / 2 } } ; Image

lp1 = ListPlot [ prinvals , AspectRatio Automatic , Image

PlotStyle { Black , PointSize [ . 025 ] } , Image

PlotLabel “(a)” ] Image

Next, we define prinlines to be a set of ordered pairs that will draw line segments between connected points. These lines are graphed by using Map to apply ListLinePlot to each ordered pair in prinlines. Generally, Map[f,list] applies the function f to each element of list. We use Show and illustrate the use of the AspectRatio and PlotLabel options to graph these line segments in lp2.

prinlines = { { { 1 , 0 } , { 1 , 0 } } , { { 1 / 2 , Sqrt [ 3 ] / 2 } , { 1 / 2 , Sqrt [ 3 ] / 2 } } , Image

{ { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } , { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } } , Image

{ { Sqrt [ 3 ] / 2 , 1 / 2 } , { Sqrt [ 3 ] / 2 , 1 / 2 } } , Image

{ { 0 , 1 } , { 0 , 1 } } , { { 1 / 2 , Sqrt [ 3 ] / 2 } , { 1 / 2 , Sqrt [ 3 ] / 2 } } , Image

{ { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } , { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } } , Image

{ { Sqrt [ 3 ] / 2 , 1 / 2 } , { Sqrt [ 3 ] / 2 , 1 / 2 } } } ; Image

prinlines2 = Map [ ListLinePlot [ # , Image

PlotStyle { { GrayLevel [ . 5 ] , Dashing [ . 01 ] } } ] & , prinlines ] ; Image

lp2 = Show [ prinlines2 , AspectRatio Automatic , Image

PlotLabel “(b)” ] Image

In lp3, we use Plot to graph the functions y=1x2Image and y=1x2Image. In the color version of this text, the CMYKColor code corresponds to a deep red. The graph is drawn to scale because we include the option AspectRatio->Automatic.

lp3 = Plot [ { Sqrt [ 1 x 2 ] , Sqrt [ 1 x 2 ] } , { x , 1 , 1 } , Image

PlotStyle CMYKColor [ 0 , 0.89 , 0.94 , 0.28 ] , Image

AspectRatio Automatic , PlotLabel “(c)” ] Image

Finally, we use Show to display all four graphics together in lp4.

lp4 = Show [ lp1 , lp2 , lp3 , PlotLabel “(d)” ] Image

The four images are displayed as a graphics grid using Show together with GraphicsGrid in Fig. 2.11.

Image
Figure 2.11 (from left to right) (a)–(d) The four plots lp1, lp2, lp3, and lp4 combined into a single graphic.

Show[GraphicsGrid[{{lp1,lp2},{lp3,lp4}}]]Image □

A comprehensive discussion of Mathematica's extensive graphics capabilities cannot be reasonably covered in a single section of a text so our approach is to address issues that might of interest or present a different point of view to the beginning user of Mathematica. In the previous example, we saw that x3+1=0Image has three solutions, two of which are complex. To visualize this graphically, observe that the zeros of z3+1=0Image are the level curves of f(x,y)=|(x+iy)3+1|Image (x, y real) corresponding to 0. In a plot of f(x,y)Image, the solutions are the zeros. Shortly we will discuss ContourPlot and Plot3D. For now, we remark that

cp1 = ContourPlot [ Abs [ ( x + I y ) 3 + 1 ] , { x , 2 , 1 } , { y , 3 / 2 , 3 / 2 } , Image

Contours 30 , Axes True ] Image

p13d = Plot3D [ Abs [ ( x + I y ) 3 + 1 ] , { x , 2 , 1 } , { y , 3 / 2 , 3 / 2 } , Image

Axes True , PlotRange { 0 , 15 } , MeshFunctions ( # 3 & ) , Image

Mesh 35 ] Image

Show [ GraphicsRow [ { cp1 , p13d } ] ] Image

generates several level curves of f(x,y)Image (Fig. 2.12 (a)) and a 3-d plot of f(x,y)Image (Fig. 2.12 (b)) that help us see the zeros of the original equation. In the 3-d plot, note how we use the MeshFunctions option to generate contours on the 3-d plot.

Image
Figure 2.12 (a) Contour plot of f(x,y). (b) 3-d plot of f(x,y).

2.3.2 Parametric and Polar Plots in Two Dimensions

For parametrically defined functions, x=x(t)Image, y=y(t)Image, atbImage, use ParametricPlot. ParametricPlot[{x[t],y[t]},{t,a,b}] attempts to graph the parametrically defined functions x=x(t)Image, y=y(t)Image, atbImage.

To graph the polar function, r=f(θ)Image, use PolarPlot.

PolarPlot[f[theta],{theta,alpha,beta}]

attempts to graph r=f(θ)Image for αθβImage.

To obtain the θ symbol with Mathematica on a Mac, press the esc key, type “theta”, and then press the esc key again. Use the same process to obtain other symbols, such as α, β, and so on.

Image

Example 2.25

The Unit Circle

The unit circle is the set of points (x,y)Image exactly 1 unit from the origin, (0,0)Image, and, in rectangular coordinates, has equation x2+y2=1Image. The unit circle is the classic example of a relation that is neither a function of x nor a function of y. The top half of the unit circle is given by y=1x2Image and the bottom half is given by y=1x2Image.

p1 = Plot [ { Sqrt [ 1 x 2 ] , Sqrt [ 1 x 2 ] } , { x , 1 , 1 } , Image

PlotRange { { 3 / 2 , 3 / 2 } , { 3 / 2 , 3 / 2 } } , Image

AspectRatio Automatic , PlotStyle CMYKColor [ 1.00 , . 70 , 0 , . 75 ] , Image

PlotLabel “Georgia Southern Blue” ] Image

Each point (x,y)Image on the unit circle is a function of the angle, t, that subtends the x-axis, which leads to a parametric representation of the unit circle, {x=cost,y=sint,0t2πImage, which we graph with ParametricPlot.

p2 = ParametricPlot [ { Cos [ t ] , Sin [ t ] } , { t , 0 , 2 Pi } , Image

PlotRange { { 3 / 2 , 3 / 2 } , { 3 / 2 , 3 / 2 } } , Image

AspectRatio Automatic , PlotStyle CMYKColor [ . 40 , . 43 , . 84 , . 08 ] , Image

PlotLabel “Georgia Southern Gold” ] Image

Using the change of variables x=rcostImage and y=rsintImage to convert from rectangular to polar coordinates, a polar equation for the unit circle is r=1Image. We use PolarPlot to graph r=1Image.

p3 = PolarPlot [ 1 , { t , 0 , 2 Pi } , PlotRange { { 3 / 2 , 3 / 2 } , { 3 / 2 , 3 / 2 } } , Image

AspectRatio Automatic , PlotStyle CMYKColor [ . 97 , . 33 , . 78 , . 24 ] , Image

PlotLabel “Ohio University Green” ] Image

We display p1, p2, and p3 side-by-side using Show together with GraphicsRow in Fig. 2.13. Of course, they all look the same, just in different colors (or shades of gray if you are reading the printed version of this text).

Image
Figure 2.13 The unit circle generated with Plot, ParametricPlot, and PolarPlot.

Show [ GraphicsRow [ { p1 , p2 , p3 } ] ] Image

To illustrate several other features of Mathematica's extensive graphics capability, we elaborate on the example and construct a unit circle that can be used as a handout in a trigonometry, pre-calculus, or calculus class.

In p1, we graph the unit circle.

p1 = ParametricPlot [ { Cos [ t ] , Sin [ t ] } , { t , 0 , 2 Pi } , Image

PlotStyle Black ] Image

Next, we use ParametricPlot to connect the principal value points on the unit circle with line segments by observing that the parametric equations x=(1t)a+tcImage, y=(1t)b+tdImage, 0t1Image graphs a line segment connecting (a,b)Image and (c,d)Image.

p2a = ParametricPlot [ { { 1 / 2 , Sqrt [ 3 ] / 2 } ( t 1 ) , Image

{ 1 / 2 , Sqrt [ 3 ] / 2 } ( t 1 ) } , Image

{ t , 0 , 1 } , PlotStyle { { Black , Dashing [ { . 01 } ] } } , Image

PlotLabel“(b)”]Image

p2b = ParametricPlot [ { { Sqrt [ 3 ] / 2 , 1 / 2 } ( t 1 ) , Image

{ Sqrt [ 3 ] / 2 , 1 / 2 } ( t 1 ) } , Image

{ t , 0 , 1 } , PlotStyle { { Black , Dashing [ { . 01 } ] } } ] Image

p2c = ParametricPlot [ { { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } ( t 1 ) , Image

{ 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } ( t 1 ) } , Image

{ t , 0 , 1 } , PlotStyle { { Black , Dashing [ { . 01 } ] } } ] Image

p2d = ParametricPlot [ { { 1 / 2 , Sqrt [ 3 ] / 2 } ( t 1 ) , Image

{ 1 / 2 , Sqrt [ 3 ] / 2 } ( t 1 ) } , Image

{ t , 0 , 1 } , PlotStyle { { Black , Dashing [ { . 01 } ] } } ] Image

p2e = ParametricPlot [ { { Sqrt [ 3 ] / 2 , 1 / 2 } ( t 1 ) , Image

{ Sqrt [ 3 ] / 2 , 1 / 2 } ( t 1 ) } , Image

{ t , 0 , 1 } , PlotStyle { { Black , Dashing [ { . 01 } ] } } ] Image

p2f = ParametricPlot [ { { 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } ( t 1 ) , Image

{ 1 / Sqrt [ 2 ] , 1 / Sqrt [ 2 ] } ( t 1 ) } , Image

{ t , 0 , 1 } , PlotStyle { { Black , Dashing [ { . 01 } ] } } ] Image

q1 = Show [ p1 , p2a , p2b , p2c , p2f , p2e , p2d ] Image

Next, we use the graphics primitive Text to label the principal values on the unit circle.

Image

ta = Graphics [ Text [ "( 1 2 , 3 2 )" , { 1 / 2 + . 1 , Sqrt [ 3 ] / 2 + . 1 } ] ] ; Image

tb = Graphics [ Text [ "( 3 2 , 1 2 )" , { Sqrt [ 3 ] / 2 + . 1 , 1 / 2 + . 1 } ] ] ; Image

tc = Graphics [ Text [ "( 1 2 , 1 2 )" , { 1 / Sqrt [ 2 ] + . 1 , 1 / Sqrt [ 2 ] + . 1 } ] ] ; Image

td = Graphics [ Text [ "(- 1 2 , 3 2 )" , { 1 / 2 . 1 , Sqrt [ 3 ] / 2 + . 1 } ] ] ; Image

te = Graphics [ Text [ "(- 3 2 , 1 2 )" , { Sqrt [ 3 ] / 2 . 1 , 1 / 2 + . 1 } ] ] ; Image

tf = Graphics [ Text [ "(- 1 2 , 1 2 )" , { 1 / Sqrt [ 2 ] . 1 , 1 / Sqrt [ 2 ] + . 1 } ] ] ; Image

tg = Graphics [ Text [ "(- 1 2 ,- 3 2 )" , { 1 / 2 . 1 , Sqrt [ 3 ] / 2 . 1 } ] ] ; Image

th = Graphics [ Text [ "(- 3 2 ,- 1 2 )" , { Sqrt [ 3 ] / 2 . 1 , 1 / 2 . 1 } ] ] ; Image

ti = Graphics [ Text [ "(- 1 2 ,- 1 2 )" , { 1 / Sqrt [ 2 ] . 1 , 1 / Sqrt [ 2 ] . 1 } ] ] ; Image

tj = Graphics [ Text [ "( 1 2 ,- 3 2 )" , { 1 / 2 + . 1 , Sqrt [ 3 ] / 2 . 1 } ] ] ; Image

tk = Graphics [ Text [ "( 3 2 ,- 1 2 )" , { Sqrt [ 3 ] / 2 + . 1 , 1 / 2 . 1 } ] ] ; Image

tl = Graphics [ Text [ "( 1 2 ,- 1 2 )" , { 1 / Sqrt [ 2 ] + . 1 , 1 / Sqrt [ 2 ] . 1 } ] ] ; Image

Our last step is to combine these graphics objects together and show them as a single image with Show.

Show [ q1 , ta , tb , tc , td , te , tf , tg , th , ti , tj , tk , tl , Image

PlotLabel “The Unit Circle” ] Image

In the following example, the equations of the parametrically defined functions involve integrals.

Remark 2.2

Topics from calculus are discussed in Chapter 3. For now, we state that Integrate[f[x],{x,a,b}] attempts to evaluate abf(x)dxImage.

Rather than explicitly stating the color as in the previous example, we use gradients from the Color Scheme palette to adjust the colors in the resulting graphics (see Fig. 2.14).

Image
Figure 2.14 A labeled unit circle. Within Mathematica, you can increase the size of the graphic by clicking and dragging on the bounding box so that the result fills a page that when printed is suitable for distribution to classes such as trigonometry, pre-calculus, and calculus classes.

Example 2.26

Cornu Spiral

The Cornu spiral (or clothoid) (see [8] and [17]) has parametric equations

x ( t ) = 0 t sin ( 1 2 u 2 ) d u and y ( t ) = 0 t cos ( 1 2 u 2 ) d u .

Graph the Cornu spiral.

Solution

We begin by defining x and y. Notice that Mathematica can evaluate these integrals, even though the results are in terms of the FresnelS and FresnelC functions, which are defined in terms of integrals:

F r e s n e l S [ t ] = 0 t sin ( π 2 u 2 ) d u and F r e s n e l C [ t ] = 0 t cos ( π 2 u 2 ) d u .

x [ t _ ] = Integrate [ Sin [ u 2 / 2 ] , { u , 0 , t } ] Image

π FresnelS [ t π ] Image

y [ t _ ] = Integrate [ Cos [ u 2 / 2 ] , { u , 0 , t } ] Image

πFresnelC[tπ]Image

We use ParametricPlot to graph the Cornu spiral in Fig. 2.15. The option AspectRatio->Automatic instructs Mathematica to generate the plot to scale; PlotLabel->"Cornu spiral" labels the plot. Rather than explicitly stating the color, we use gradients from the Color Scheme palette to adjust the color of the plot.

Image
Figure 2.15 Graphing the Cornu Spiral using different color gradients and thickness. Note that in the printed version of this text, all images will be in gray.

p1 = ParametricPlot [ { x [ t ] , y [ t ] } , { t , 10 , 10 } , AspectRatio Automatic , Image

PlotStyle Thickness [ . 01 ] , PlotLabel “Cornu Spiral” , Image

Frame True , FrameLabel { x , y } , ColorFunction Image

( ColorData [ “SouthwestColors” ] [ # 1 ] & ) ] Image

p2 = ParametricPlot [ { x [ t ] , y [ t ] } , { t , 10 , 10 } , AspectRatio Automatic , Image

PlotStyle Thickness [ . 05 ] , PlotLabel “Cornu Spiral” , Image

Frame True , FrameLabel { x , y } , ColorFunction Image

( ColorData [ “SolarColors” ] [ # 1 ] & ) ] Image

p3 = ParametricPlot [ { x [ t ] , y [ t ] } , { t , 10 , 10 } , AspectRatio Automatic , Image

PlotStyle Thickness [ . 025 ] , PlotLabel “Cornu Spiral” , Image

ColorFunction Image

( ColorData [ “DarkRainbow” ] [ # 1 ] & ) ] Image

We use Show together with GraphicsRow to display the three graphics together in Fig. 2.15.

Show[GraphicsRow[{p1,p2,p3}]]Image □

Observe that the graph of the polar equation r(θ)=f(θ)Image, αθβImage is the same as the graph of the parametric equations

x ( θ ) = f ( θ ) cos θ and  y ( θ ) = f ( θ ) sin θ , α θ β

so both ParametricPlot and PolarPlot can be used to graph polar equations.

Example 2.27

Graph (a) r=sin(8θ/7)Image, 0θ14πImage; (b) r=θcosθImage, 19π/2θ19π/2Image; (c) (“The Butterfly”) r=ecosθ2cos4θ+sin5(θ/12)Image, 0θ24πImage; and (d) (“The Lituus”) r2=1/θImage, 0.1θ10πImage.

Solution

For (a) and (b) we use ParametricPlot. First define r and then use ParametricPlot to generate the graph of the polar curve. No graphics are displayed because we place a semi-colon at the end of each command. We illustrate the use of various options, particularly the PlotStyle and PlotLabel options in each.

Clear [ r ] Image

r [ θ _ ] = Sin [ 8 θ / 7 ] ; Image

pp1 = ParametricPlot [ { r [ θ ] Cos [ θ ] , r [ θ ] Sin [ θ ] } , Image

{ θ , 0 , 14 Pi } , AspectRatio Automatic , Image

PlotStyle CMYKColor [ 0 , 1 , . 7 , . 1 ] , Image

PlotLabel “(a)-University of Georgia Red” ] Image

For (b), we use the option PlotRange->{{-30,30},{-30,30}} to indicate that the range displayed on both the vertical and horizontal axes corresponds to the interval [30,30]Image. To help assure that the resulting graphic appears “smooth”, we increase the number of points that Mathematica samples when generating the graph by including the option PlotPoints->200.

Clear [ r ] Image

r [ θ _ ] = θ Cos [ θ ] ; Image

pp2 = ParametricPlot [ { r [ θ ] Cos [ θ ] , r [ θ ] Sin [ θ ] } , Image

{ θ , 19 Pi / 2 , 19 Pi / 2 } , AspectRatio Automatic , Image

PlotRange { { 30 , 30 } , { 30 , 30 } } , Image

PlotStyle CMYKColor [ . 03 , 1 , . 63 , . 12 ] , Image

PlotLabel “(b)-The Ohio State University Red” ] Image

For (c) and (d), we use PolarPlot. Using standard mathematical notation, we know that sin5(θ/12)=(sin(θ/12))5Image. However, when defining r with Mathematica, be sure you use the form Sin(θ/12)̂5Image, not Sin̂5[θ/12]Image, which Mathematica will not interpret in the way intended.

Clear [ r ] Image

r [ θ _ ] = Exp [ Cos [ θ ] ] 2 Cos [ 4 θ ] + Sin [ θ / 12 ] 5 ; Image

pp3 = PolarPlot [ r [ θ ] , { θ , 0 , 24 Pi } , PlotPoints 200 , Image

PlotRange { { 4 , 5 } , { 4.5 , 4.5 } } , Image

AspectRatio Automatic , Image

PlotLabel “(c)-Oklahoma State Orange” , Image

PlotStyle CMYKColor [ 0 , . 63 , 1.0 , 0 ] ] Image

pp4 = PolarPlot [ { Sqrt [ 1 / θ ] , Sqrt [ 1 / θ ] } , { θ , 0.1 , 10 Pi } , Image

AspectRatio Automatic , PlotRange All , Image

PlotLabel “(d)-Harvey Mudd Gold” , Image

PlotStyle CMYKColor [ 0 , . 3 , 1 . , 0 ] ] Image

Finally, we use Show together with GraphicsGrid to display all four graphs as a graphics array in Fig. 2.16. pp1 and pp2 are shown in the first row; pp3 and pp4 in the second.

Image
Figure 2.16 Graphs of four polar equations.

Show[GraphicsGrid[{{pp1,pp2},{pp3,pp4}}]]Image □

2.3.3 Three-Dimensional and Contour Plots; Graphing Equations

An elementary function of two variables, z=f(x,y)=expressioninxandyImage, is typically defined using the form

f[x_,y_]=expression in x and y.

For delayed evaluation, use f[x_,y_]:=... rather than f[x_,y_]=... (immediate evaluation). Once a function has been defined, a basic graph is generated with Plot3D:

Plot3D[f[x,y],{x,a,b},{y,c,d}]

graphs f(x,y)Image for axbImage and cydImage.

For details regarding Plot3D and its options enter ?Plot3D or ??Plot3D or access the Documentation Center to obtain information about the Plot3D command, as we do here.

Image

Graphs of several level curves of z=f(x,y)Image are generated with

ContourPlot[f[x,y],{x,a,b},{y,c,d}].

A density plot of z=f(x,y)Image is generated with

DensityPlot[f[x,y],{x,a,b},{y,c,d}].

For details regarding ContourPlot (DensityPlot) and its options enter ?ContourPlot (?DensityPlot) or ??ContourPlot (??DensityPlot) or access the Documentation Center.

Image

Example 2.28

Let f(x,y)=x2yx4+4y2Image. (a) Calculate f(1,1)Image. (b) Graph f(x,y)Image and several contour plots of f(x,y)Image on a region containing the origin, (0,0)Image.

Solution

After defining f(x,y)Image, we evaluate f(1,1)=1/5Image.

Clear [ f ] Image

f [ x _ , y _ ] = x 2 y / ( x 4 + 4 y 2 ) ; Image

f [ 1 , 1 ] Image

1 5 Image

Next, we use Plot3D to graph f(x,y)Image for 1/2x1/2Image and 1/2y1/2Image in Fig. 2.17. We illustrate the use of the Axes, Boxed, PlotPoints, MeshFunctions, PlotStyle, and ColorFunction options.

Image
Figure 2.17 Three-dimensional plot of f(x,y): Upper left is the basic plot generated with Plot3D; in upper right, we use contour lines to determine the mesh; in lower left, we use the GrayTones color gradient to shade the plot; in lower right, we create a transparent plot with Opacity.

p1 = Plot3D [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Axes Automatic , Boxed False , PlotPoints 60 , Image

PlotLabel“(a)”]Image

Use MeshFunctions to modify the standard rectangular grid. In Fig. 2.17 (b), we use the level curves of the function for the grid.

To adjust the viewing angle of three-dimensional graphics, select the graphic and drag to the desired viewing angle.

p2 = Plot3D [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Axes Automatic , Boxed False , PlotPoints 60 , Image

MeshFunctions ( # 3 & ) , PlotLabel “(b)” ] Image

We use the GrayTones color gradient to shade the graph. (Fig. 2.17 (c))

p3 = Plot3D [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Axes Automatic , Boxed False , PlotPoints 60 , Image

MeshFunctions ( # 3 & ) , PlotLabel “(c)” , Image

ColorFunction { ColorData [ “GrayTones” ] [ # 3 ] & } ] Image

Use Opacity to make a “clear” plot. (See Fig. 2.17 (d).) We use Show together with GraphicsGrid to display all four plots together in Fig. 2.17.

p4 = Plot3D [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Axes Automatic , Boxed False , PlotPoints 60 , Image

MeshFunctions ( # 3 & ) , PlotLabel “(d)” , Image

ColorFunction { Opacity [ . 5 ] } ] Image

Show [ GraphicsGrid [ { { p1 , p2 } , { p3 , p4 } } ] ] Image

Four contour plots are generated with ContourPlot. The second through fourth illustrate the use of the PlotPoints, Frame, ContourShading, Axes, AxesOrigin, ColorFunction, and Contours options. (See Fig. 2.18.)

Image
Figure 2.18 Three contour plots of f(x,y).

cp1 = ContourPlot [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Contours 30 , PlotPoints 50 , Image

PlotLabel “(a)” ] Image

cp2 = ContourPlot [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Contours 30 , PlotPoints 50 , Image

ColorFunction ColorData [ “GrayTones” ] , Image

PlotLabel “(b)” ] Image

cp3 = ContourPlot [ f [ x , y ] , { x , 1 / 2 , 1 / 2 } , { y , 1 / 2 , 1 / 2 } , Image

Contours 30 , PlotPoints 50 , ContourShading False , Image

Frame False , Axes Automatic , AxesOrigin { 0 , 0 } , Image

AxesLabel { x , y } , ContourStyle Black , Image

PlotLabel“(c)”]Image

Show [ GraphicsRow [ { cp1 , cp2 , cp3 } ] ] Image

Fig. 2.18 shows the graphics array generated with the previous commands. With Mathematica 11, if you want to adjust your array, drag and move the objects within the graphic. □

With Mathematica 11, you can adjust the viewing angle of a three-dimensional graphics by selecting the graphic and dragging it to the desired position.

Image

Manually, use the ViewPoint option.

Image

Fig. 2.19 shows four different views of the graph of g(x,y)=xsiny+ysinxImage for 0x5πImage and 0y5πImage. The options AxesLabel, BoxRatios, ViewPoint, PlotPoints, Shading, and Mesh are also illustrated.

Image
Figure 2.19 Four different plots of g(x,y)=xsiny+ysinxImage for 0 ⩽ x ⩽ 5π.

Clear [ g ] Image

g [ x _ , y _ ] = x Sin [ y ] + y Sin [ x ] ; Image

p1 = Plot3D [ g [ x , y ] , { x , 0 , 5 Pi } , { y , 0 , 5 Pi } , Image

PlotPoints 60 , AxesLabel { x , y , z } , Image

PlotLabel “(a)” ] ; Image

p2 = Plot3D [ g [ x , y ] , { x , 0 , 5 Pi } , { y , 0 , 5 Pi } , Image

PlotPoints 60 , ViewPoint - > { 2.846 , 1.813 , 0.245 } , Image

Boxed False , BoxRatios { 1 , 1 , 1 } , Image

AxesLabel { x , y , z } , Image

PlotLabel “(b)” ] ; Image

p3 = Plot3D [ g [ x , y ] , { x , 0 , 5 π } , { y , 0 , 5 π } , Image

PlotPoints 60 , ViewPoint { 1.488 , 1.515 , 2.634 } , Image

AxesLabel { x , y , z } , ColorFunction ( White & ) , Image

PlotLabel “(c)” ] ; Image

p4 = Plot3D [ g [ x , y ] , { x , 0 , 5 Pi } , { y , 0 , 5 Pi } , Image

PlotPoints 60 , AxesLabel { x , y , z } , Image

Mesh False , BoxRatios { 2 , 2 , 3 } , Image

ViewPoint - > { 1.736 , 1.773 , 2.301 } , Image

PlotLabel “(d)” ] ; Image

Show [ GraphicsGrid [ { { p1 , p2 } , { p3 , p4 } } ] ] Image

ContourPlot is especially useful when graphing equations. The graph of the equation f(x,y)=CImage, where C is a constant, is the same as the contour plot of z=f(x,y)Image corresponding to C. That is, the graph of f(x,y)=CImage is the same as the level curve of z=f(x,y)Image corresponding to z=CImage.

Use ContourPlot to graph equations of the form f(x,y)=g(x,y)Image with

ContourPlot[f[x,y]==g[x,y],{x,a,b},{y,c,d}].

Example 2.29

Graph the equation y22x4+2x6x8=0Image for 1.5x1.5Image.

Solution

We define f(x,y)Image to be the left-hand side of the equation y22x4+2x6x8=0Image and then use ContourPlot to graph eq for 1.5x1.5Image in Fig. 2.20.

Image
Figure 2.20 Three plots of y2 − 2x4 + 2x6 − x8 = C. In (c), the graph is the plot of y2 − 2x4 + 2x6 − x8 = 0.

Clear [ x , y ] Image

f [ x _ , y _ ] = y 2 x 4 + 2 x 6 x 8 ; Image

cp1 = ContourPlot [ f [ x , y ] , { x , 2 , 2 } , { y , 2 , 2 } , Image

AspectRatio Automatic , PlotLabel “(a)” , Image

Contours 30 ] Image

To graph the contour plots of z=f(x,y)Image for particular values of C, create a list of the values of C for which you want the contour plots and then use the option Contours->List of C values.

For example, here we use Table to create a list of 30 equally spaced values of f(0,y)Image for 2y2Image.

vals = Table [ f [ 0 , y ] , { y , 2 , 2 , 4 / 29 } ] ; Image

Next, we use ContourPlot to graph f(x,y)=CImage for each C-value in vals and illustrate various options associated with the ContourPlot function.

cp2 = ContourPlot [ lhseq , { x , 2 , 2 } , { y , 2 , 2 } , Image

AspectRatio Automatic , Frame False , Contours vals , Image

Axes Automatic , AxesLabel { x , y } , PlotLabel “(b)” , Image

AxesOrigin { 0 , 0 } , ContourShading False ] Image

cp3 = ContourPlot [ lhseq = = 0 , { x , 2 , 2 } , { y , 2 , 2 } , Image

AspectRatio Automatic , Frame False , Image

Axes Automatic , AxesLabel { x , y } , PlotLabel “(c)” , Image

AxesOrigin { 0 , 0 } , ContourStyle Black ] Image

Finally, we use Show together with GraphicsRow to display all three graphics side-by-side in Fig. 2.20.

Show[GraphicsRow[{cp1,cp2,cp3}]]Image □

Equations can be plotted together, as with the commands Plot and Plot3D, with

ContourPlot[{eq1,eq2,...,eqn},{x,a,b},{y,c,d}].

Example 2.30

Graph the equations x2+y2=1Image and 4x2y2=1Image for 1.5x1.5Image.

Solution

We use ContourPlot to graph the equations together on the same axes in Fig. 2.21. The graph of x2+y2=1Image is the unit circle while the graph of 4x2y2=1Image is a hyperbola.

Image
Figure 2.21 Plots of x2 + y2 = 1 and 4x2 − y2 = 1.

cp1 = ContourPlot [ { x 2 + y 2 = = 1 , 4 x 2 y 2 = = 1 } , Image

{ x , 3 / 2 , 3 / 2 } , { y , 3 / 2 , 3 / 2 } , Frame False , Image

Axes Automatic , AxesOrigin { 0 , 0 } , Image

ContourStyle { Black , Gray } ] Image

From Fig. 2.21, we see four intersection points. To solve an equation of the form f(x)=g(x)Image for x, use Solve. Solve[f[x]==g[x],x] attempts to solve the equation f(x)=g(x)Image for x. For a system of two equations in two variables, try using Solve[{f[x,y]==0,g[x,y]==0},{x,y}] to solve the system f(x,y)=0Image, g(x,y)=0Image. Solve will be discussed in more detail in the next section.

Solve [ { x 2 + y 2 = = 1 , 4 x 2 y 2 = = 1 } ] Image

{{x25,y35},{x25,y35},{x25,y35},{x25,y35}}Image □

Solution

We begin by defining conic to be the equation ax2+bxy+cy2=1Image and then use Permutations to produce all possible orderings of the list of numbers {1,1,2}Image, naming the resulting output vals.

Permutations[list] returns a list of all possible orderings of the list list.

Clear [ a , b , c , x , y , p ] Image

conic = a x 2 + b x y + c y 2 = = 1 ; Image

vals = Permutations [ { 1 , 1 , 2 } ] Image

{ { 1 , 1 , 2 } , { 1 , 2 , 1 } , { 1 , 1 , 2 } , { 1 , 2 , 1 } , { 2 , 1 , 1 } , { 2 , 1 , 1 } } Image

Next we define the function p. Given a1, b1, and c1, p defines toplot to be the equation obtained by replacing a, b, and c in conic by a1, b1, and c1, respectively. Then, toplot is graphed for 4x4Image. The function p returns a graphics object.

Clear [ p ] Image

p [ { a1 _ , b1 _ , c1 _ } ] := Module [ { toplot } , Image

toplot = Evaluate [ conic /. { a a1 , b b1 , c c1 } ] ; Image

ContourPlot [ Evaluate [ toplot ] , { x , 5 , 5 } , Image

{ y , 5 , 5 } , Frame False , Axes Automatic , Ticks None ] Image

]

We then use Map to compute p for each ordered triple in vals. The resulting output, named graphs, is a set of six graphics objects.

graphs = Map [ p , vals ] ; Image

Partition is then used to partition graphs into three element subsets. The resulting array of graphics objects named toshow is displayed with Show and GraphicsGrid in Fig. 2.22.

Image
Figure 2.22 Plots of six conic sections.

Show[GraphicsGrid[Partition[graphs,3]]]Image □

Studying how parameter values affect the conic is particularly well-suited to a Manipulate object as illustrated in the following commands and then in Fig. 2.23.

Image
Figure 2.23 Using Manipulate to adjust parameter values.

Manipulate [ ContourPlot [ a x 2 + b x y + c y 2 = = 1 , Image

{ x , 5 , 5 } , Image

{ y , 5 , 5 } , Frame False , Axes Automatic , Ticks None , Image

ContourStyle Black , PlotPoints - > 60 ] , { { a , 1 } , 2 , 2 } , Image

{ { b , 0 } , 2 , 2 } , { { c , 1 } , 2 , 2 } ] Image

2.3.4 Parametric Curves and Surfaces in Space

The command

ParametricPlot3D[{x[t],y[t],z[t]},{t,a,b}]

generates the three-dimensional curve {x=x(t),y=y(t),z=z(t),Image atbImage and the command

ParametricPlot3D[{x[u,v],y[u,v],z[u,v]},{u,a,b},{v,c,d}]

plots the surface {x=x(u,v),y=y(u,v),z=z(u,v),Image aubImage, cvdImage.

Entering Information[ParametricPlot3D] or ??ParametricPlot3D returns a description of the ParametricPlot3D command along with a list of options and their current settings.

Solution

We define x=x(s,t)Image, y=y(s,t)Image, z=z(s,t)Image, and r(s,t)=<x(s,t),y(s,t),z(s,t)>Image.

c = 3 ; Image

a = 1 ; Image

x [ s _ , t _ ] = ( 7 + Cos [ s / 3 2 t ] + 2 Cos [ s / 3 + t ] ) Sin [ s ] ; Image

y [ s _ , t _ ] = ( 7 + Cos [ s / 3 2 t ] + 2 Cos [ s / 3 + t ] ) Cos [ s ] ; Image

z [ s _ , t _ ] = Sin [ s / 3 2 t ] + 2 Sin [ s / 3 + t ] ; Image

r [ s _ , t _ ] = { x [ s , t ] , y [ s , t ] , z [ s , t ] } ; Image

The torus is then graphed with ParametricPlot3D, DensityPlot, and ContourPlot in Fig. 2.24. In the plots, we illustrate the Mesh, MeshFunctions, PlotPoints, and PlotRange options. All four plots are shown together with Show and GraphicsGrid. Notice that DensityPlot and ContourPlot yield very similar results: a basic density plot is similar to a basic contour plot but without the contour lines.

Image
Figure 2.24 On the top row, two plots of Umbilic torus; on the bottom, comparing a density plot (on the left) to a contour plot (on the right).

threedp1uta = ParametricPlot3D [ r [ s , t ] , { s , Pi , Pi } , Image

{ t , Pi , Pi } , PlotPoints - > { 30 , 30 } , Image

AspectRatio - > 1 , AxesLabel - > { x , y , z } , Image

PlotRange - > { { 12 , 12 } , { 12 , 12 } , { 3 , 3 } } , Image

BoxRatios - > { 4 , 4 , 1 } , Mesh False , PlotStyle Opacity [ . 9 ] , Image

PlotLabel “(a)” ] Image

threedp1utb = ParametricPlot3D [ r [ s , t ] , { s , Pi , Pi } , Image

{ t , Pi , Pi } , PlotPoints - > { 50 , 50 } , Image

AspectRatio - > 1 , AxesLabel - > { x , y , z } , Image

PlotRange - > { { 12 , 12 } , { 12 , 12 } , { 3 , 3 } } , Image

BoxRatios - > { 4 , 4 , 1 } , Image

MeshFunctions - > { # 3 & } , Mesh 10 , Image

PlotLabel “(b)” ] Image

threedp1utc = DensityPlot [ r [ s , t ] , { s , Pi , Pi } , Image

{ t , Pi , Pi } , PlotPoints - > { 100 , 100 } , Image

AspectRatio - > 1 , AxesLabel - > { x , y , z } , Image

MeshFunctions - > { # 3 & } , Mesh 10 , Image

PlotLabel “(c)” ] Image

threedp1utd = ContourPlot [ r [ s , t ] , { s , Pi , Pi } , Image

{ t , Pi , Pi } , PlotPoints - > { 100 , 100 } , Image

AspectRatio - > 1 , AxesLabel - > { x , y , z } , Image

MeshFunctions - > { # 3 & } , Mesh 10 , PlotLabel “(d)” ] Image

Show [ GraphicsGrid [ { { threedp1uta , threedp1utb } , Image

{threedp1utc,threedp1utd}}]]Image □

Example 2.33

Gray's Torus Example

A parametrization of an elliptical torus is given by

x = ( a + b cos v ) cos u , y = ( a + b cos v ) sin u , z = c sin v

For positive integers p and q, the curve with parametrization

x = ( a + b cos q t ) cos p t , y = ( a + b cos q t ) sin p t , z = c sin q t

winds around the elliptical torus and is called a torus knot.

This example is explored in detail in Sections 8.2 and 11.4 of Gray's Modern Differential Geometry of Curves and Surfaces, [8], an indispensible reference for those who use Mathematica's graphics extensively. Note that since Dr. Gray's death, the text has been revised and updated in its third edition by Abbena and Salamon, [1].

Plot the torus if a=8Image, b=3Image, and c=5Image and then graph the torus knots for p=2Image and q=5Image, p=1Image and q=10Image, and p=2Image and q=3Image.

Solution

We begin by defining torus and torusknot.

torus [ a _ , b _ , c _ ] [ p _ , q _ ] [ u _ , v _ ] := Image

{ ( a + b Cos [ u ] ) Cos [ v ] , ( a + b Cos [ u ] ) Sin [ v ] , c Sin [ u ] } Image

torusknot [ a _ , b _ , c _ ] [ p _ , q _ ] [ t _ ] := Image

{ ( a + b Cos [ q t ] ) Cos [ p t ] , ( a + b Cos [ q t ] ) Sin [ p t ] , c Sin [ q t ] } Image

Next, we use ParametricPlot3D to generate all four graphs

pp1 = ParametricPlot3D [ Evaluate [ torus [ 8 , 3 , 5 ] [ 2 , 5 ] [ u , v ] ] , Image

{ u , 0 , 2 Pi } , { v , 0 , 2 Pi } , PlotPoints 60 ] ; Image

pp2 = ParametricPlot3D [ Evaluate [ torusknot [ 8 , 3 , 5 ] [ 2 , 5 ] [ t ] ] , Image

{ t , 0 , 3 Pi } , PlotPoints 200 ] ; Image

pp3 = ParametricPlot3D [ Evaluate [ torusknot [ 8 , 3 , 5 ] [ 1 , 10 ] [ t ] ] , Image

{ t , 0 , 3 Pi } , PlotPoints 200 ] ; Image

pp4 = ParametricPlot3D [ Evaluate [ torusknot [ 8 , 3 , 5 ] [ 2 , 3 ] [ t ] ] , Image

{ t , 0 , 3 Pi } , PlotPoints 200 ] ; Image

and show the result as a graphics array with Show and GraphicsGrid in Fig. 2.25.

Image
Figure 2.25 (a) An elliptical torus. (b) This knot is also known as the trefoil knot. (c) The curve generated by torusknot[8,3,5][2,3][1,10] is not a knot. (d) The torus knot with p = 2 and q = 3.

Show [ GraphicsGrid [ { { pp1 , pp2 } , { pp3 , pp4 } } ] ] Image

If we take advantage of a few options, such as eliminating the mesh (Mesh->False) and increasing the opacity (PlotStyle->Opacity[.4]), we can produce a graphic of the knot on the torus. After using the PlotStyle option together with Opacity, we produce a nearly transparent torus. Then, each knot is plotted. To assure smooth plots, we increase the number of points plotted with PlotPoints and also increase the thickness of the curve with Thickness.

pp1 = ParametricPlot3D [ Evaluate [ torus [ 8 , 3 , 5 ] [ 2 , 5 ] [ u , v ] ] , Image

{ u , 0 , 2 Pi } , { v , 0 , 2 Pi } , PlotPoints 60 , Image

Mesh False , PlotStyle Opacity [ . 4 ] , Image

ColorFunction “AlpineColors” ] ; Image

pp2 = ParametricPlot3D [ Evaluate [ torusknot [ 8 , 3 , 5 ] [ 2 , 5 ] [ t ] ] , Image

{ t , 0 , 3 Pi } , PlotPoints 200 , PlotStyle { Black , { Thickness [ . 025 ] } } ] ; Image

pp3 = ParametricPlot3D [ Evaluate [ torusknot [ 8 , 3 , 5 ] [ 1 , 10 ] [ t ] ] , Image

{ t , 0 , 3 Pi } , PlotPoints 200 , PlotStyle { Black , { Thickness [ . 025 ] } } ] ; Image

pp4 = ParametricPlot3D [ Evaluate [ torusknot [ 8 , 3 , 5 ] [ 2 , 3 ] [ t ] ] , Image

{ t , 0 , 3 Pi } , PlotPoints 200 , PlotStyle { Black , { Thickness [ . 025 ] } } ] ; Image

We use Show twice together with GraphicsRow to first display the torus with each knot and then display all three graphics side-by-side in Fig. 2.26.

Image
Figure 2.26 The knots in Fig. 2.25 on the torus.

Show[GraphicsRow[{Show[{pp1,pp2}],Show[{pp1,pp3}],Show[{pp1,pp4}]}]]Image □

Example 2.34

Quadric Surfaces

The quadric surfaces are the three-dimensional objects corresponding to the conic sections in two dimensions. A quadric surface is a graph of

A x 2 + B y 2 + C z 2 + D x y + E x z + F y z + G x + H y + I z + J = 0 ,

where A, B, C, D, E, F, G, H, I, and J are constants.

The intersection of a plane and a quadric surface is a conic section.

Several of the basic quadric surfaces, in standard form, and a parametrization of the surface are listed in the following table.

Name Parametric Equations
Ellipsoid
x 2 a 2 + y 2 b 2 + z 2 c 2 = 1 Image {x=acostcosr,y=bcostsinr,z=csint,Imageπ/2 ⩽ t ⩽ π/2, −π ⩽ r ⩽ π
Hyperboloid of One Sheet
x 2 a 2 + y 2 b 2 z 2 c 2 = 1 Image {x=asectcosr,y=bsectsinr,z=ctant,Imageπ/2 < t < π/2, −π ⩽ r ⩽ π
Hyperboloid of Two Sheets
x 2 a 2 y 2 b 2 z 2 c 2 = 1 Image {x=asect,y=btantcosr,z=ctantsinr,Imageπ/2 < t < π/2 or π/2 < t < 3π/2, −π ⩽ r ⩽ π

Graph the ellipsoid with equation 116x2+14y2+z2=1Image, the hyperboloid of one sheet with equation 116x2+14y2z2=1Image, and the hyperboloid of two sheets with equation 116x214y2z2=1Image.

Solution

A parametrization of the ellipsoid with equation 116x2+14y2+z2=1Image is given by

x = 4 cos t cos r , y = 2 cos t sin r , z = sin t , π / 2 t π / 2 , π r π ,

which is graphed with ParametricPlot3D.

Clear [ x , y , z ] Image

x [ t _ , r _ ] = 4 Cos [ t ] Cos [ r ] ; Image

y [ t _ , r _ ] = 2 Cos [ t ] Sin [ r ] ; Image

z [ t _ , r _ ] = Sin [ t ] ; Image

pp1 = ParametricPlot3D [ { x [ t , r ] , y [ t , r ] , z [ t , r ] } , { t , Pi / 2 , Pi / 2 } , { r , Pi , Pi } , Image

PlotPoints 30 , DisplayFunction Identity ] ; Image

A parametrization of the hyperboloid of one sheet with equation 116x2+14y2z2=1Image is given by

x = 4 sec t cos r , y = 2 sec t sin r , z = tan t , π / 2 < t < π / 2 , π r π .

Because sectImage and tantImage are undefined if t=±π/2Image, we use ParametricPlot3D to graph these parametric equations on a subinterval of [π/2,π/2]Image, [π/3,π/3]Image.

Clear [ x , y , z ] Image

x [ t _ , r _ ] = 4 Sec [ t ] Cos [ r ] ; Image

y [ t _ , r _ ] = 2 Sec [ t ] Sin [ r ] ; Image

z [ t _ , r _ ] = Tan [ t ] ; Image

pp2 = ParametricPlot3D [ { x [ t , r ] , y [ t , r ] , z [ t , r ] } , { t , Pi / 3 , Pi / 3 } , { r , Pi , Pi } , Image

PlotPoints 30 , DisplayFunction Identity ] ; Image

pp1 and pp2 are shown together in Fig. 2.27 using Show and GraphicsRow.

Image
Figure 2.27 (a) Plot of 116x2+14y2+z2=1Image. (b) Plot of 116x2+14y2z2=1Image.

Show [ GraphicsRow [ { pp1 , pp2 } ] ] Image

For (c), we take advantage of the ContourPlot3D function:

ContourPlot3D[f[x,y,z],{x,a,b},{y,c,d},{z,u,v}]

graphs several level surfaces of w=f(x,y,z)Image.

We use ContourPlot3D to graph the equation 116x214y2z21=0Image in Fig. 2.28 (a), illustrating the use of the PlotPoints, Axes, AxesLabel, and BoxRatios options. In Fig. 2.28 (b), several level surfaces are drawn that illustrate the use of the Opacity function with the ContourStyle and Mesh options.

Image
Figure 2.28 (a) Plot of 116x214y2z2=1Image generated with ContourPlot3D. (b) Several level surfaces of f(x,y,z)=116x214y2z2Image.

cp3d1 = ContourPlot3D [ x 2 / 16 y 2 / 4 z 2 1 = = 0 , Image

{ x , 10 , 10 } , { y , 8 , 8 } , { z , 2 , 2 } , Image

PlotPoints { 8 , 8 , 8 } , Axes Automatic , Image

AxesLabel { x , y , z } , BoxRatios { 2 , 1 , 1 } ] Image

cp3d2 = ContourPlot3D [ x 2 / 16 y 2 / 4 z 2 1 , Image

{ x , 10 , 10 } , { y , 8 , 8 } , Image

{ z , 2 , 2 } , PlotPoints { 8 , 8 , 8 } , Axes Automatic , Image

AxesLabel { x , y , z } , Image

BoxRatios { 2 , 1 , 1 } , Mesh False , Image

ContourStyle Opacity [ . 5 ] ] Image

Show[GraphicsRow[{cp3d1,cp3d2}]]Image □

ContourPlot3D is especially useful in plotting equations involving three variables x, y, and z for which it is difficult to solve for one variable as a function of the other two.

Example 2.35

Cross-Cap

The Cross-Cap has equation

4 x 2 ( x 2 + y 2 + z 2 + z ) + y 2 ( y 2 + z 2 1 ) .

We use ContourPlot3D to generate the plot of the Cross-Cap shown in Fig. 2.29

Image
Figure 2.29 The Cross-Cap.

ContourPlot3D [ 4 x 2 ( x 2 + y 2 + z 2 + z ) + Image

y 2 ( y 2 + z 2 1 ) = = 0 , { x , 1 , 1 } , { y , 1 , 1 } , Image

{ z , 1 , 1 } , Mesh False , Boxed False , Image

Axes None , ContourStyle Opacity [ . 8 ] ] Image

Example 2.36

A homotopy from the Roman surface to the Boy surface is given by

x ( u , v ) = 2 cos ( 2 u ) cos 2 v + cos u sin ( 2 v ) 2 α 2 sin ( 3 u ) sin ( 2 v ) , y ( u , v ) = 2 sin ( 2 u ) cos 2 v + sin u sin ( 2 v ) 2 α 2 sin ( 3 u ) sin ( 2 v ) ,  and  z ( u , v ) = 3 cos 2 v 2 α 2 sin ( 3 u ) sin ( 2 v ) .

Here, α=0Image gives the Roman surface and α=1Image gives the Boy surface.

If f and g are functions from X to Y, a homotopy from f to g is a continuous function H from X×[0,1]Image to Y satisfying H(x,0)=f(x)Image and H(x,1)=g(x)Image.

To see the homotopy we first define x, y, z, and r=x(s,t)i+y(s,t)j+z(s,t)kImage.

Clear [ x , y , z , r ] Image

x [ α _ ] [ s _ , t _ ] = ( Sqrt [ 2 ] Cos [ t ] 2 Cos [ 2 s ] + Cos [ s ] Sin [ 2 t ] ) / ( 2 α Sqrt [ 2 ] Sin [ 3 s ] Sin [ 2 t ] ) ; Image

y [ α _ ] [ s _ , t _ ] = ( Sqrt [ 2 ] Cos [ t ] 2 Sin [ 2 s ] Sin [ s ] Sin [ 2 t ] ) / ( 2 α Sqrt [ 2 ] Sin [ 3 s ] Sin [ 2 t ] ) ; Image

z [ α _ ] [ s _ , t _ ] = 3 Cos [ t ] 2 / ( 2 α Sqrt [ 2 ] Sin [ 3 s ] Sin [ 2 t ] ) ; Image

r [ α _ ] [ s _ , t _ ] = { x [ α ] [ s , t ] , y [ α ] [ s , t ] , z [ α ] [ s , t ] } ; Image

We then use Table together with ParametricPlot3D to parametrically plot x, y, and z, 0u2πImage, 0v2πImage for nine equally spaced values of α between 0 and 1. Note that if the semi-colon is omitted at the end of the command, the nine plots are displayed.

smalltable = Table [ ParametricPlot3D [ r [ α ] [ s , t ] , Image

{ s , 0 , 2 Pi } , { t , 0 , 2 Pi } , Boxed False , Axes None , Image

PlotRange { { 2 , 5 / 2 } , { 2 , 2 } , { 0 , 7 / 2 } } ] , Image

{ α , 0 , 1 , 1 / 8 } ] ; Image

We then use Partition to partition smalltable into three element subsets. The resulting 3×3Image array of graphics is shown as a grid with Show together with GraphicsGrid in Fig. 2.30.

Image
Figure 2.30 Seeing the Roman surface continuously transform to the Boy surface.

To adjust the viewing angles of three dimensional plots, select the graphic and drag to the desired viewing angle.

Show [ GraphicsGrid [ Partition [ smalltable , 3 ] ] ] Image

Another way of seeing the transformation is to use Manipulate. Manipulate is very powerful. In its most basic form, Manipulate[f[x],{x,a,b}] creates an interactive display of f(x)Image for x values from a to b. Because the previous commands depended only on α, we combine the commands into a single Manipulate object that depends on α.

Manipulate [ Image

Clear [ x , y , z , r ] ; Image

x [ α _ ] [ s _ , t _ ] = ( Sqrt [ 2 ] Cos [ t ] 2 Cos [ 2 s ] + Cos [ s ] Sin [ 2 t ] ) / Image

( 2 α Sqrt [ 2 ] Sin [ 3 s ] Sin [ 2 t ] ) ; Image

y [ α _ ] [ s _ , t _ ] = ( Sqrt [ 2 ] Cos [ t ] 2 Sin [ 2 s ] Sin [ s ] Sin [ 2 t ] ) / Image

( 2 α  Sqrt [ 2 ] Sin [ 3 s ] Sin [ 2 t ] ) ; Image

z [ α _ ] [ s _ , t _ ] = 3 Cos [ t ] 2 / ( 2 α Sqrt [ 2 ] Sin [ 3 s ] Sin [ 2 t ] ) ; Image

r [ α _ ] [ s _ , t _ ] = { x [ α ] [ s , t ] , y [ α ] [ s , t ] , z [ α ] [ s , t ] } ; Image

ParametricPlot3D [ r [ α ] [ s , t ] , Image

{ s , 0 , 2 Pi } , { t , 0 , 2 Pi } , Boxed False , Axes None , Image

PlotRange { { 2 , 5 / 2 } , { 2 , 2 } , { 0 , 7 / 2 } } ] , Image

{ { α , . 25 } , 0 , 1 } ] Image

Several images from the result are shown in Fig. 2.31.

Image
Figure 2.31 With Manipulate we can create an animation of the transformation of the Roman surface to the Boy surface or inspect the plot for various values of α.

Manipulation of graphics is discussed in more detail in Section 5.6, Matrices and Graphics. Here, we simply illustrate a few quick ways to manipulate a basic jpeg that illustrate a few of the features of Mathematica 11.

Mathematica 11 provides numerous ways to adjust digital images.

Image

Generally, be sure the graphics file that you want to adjust is in the same folder/directory as the open Mathematica notebook. Use Import["file"] to import the file into your open Mathematica notebook. Keep in mind that these graphics can be assigned names just like any other Mathematica objects. Thus, use assign, =, to assign a name to each graphic as you make adjustments. Finally, use Export, Export["file.ext",expr] to export your modified graphic expr using the name file.ext, where ext is usually something similar to jpg or pdf. You can then move those graphics into your favorite photography or printing program so you can fine-tune the adjustments and then print the results.

Example 2.37

We use Import to import a few graphics into Mathematica. The four graphs are displayed in a row using Show and GraphicsRow in Fig. 2.32.

Image
Figure 2.32 Importing elementary graphics into Mathematica.

p1 = Import [ “rome.jpg” ] ; Image

p2 = Import [ “atlanta.jpg” ] ; Image

p3 = Import [ “thehigh.jpg” ] ; Image

p4 = Import [ “roundchurch.jpg” ] ; Image

Show [ GraphicsRow [ { p1 , p2 , p3 , p4 } ] ] Image

Mathematica's tools for editing digital images are quite extensive. Here, we illustrate just a few of the basic available functions. You can combine graphics together with ImageCollage. The command

ImageCollage[{n1->image1,n2->image2,...,nm->imagem}]

creates a collage that weights the various images according to the n-value (see Fig. 2.33).

Image
Figure 2.33 Combining images into a single collage.

r1 = ImageCollage [ { p1 , p2 , p3 , p4 } ] ; Image

r2 = ImageCollage [ { 5 p1 , 2 p2 , 3 p3 , 1 p4 } ] ; Image

Show [ GraphicsRow [ { r1 , r2 } ] ] Image

ImageEffect can be used to apply various effects to an image. Use ?ImageEffect for a comprehensive discussion of the different effects that are available (see Fig. 2.34).

Image
Figure 2.34 Applying basic visual effects to an image.

p1a = ImageEffect [ p1 , “Charcoal” ] ; Image

p1b = ImageEffect [ p1 , “OilPainting” ] ; Image

p1c = ImageEffect [ p1 , “Solarization” ] ; Image

p1d = ImageEffect [ p1 , “Comics” ] ; Image

Show[GraphicsRow[{p1a,p1b,p1c,p1d}]]Image

DominantColors returns a list of the dominant colors in a graphic (see Fig. 2.35).

Image
Figure 2.35 Approximating the image of The High with 2, 4, 5, and 6 colors.

DominantColors [ p3 ] Image

Image

You can then use commands like ColorQuantize to approximate an image with n discrete colors. Later, we will use ImageApply to change the colors of individual pixels.

p3a = ColorQuantize [ p3 , 2 ] ; Image

p3b = ColorQuantize [ p3 , 4 ] ; Image

p3c = ColorQuantize [ p3 , 5 ] ; Image

p3d = ColorQuantize [ p3 , 6 ] ; Image

Show [ GraphicsRow [ { p3a , p3b , p3c , p3d } ] ] Image

ColorToneMapping transforms an image so that you can see small variations in color. On the other hand, ImageAdjust adjusts the color levels (see Fig. 2.36).

Image
Figure 2.36 A basic adjustment of color levels in an image.

p2b = ColorToneMapping [ p2 ] ; Image

p2c = ImageAdjust [ p2 , . 5 ] ; Image

Show [ GraphicsRow [ { p2 , p2b , p2c } ] ] Image

2.3.5 Miscellaneous Comments

Clearly, Mathematica's graphics capabilities are extensive and volumes could be written about them. You can see many commands that we haven't discussed here by using ? to see those commands that contain the string Plot.

Image

You can obtain detailed information regarding any of these commands from the Documentation Center by clicking on the command's name.

Be sure to take advantage of MathWorld for a huge number of resources related to graphics and Mathematica.

For now, we briefly mention a few of the ones that were not discussed previously. To plot lists of numbers or lists of ordered pairs, use ListPlot or ListLinePlot, which are discussed in more detail in Chapter 4. For matrices and other arrays use commands such as MatrixPlot or ArrayPlot that are discussed in more detail in Chapter 4.

Example 2.38

Cellular Automaton

Very loosly speaking, a cellular automaton is a discrete function that assigns values to subsequent rows based on the values of the cells in the previous row(s). For a concise discussion of cellular automaton refer to Weisstein (Weisstein, Eric W. “Cellular Automaton.” From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.com/CellularAutomaton.html), CellularAutomatan is a powerful command that allows you to investigate (quite complicated) cellular automaton. In its simplest form,

CellularAutomaton[rule,initialvalues,n]

returns the first n generations of the cellular automaton following the specified rule and having the indicated initial values.

The simplest cellular automaton are called elementary cellular automaton (Weisstein, Eric W. “Elementary Cellular Automaton.” From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.com/ElementaryCellularAutomaton.html). Based on basic counting principals, there are 256 elementary cellular automaton. They are cataloged by number. With

CellularAutomaton [ 146 , { { 1 } , 0 } , 5 ] Image

{ { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 } } Image

{ { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } , Image

{ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 } , Image

{ 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 } } Image

we calculate the first five generations of the elementary cellular automaton with a 1 at position 0 on generation 0 using Rule 146. To calculate the first 100 generations, we use CellularAutomaton[146, {{1},0}, 100]. The resulting array is rather large so we use ArrayPlot to visualize it in Fig. 2.37 (a). Using our color scheme, the cells with value 1 are shaded in red and those with 0 are in light green.

Image
Figure 2.37 The evolution of two cellular automaton evolving according to Rule 146.

a1 = ArrayPlot [ CellularAutomaton [ 146 , { { 1 } , 0 } , 100 ] , Image

ColorFunction “NeonColors” , AspectRatio 1 ] Image

In this case the grid is initially spaced so that positions 1, 11, 21, 31, and 41 have the value one. The first three generations using Rule 146 are calculated.

CellularAutomaton [ 146 , Image

{ SparseArray [ { 1 1 , 11 1 , 21 1 , 31 1 , 41 1 } ] , 0 } , 3 ] Image

{ { 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , Image

0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 } , Image

{ 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , Image

0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 } , Image

{ 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , Image

1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 } , Image

{ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , Image

0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 } } Image

To see how the situation evolves over 100 generations is more easily seen using ArrayPlot. See Fig. 2.37 (b)

a2 = ArrayPlot [ CellularAutomaton [ 146 , Image

{ SparseArray [ { 1 1 , 11 1 , 21 1 , 31 1 , 41 1 } ] , 0 } , 100 ] , Image

ColorFunction “NeonColors” , AspectRatio 1 ] Image

Show [ GraphicsRow [ { a1 , a2 } ] ] Image

Of the 256 elementary cellular automaton, many are equivalent. To see that some of them are equivalent, we create a plot of the 256 elementary cellular automaton for 50 generations as done with Rule 146. All 256 plots are shown on the left in Fig. 2.38 (a). With Union, we remove and sort the ones that are identically equal. Those are shown on the right in Fig. 2.38 (b).

Image
Figure 2.38 (a) The first 50 generations for the 256 elementary cellular automaton. (b) Removal of the identical ones.

t1 = Table [ ArrayPlot [ CellularAutomaton [ i , { { 1 } , 0 } , 50 ] ] , Image

{ i , 0 , 255 } ] ; Image

t2 = Partition [ t1 , 16 ] ; Image

p1 = Show [ GraphicsGrid [ t2 ] ] ; Image

t3 = Union [ t1 ] ; Image

t4 = Partition [ t3 , 12 ] ; Image

p2 = Show [ GraphicsGrid [ t4 ] ] ; Image

Show [ GraphicsRow [ { p1 , p2 } ] ] Image

To see the plots together with the rule number, use Table. Each ordered pair returned consists of the rule number and the 50 generation plot. To display the ordered pairs in an organized fashion, we use Grid. Of course, the result is quite large so just a portion of the actual grid is displayed in Fig. 2.39.

Image
Figure 2.39 Seeing the automaton together with its rule number.

t5 = Table [ { i , ArrayPlot [ CellularAutomaton [ i , { { 1 } , 0 } , 50 ] ] } , Image

{ i , 0 , 255 } ] ; Image

t6 = Partition [ t5 , 16 ] ; Image

Grid [ t6 ] Image

Note that MatrixPlot and ArrayPlot are discussed in more detail in Chapter 5.

For graphs that involve points or nodes or connecting them by edges (graph theory), you can use GraphPlot to help investigate some problems. For trees, use TreePlot.

Image

Example 2.39

Graceful graphs don't have multiple edges or loops.

We generate O with GraphPlot and display the result in Fig. 2.40 (a).

Image
Figure 2.40 (a) O. (b) C4.

gp1 = GraphPlot [ { { 0 - > 12 , “12” } , { 12 - > 1 , “11” } , { 1 - > 0 , “1” } , { 0 - > 9 , “9” } , Image

{ 1 - > 9 , “8” } , { 1 - > 6 , “5” } , { 12 - > 6 , “6” } , { 2 12 , “10” } , Image

{ 0 - > 2 , “2” } , 6 - > 9 , 9 10 , 10 6 } , Image

VertexLabeling True , AspectRatio 1 ] Image

C4Image is shown in Fig. 2.40 (b).

gp2 = GraphPlot [ { 0 4 , 4 2 , 2 3 , 3 0 } , DirectedEdges True , Image

VertexLabeling True , AspectRatio 1 ] Image

Replacing GraphPlot with TreePlot gives us Fig. 2.41.

Image
Figure 2.41 Using TreePlot instead of GraphPlot.

tp1 = TreePlot [ { { 0 - > 12 , “12” } , { 12 - > 1 , “11” } , { 1 - > 0 , “1” } , { 0 - > 9 , “9” } , Image

{ 1 - > 9 , “8” } , { 1 - > 6 , “5” } , { 12 - > 6 , “6” } , { 2 12 , “10” } , Image

{ 0 - > 2 , “2” } , 6 - > 9 , 9 10 , 10 6 } , Image

VertexLabeling True , AspectRatio 1 ] Image

tp2 = TreePlot [ { 0 4 , 4 2 , 2 3 , 3 0 } , DirectedEdges True , Image

VertexLabeling True , AspectRatio 1 ] Image

2.4 Solving Equations

2.4.1 Exact Solutions of Equations

Mathematica can find exact solutions to many equations and systems of equations, including exact solutions to polynomial equations of degree four or less. Because a single equals sign “=” is used to name objects and assign values in Mathematica, equations in Mathematica are of the form

left-hand side==right-hand side.

The “double-equals” sign “==” between the left-hand side and right-hand side specifies that the object is an equation. For example, to represent the equation 3x+7=4Image in Mathematica, type 3x+7==4. The command Solve[lhs==rhs,x] solves the equation lhs=rhsImage for x. If the only unknown in the equation lhs=rhsImage is x and Mathematica does not need to use inverse functions to solve for x, the command Solve[lhs==rhs] solves the equation lhs=rhsImage for x. Hence, to solve the equation 3x+7=4Image, both the commands Solve[3x+7==4] and Solve[3x+7==4,x] return the same result.

Image

Example 2.40

Solve the equations 3x+7=4Image, (x21)/(x1)=0Image, and x3+x2+x+1=0Image.

Solution

In each case, we use Solve to solve the indicated equation. Be sure to include the double equals sign “==” between the left and right-hand sides of each equation. Thus, the result of entering

Solve [ 3 x + 7 == 4 ] Image

{ { x 1 } } Image

means that the solution of 3x+7=4Image is x=1Image and the result of entering

Solve [ x 2 1 x 1 == 0 ] Image

{ { x 1 } } Image

means that the solution of x21x1=0Image is x=1Image. On the other hand, the equation x3+x2+x+1=0Image has two imaginary roots. We see that entering

Solve [ x 3 + x 2 + x + 1 == 0 ] Image

{ { x 1 } , { x i } , { x i } } Image

yields all three solutions. Thus, the solutions of x3+x2+x+1=0Image are x=1Image and x=±iImage. Remember that the Mathematica symbol I represents the complex number i=1Image. In general, Mathematica can find the exact solutions of any polynomial equation of degree four or less.  □

Observe that the results of a Solve command are a list.

Mathematica can also solve equations involving more than one variable for one variable (literal equations) in terms of other unknowns.

Example 2.41

(a) Solve the equation v=πr2/hImage for h. (b) Solve the equation a2+b2=c2Image for a.

Solution

These equations involve more than one unknown so we must specify the variable for which we are solving in the Solve commands. Thus, entering

Solve [ v = = Pi r 2 / h , h ] Image

{ { h π r 2 v } } Image

solves the equation v=πr2/hImage for h. (Be sure to include a space or * between π and r.) Similarly, entering

Solve [ a 2 + b 2 = = c 2 , a ] Image

{ { a b 2 + c 2 } , { a b 2 + c 2 } } Image

solves the equation a2+b2=c2Image for a. □

If Mathematica needs to use inverse functions to solve an equation, you must be sure to specify the variable(s) for which you want Mathematica to solve.

Example 2.42

Find a solution of sin2x2sinx3=0Image.

Solution

When the command Solve[Sin[x]^2-2Sin[x]-3==0] is entered, Mathematica solves the equation for Sin[x]. However, when the command

Solve[Sin[x]^2-2Sin[x]-3==0,x]

is entered, Mathematica attempts to solve the equation for x.

Solve [ Sin [ x ] 2 2 Sin [ x ] 3 == 0 ] Image

{ { x ConditionalExpression [ π 2 + 2 π C [ 1 ] , C [ 1 ] Integers ] } , Image

{ x ConditionalExpression [ 3 π 2 + 2 π C [ 1 ] , C [ 1 ] Integers ] } , Image

{ x ConditionalExpression [ π ArcSin [ 3 ] + 2 π C [ 1 ] , C [ 1 ] Integers ] } , Image

{ x ConditionalExpression [ ArcSin [ 3 ] + 2 π C [ 1 ] , C [ 1 ] Integers ] } } Image

Mathematica's result indicates that the solutions to the equation are x=π/2+2CπImage and x=3π/2+2CπImage, where C is any integer. sin13Image is not a real number so in the context of solving the equation, we ignore the complex-valued solutions. Therefore this equation has infinitely many solutions of the form x=12(4k1)πImage, k=0Image, ±1, ±2, ...; sinx=3Image has no solutions. □

Example 2.43

Let f(θ)=sin2θ+2cosθImage, 0θ2πImage. (a) Solve f(θ)=0Image. (b) Graph f(θ)Image and f(θ)Image.

Solution

After defining f(θ)Image, we use D to compute f(θ)Image and then use Solve to solve f(θ)=0Image.

D[f[x],x] computes f(x)Image; D[f[x],{x,n}] computes f(n)(x)Image. Topics from calculus are discussed in more detail in Chapter 3.

f [ θ _ ] = Sin [ 2 θ ] + 2 Cos [ θ ] ; Image

df = f [ θ ] Image

2 Cos [ 2 θ ] 2 Sin [ θ ] Image

Solve [ df = = 0 , θ ] Image

{ { θ ConditionalExpression [ π 2 + 2 π C [ 1 ] , C [ 1 ] Integers ] } , Image

{ θ ConditionalExpression [ π 6 + 2 π C [ 1 ] , C [ 1 ] Integers ] } , Image

{ θ ConditionalExpression [ 5 π 6 + 2 π C [ 1 ] , C [ 1 ] Integers ] } } Image

As in the previous example, the solutions are given as a sequence of conditionals: π/2+2CπImage, π/6+2CπImage and 5π/6+2CπImage, where C is an integer. Of these infinitely many solutions, θ=π/6Image, 5π/6Image, and 3π/2Image are in the interval [0,2π]Image.

To verify by hand, we use the identity cos2θ=12sin2θImage and factor:

2 cos 2 θ 2 sin θ = 0 1 2 sin 2 θ sin θ = 0 2 sin 2 θ + sin θ 1 = 0 ( 2 sin θ 1 ) ( sin θ + 1 ) = 0

so sinθ=1/2Image or sinθ=1Image. Because we are assuming that 0θ2πImage, we obtain the solutions θ=π/6Image, 5π/6Image, or 3π/2Image. We perform the same steps with Mathematica.

expression /. x->y replaces all occurrences of x in expressionImage by y.

s1 = TrigExpand [ df ] Image

2 Cos [ θ ] 2 2 Sin [ θ ] 2 Sin [ θ ] 2 Image

s2 = s1 /. Cos [ θ ] 2 1 Sin [ θ ] 2 Image

2 Sin [ θ ] 2 Sin [ θ ] 2 + 2 ( 1 Sin [ θ ] 2 ) Image

Factor [ s2 ] Image

2 ( 1 + Sin [ θ ] ) ( 1 + 2 Sin [ θ ] ) Image

Finally, we graph f(θ)Image and f(θ)Image with Plot in Fig. 2.42. Note that the plot is drawn to scale because we include the option AspectRatio->Automatic.

Image
Figure 2.42 Graphs of f(θ) in University of Iowa yellow and f(θ) in University of Michigan blue. (For interpretation of the colors in this figure, the reader is referred to the web version of this chapter.)

p1 = Plot [ { f [ θ ] , df } , { θ , 0 , 2 π } , AspectRatio Automatic , Image

PlotStyle { { CMYKColor [ 0 , . 09 , . 80 , 0 ] } , { CMYKColor [ 1 , 6.0 , 0 , . 60 ] } } , Image

PlotLabel“University of Iowa Yellow and University of Michigan Blue”]Image □

We can also use Solve to find the solutions, if any, of various types of systems of equations. Entering

Solve[{lhs1==rhs1,lhs2==rhs2},{x,y}]

solves a system of two equations for x and y while entering

Solve[{lhs1==rhs1,lhs2==rhs2}]

attempts to solve the system of equations for all unknowns. In general, Solve can find the solutions to a system of linear equations. In fact, if the systems to be solved are inconsistent or dependent, Mathematica's output indicates so.

Example 2.44

Solve each system: (a) {3xy=4x+y=2Image; (b) {2x3y+4z=23x2y+z=0x+yz=1Image; (c) {2x2y2z=2x+y+3z=03x+3y2z=1Image; and (d) {2x+2y2z=23x2y+2z=2x+3y3z=3Image.

Solution

In each case we use Solve to solve the given system. For (a), the result of entering

Solve [ { 3 x y == 4 , x + y == 2 } , { x , y } ] Image

{{x32,y12}}Image

means that the solution of {3xy=4x+y=2Image is (x,y)=(3/2,1/2)Image, which is the point of intersection of the lines with equations 3xy=4Image and x+y=2Image. See Fig. 2.43 (a).

Image
Figure 2.43 (a) Two intersecting lines. (b) Three planes that intersect in a single point. (c) These three planes have no point in common. (d) The intersection of these three planes is a line.

cp1 = ContourPlot [ { 3 x y = = 4 , x + y = = 2 } , Image

{ x , 1 , 2 } , { y , 1 , 2 } , Frame False , Image

Axes Automatic , AxesOrigin { 0 , 0 } , Image

AxesLabel { x , y } ] Image

(b) We can verify that the results returned by Mathematica are correct. First, we name the system of equations sys and then use Solve to solve the system of equations naming the result sols.

sys = { 2 x 3 y + 4 z == 2 , 3 x 2 y + z == 0 , Image

x + y z == 1 } ; Image

sols = Solve [ sys , { x , y , z } ] Image

{ { x 7 10 , y 9 5 , z 3 2 } } Image

We verify the result by substituting the values obtained with Solve back into sys with ReplaceAll (/.).

sys /. sols Image

{{True,True,True}}Image

means that the solution of {2x3y+4z=23x2y+z=0x+yz=1Image is (x,y,z)=(7/10,9/5,3/2)Image, which is the point of intersection of the planes with equations 2x3y+4z=2Image, 3x2y+z=0Image, x+yz=1Image. See Fig. 2.43 (b).

cp2a = ContourPlot3D [ { 2 x 3 y + 4 z == 2 , 3 x 2 y + z == 0 , Image

x + y z == 1 } , { x , 3 , 3 } , Image

{ y , 3 , 3 } , { z , 3 , 3 } ] Image

To better see the intersection point, click within the graphic and then drag to an appropriate viewing angle.

Image

(c) When we use Solve to solve this system, Mathematica returns {}, which indicates that the system has no solution; the system is inconsistent.

Solve [ { 2 x 2 y 2 z == 2 , x + y + 3 z == 0 , 3 x + 3 y 2 z == 1 } ] Image

{}

To see that the planes with equations 2x2y2z=2Image, x+y+3z=0Image, and 3x+3y2z=1Image have no points in common, graph them with in Fig. 2.43 (c).

cp3a = ContourPlot3D [ { 2 x 2 y 2 z = = 2 , Image

x + y + 3 z = = 0 , 3 x + 3 y 2 z = = 1 } , { x , 2 , 2 } , Image

{y,2,2},{z,2,2}]Image

To better see that the planes do not intersect, we click and drag the graphic to an appropriate viewing angle.

Image

(d) On the other hand, when we use Solve to solve this system, Mathematica's result indicates that the system has infinitely many solutions. That is, all ordered triples of the form {(0,y,1+y)|yreal}Image are solutions of the system.

Solve [ { 2 x + 2 y 2 z == 2 , 3 x 2 y + 2 z == 2 , Image

x + 3 y 3 z == 3 } ] Image

{ { x 0 , z 1 + y } } Image

We see that the intersection of the three planes is a line with ContourPlot3D. See Fig. 2.43 (d).

cp3a = ContourPlot3D [ { 2 x 2 y 2 z = = 2 , Image

3 x 2 y + 2 z = = 2 , x + 3 y 3 z = = 3 } , { x , 2 , 2 } , Image

{ y , 2 , 2 } , { z , 2 , 2 } ] Image

Show[GraphicsGrid[{{cp1,cp2a},{cp3a,cp4a}}]]Image □

We can often use Solve to find solutions of a nonlinear system of equations as well.

Example 2.45

Solve the systems (a) {4x2+y2=4x2+4y2=4Image and (b) {1a2x2+1b2y2=1y=mxImage (a, b greater than zero) for x and y.

Solution

The graphs of the equations are both ellipses. We use ContourPlot to graph each equation, naming the results cp1 and cp2, respectively, and then use Show to display both graphs together in Fig. 2.44 (a). The solutions of the system correspond to the intersection points of the two graphs. Alternatively, the solutions of the system correspond to the intersection points of the level curves of f(x,y)=4x2+y24Image and g(x,y)=x2+4y24Image corresponding to 0. See Fig. 2.44 (b).

Image
Figure 2.44 (a) Graphs of 4x2 + y2 = 4 and x2 + 4y2 = 4. (b) 3-dimensional plots of f(x,y) and g(x,y) together with their level curves shown as contours.

cp1 = ContourPlot [ 4 x 2 + y 2 4 , { x , 3 , 3 } , { y , 3 , 3 } , Contours { 0 } , Image

ContourShading False , PlotPoints 60 ] ; Image

cp2 = ContourPlot [ x 2 + 4 y 2 4 , { x , 3 , 3 } , { y , 3 , 3 } , Contours { 0 } , Image

ContourShading False , PlotPoints 60 ] ; Image

cp3 = Show [ cp1 , cp2 , Frame False , Axes Automatic , AxesOrigin { 0 , 0 } ] Image

cp4 = Plot3D [ { 4 x 2 + y 2 4 , x 2 + 4 y 2 4 } , { x , 10 , 10 } , Image

{ y , 10 , 10 } , BoxRatios { 1 , 1 , 1 } , MeshFunctions - > { # 3 & } , Image

ColorFunction ( ColorData [ "Rainbow" ] [ # 3 ] & ) , Image

PlotStyle { Opacity [ . 4 ] , Opacity [ . 8 ] } ] Image

Show [ GraphicsRow [ { cp3 , cp4 } ] ] Image

Finally, we use Solve to find the solutions of the system.

 .1in

Solve [ { 4 x 2 + y 2 == 4 , x 2 + 4 y 2 == 4 } ] Image

{ { x 2 5 , y 2 5 } , { x 2 5 , y 2 5 } , { x 2 5 , y 2 5 } , { x 2 5 , y 2 5 } } Image

For (b), we also use Solve to find the solutions of the system. However, because the unknowns in the equations are a, b, m, x, and y, we must specify that we want to solve for x and y in the Solve command.

Solve [ { x 2 a 2 + y 2 b 2 == 1 , y == m x } , { x , y } ] Image

{{xabb2+a2m2,yabmb2+a2m2},{xabb2+a2m2,yabmb2+a2m2}}Image □

Although Mathematica can find the exact solution to every polynomial equation of degree four or less, exact solutions to some equations may not be meaningful. In those cases, Mathematica can provide approximations of the exact solutions using either the N[expression] or the expression // N commands.

Example 2.46

Approximate the solutions to the equations (a) x42x2=1xImage; and (b) 1x2=x3Image.

Solution

Each of these is a polynomial equation with degree less than five so Solve will find the exact solutions of each equation. However, the solutions are quite complicated so we use N to obtain approximate solutions of each equation. For (a), entering

N [ Solve [ x 4 2 x 2 == 1 x ] ] Image

{ { x 0.182777 0.633397 i } , { x 0.182777 + 0.633397 i } , { x 1.71064 } , Image

{ x 1.34509 } } Image

first finds the exact solutions of the equation x42x2=1xImage and then computes approximations of those solutions. The resulting output is the list of approximate solutions. Approximating solutions of equations is discussed further in the next subsection.

For (b), entering

Solve [ 1 x 2 == x 3 , x ] // N Image

{ { x 0.754878 } , { x 0.877439 + 0.744862 i } , Image

{ x 0.877439 0.744862 i } } Image

{ { x 0.754878 } , { x 0.877439 + 0.744862 i } , Image

{ x 0.877439 0.744862 i } } Image

first finds the exact solutions of the equation 1x2=x3Image and then computes approximations of those solutions. The resulting output is the list of approximate solutions. □

2.4.2 Approximate Solutions of Equations

When solving an equation is either impractical or impossible, Mathematica provides several functions including FindRoot, NRoots, and NSolve to approximate solutions of equations. NRoots and numerically approximate the roots of any polynomial equation. The command NRoots[poly1==poly2,x] approximates the solutions of the polynomial equation poly1==poly2, where both poly1 and poly2 are polynomials in x. The syntax for NSolve is the same as the syntax of NRoots although NSolve is usually capable of approximating solutions of more complicated equations than NRoots. Often you will find that using Solve together with N will yield the exact same results as those obtained using NSolve. Regardless, it is relatively easy to ask Mathematica to approximate the solutions of a complicated equation and it cannot. In a general sense, solving and approximating solutions of equations in a general sense is a challenging problem.

FindRoot attempts to approximate a root to an equation provided that a “reasonable” guess of the root is given. FindRoot works on functions other than polynomials. The command

FindRoot[lhs==rhs,{x,firstguess}]

searches for a numerical solution to the equation lhs==rhs, starting with x = firstguess. To locate more than one root, FindRoot must be used several times. One way of obtaining firstguess (for real-valued solutions) is to graph both lhs and rhs with Plot, find the point(s) of intersection, and estimate the x-coordinates of the point(s) of intersection. Generally, NRoots is easier to use than FindRoot when trying to approximate the roots of a polynomial. For “simple” non-polynomial equations, NSolve may work well. If NRoots or NSolve do not yield the desired results, it may be best to use FindRoot to approximate each root (solution) individually.

Example 2.47

Approximate the solutions of x5+x44x3+2x23x7=0Image.

Solution

Because x5+x44x3+2x23x7=0Image is a polynomial equation, we may use NRoots to approximate the solutions of the equation. Thus, entering

NRoots [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 == 0 , x ] Image

x = = 2.74463 x = = 0.880858 x = = 0.41452 1.19996 i Image

x = = 0.41452 + 1.19996 i x = = 1.79645 Image

x = = 2.74463 x = = 0.880858 x = = 0.41452 1.19996 i Image

x = = 0.41452 + 1.19996 i x = = 1.79645 Image

approximates the solutions of x5+x44x3+2x23x7=0Image. The symbol || appearing in the result represents “or”.

We obtain equivalent results with NSolve.

NSolve [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 == 0 , x ] Image

{ { x 2.74463 } , { x 0.880858 } , { x 0.41452 1.19996 i } , Image

{ x 0.41452 + 1.19996 i } , { x 1.79645 } } Image

{ { x 2.74463 } , { x 0.880858 } , { x 0.41452 1.19996 i } , Image

{ x 0.41452 + 1.19996 i } , { x 1.79645 } } Image

FindRoot may also be used to approximate each root of the equation. However, to use FindRoot, we must supply an initial approximation of the solution that we wish to approximate. The real solutions of x5+x44x3+2x23x7=0Image correspond to the values of x where the graph of f(x)=x5+x44x3+2x23x7Image intersects the x-axis. We use Plot to graph f(x)Image in Fig. 2.45.

Image
Figure 2.45 Graph of f(x)=x5 + x4 − 4x3 + 2x2 − 3x − 7 in Princeton orange.

Plot [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 , { x , 3 , 2 } , Image

PlotStyle CMYKColor [ { 0 , . 62 , . 95 , 0 } ] , Image

PlotLabel “Princeton Orange” , Image

AxesLabel { x , y } ] Image

We see that the graph intersects the x-axis near x2.5Image, −1, and 1.5. We use these values as initial approximations of each solution. Thus, entering

FindRoot [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 == 0 , { x , 2.5 } ] Image

{ x 2.74463 } Image

approximates the solution near −2.5, entering

FindRoot [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 == 0 , { x , 1 } ] Image

{ x 0.880858 } Image

approximates the solution near −1, and entering

FindRoot [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 == 0 , { x , 2 } ] Image

{ x 1.79645 } Image

approximates the solution near 1.5, which is relatively close to x=2Image, which is why we used x=2Image as our initial guess in the FindRoot command. Note that FindRoot may be used to approximate complex solutions as well. To obtain initial guesses, observe that the solutions of f(z)=0Image, z=x+iyImage, x, y real, are the level curves of w=|f(z)|Image that are points. In Fig. 2.46, we use ContourPlot to graph various level curves of w=|f(x+iy)|Image, 2x2Image, 2y2Image. In the plot, observe that the two complex solutions occur at x±iy0.5±1.2iImage.

Image
Figure 2.46 Level curves of w = |f(x + iy)|, −2 ⩽ x ⩽ 2, −2 ⩽ y ⩽ 2.

f [ z _ ] = z 5 + z 4 4 z 3 + 2 z 2 3 z 7 ; Image

ContourPlot [ Abs [ f [ x + I y ] ] , { x , 2 , 2 } , { y , 2 , 2 } , Image

ContourShading False , Contours 60 , Image

PlotPoints 200 , Frame False , Axes Automatic , Image

AxesOrigin { 0 , 0 } ] Image

Thus, entering

FindRoot [ x 5 + x 4 4 x 3 + 2 x 2 3 x 7 == 0 , { x , 0.5 + I } ] Image

{ x 0.41452 + 1.19996 i } Image

approximates the solution near x+iy0.5+1.2iImage. For polynomials with real coefficients, complex solutions occur in conjugate pairs so the other complex solution is approximately 0.414521.19996iImage.

Example 2.48

Find the first three nonnegative solutions of x=tanxImage.

Solution

We attempt to solve x=tanxImage with Solve.

Image

We next graph y=xImage and y=tanxImage together in Fig. 2.47.

Image
Figure 2.47 y = x and y=tanxImage.

Plot [ Tooltip [ { x , Tan [ x ] } ] , { x , 0 , 4 Pi } , Image

PlotRange{4Pi,4Pi}]Image □

In the graph, we see that x=0Image is a solution. This is confirmed with FindRoot.

Remember that vertical lines are never the graphs of functions. In this case, they represent the vertical asymptotes at odd multiples of π/2Image.

FindRoot [ x = = Tan [ x ] , { x , 0 } ] Image

{ x 0 . } Image

The second solution is near 4 while the third solution is near 7. Using FindRoot together with these initial approximations locates the second two solutions.

FindRoot [ x = = Tan [ x ] , { x , 4 } ] Image

{ x 4.49341 } Image

FindRoot [ x = = Tan [ x ] , { x , 7 } ] Image

{ x 7.72525 } Image

FindRoot can also be used to approximate solutions to systems of equations. (Although NRoots can solve a polynomial equation, NRoots cannot be used to solve a system of polynomial equations.) When approximations of solutions of systems of equations are desired, use either Solve and N together, when possible, or FindRoot.

Example 2.49

Approximate the solutions to the system of equations {x2+4xy+y2=45x24xy+2y2=8Image.

Solution

We begin by using ContourPlot to graph each equation in Fig. 2.48. From the resulting graph, we see that x2+4xy+y2=4Image is a hyperbola, 5x24xy+2y2=8Image is an ellipse, and there are four solutions to the system of equations.

Image
Figure 2.48 Graphs of x2 + 4xy + y2 = 4 (Nebraska scarlet) and 5x2 − 4xy + 2y2 = 8 (Wisconsin gold).

cp1 = ContourPlot [ x 2 + 4 x y + y 2 4 = = 0 , { x , 4 , 4 } , { y , 4 , 4 } , Image

PlotPoints 60 , ContourShading False , Image

ContourStyle CMYKColor [ { . 002 , 1 , . 85 , . 06 } ] ] ; Image

cp2 = ContourPlot [ 5 x 2 4 x y + 2 y 2 8 = = 0 , { x , 4 , 4 } , Image

{ y , 4 , 4 } , PlotPoints 60 , Image

ContourShading False , Image

ContourStyle { Dashing [ { 0.01 } ] , Image

CMYKColor [ { . 05 , . 26 , 1 , . 27 } ] } ] ; Image

Show [ cp1 , cp2 , Frame False , Axes Automatic , Image

AxesOrigin { 0 , 0 } , Image

PlotLabel “Nebraska Scarlet and Wisconsin Gold” ] Image

From the graph we see that possible solutions are (0,2)Image and (0,2)Image. In fact, substituting x=0Image and y=2Image and x=0Image and y=2Image into each equation verifies that these points are both exact solutions of the equation. The remaining two solutions are approximated with FindRoot.

FindRoot [ { x 2 + 4 x y + y 2 == 4 , 5 x 2 4 x y + 2 y 2 == 8 } , Image

{ x , 1 } , { y , 0.25 } ] Image

{x1.39262,y0.348155}Image □