introduction to mathematica by david maslanka. 1....

29
Introduction to Mathematica by David Maslanka. Mathematica is a computer algebra system designed to do mathematics. Symbolic, numerical and graphical computations can all be done with Mathematica. Mathematica's treatment of the topics of calculus is thorough. The user may compose Mathematica instructions to carry out all of the fundamental operations that are studied in Calculus I - III, Differential Equations and Linear Algebra. 1. Starting Mathematica_____________________________________________________ When using the Windows version of Mathematica, you may open the program by first clicking on the Start button: This button is located on the left end of the Start bar. Next select All Programs from its menu, Choose Wolfram Mathematica from the program list displayed, and finally, choose Wolfram Mathematica 7 from the drop down menu. You may now copy the link to this program's location and create a shortcut for it on your desktop which will be easily recognizable by its designation with the Mathmatica icon: Whenever Mathematica 7 is started, a blank notebook file will open. It is given the default name: Untitled 1.

Upload: lydat

Post on 14-Apr-2018

243 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

Introduction to Mathematica by David Maslanka.

Mathematica is a computer algebra system designed to do mathematics. Symbolic, numerical and graphical computationscan all be done with Mathematica. Mathematica's treatment of the topics of calculus is thorough. The user may compose Mathematica instructions to carry out all of the fundamental operations that are studied in Calculus I - III, Differential Equations and Linear Algebra.

1. Starting Mathematica_____________________________________________________

When using the Windows version of Mathematica, you may open the program by first clicking on the Start button:

This button is located on the left end of the Start bar. Next select All Programs from its menu, Choose Wolfram Mathematica from the program list displayed, and finally, choose Wolfram Mathematica 7 from the drop down menu.

You may now copy the link to this program's location and create a shortcut for it on your desktop which will be easily recognizable by its designation with the Mathmatica icon:

Whenever Mathematica 7 is started, a blank notebook file will open. It is given the default name: Untitled 1.

Page 2: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

2. Adjusting the Magnification________________________________________________

The default size of the characters displayed in a Mathematica notebook is rather small. However, the magnification can easily be adjusted by clicking on Window along top toolbar and selecting a more readable magnification, e.g. 150%.

3. Entering an Instruction in Mathematica__________________________________________

We will call any typed string of symbols to be executed by Mathematica' s computational engine (or kernel) an instruction. To execute a typed instruction, hit the [Enter] key at the far right of the keyboard. On a laptop or when using the interior [Enter] key on a desktop keyboard, you must strike the [Enter] key while simultaneously holding down the [Shift] key. Instructions entered in the 7.0 version of Mathematica typically appear in black bold type and in Courrier font by default. Some of the elementary symbols used frequently in Mathematica commands are described below. • Integers: Use the keys on the top row of your computer' s keyboard to enter the desired integer.

Use the hyphen character " - " also located along this row to denote a negative number.

• Fractions: To enter the fraction in a command, insert the slash symbol " / " between the numbers p and q.

• Decimals: Decimals are also entered in the natural way.

2 Mathematica Intro.nb

Page 3: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

4. Special Numbers_________________________________________________________

To view the Basic Math Assistant Palette, click on the Palettes button on the Toolbar and then select Basic Math Assistant from its menu.

Note : Mathematica is case sensitive so to indicate the numbers: p, e , i, you must type capitalized first letters or click on the pallete icon indicated above.

Almost any letter or string of letters and numbers can be used to name a variable.However, certain letters and strings have preassigned meanings and are therefore "protected" and cannot be reassigned, e.g.D denotes the differential operator and cannot be used to name a variable quantity.

Mathematica Intro.nb 3

Page 4: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

5. Numerical Operations : Operational Mathematica syntax_________________________

Examples________________________________________________________________________

In[1]:= 2 + 3

Out[1]= 5

In[2]:= 2 − 3

Out[2]= −1

In[3]:= 2 ∗ 3

Out[3]= 6

_______________________________________________________

Note: To compute the product of 2 times 3, we may simply enter: 2 3 in an input cell, i.e., 2[space]3 , and Mathematica will automatically insert the multiplication operator.____________________________________________________________________________________________________

In[4]:= 2 × 3

Out[4]= 6

In[5]:= 2 ê 3

Out[5]=2

3

4 Mathematica Intro.nb

Page 5: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[6]:= 3!

Out[6]= 6

6. Numerical Relations : Relation Mathematica syntax_____________________________

Examples________________________________________________________________________

In[7]:= 3 ≠ 4

Out[7]= True

In[8]:= Pi ≤ E

Out[8]= False

In[9]:= 3 ê 4 0.75

Out[9]= True

Mathematica Intro.nb 5

Page 6: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[10]:= Sqrt@PiD π

Out[10]= True

7. Standard Functions : Function Mathematica syntax______________________________ _

Examples________________________________________________________________________

In[11]:= Log@ED

Out[11]= 1

In[12]:= Log@E, E^2D

Out[12]= 2

In[13]:= Sin@Pi ê 6D

Out[13]=1

2

In[14]:= Abs@−5D 5

Out[14]= True

8. Some Commands for Numerical Calculations___________________________________

We write some more instructions for calculation using the symbols described above.

In[15]:= H3 + 4L ∗ 9

Out[15]= 63

6 Mathematica Intro.nb

Page 7: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[16]:= H3 + 4L 9

Out[16]= 63

In[17]:= 3 ∗ Exp@1D ê 4

Out[17]=3

4

In[18]:= H4 ê 3L Pi ∗ r^3

Out[18]=4 π r3

3

In[19]:= H4 ê 3L Pir^3

Out[19]=4 Pir3

3

__________________________________

Note that (3 + 4)*9 = (3 + 4) 9 and 3*Exp[1]/4=3 Exp[1]/4 but (4/3) Pi*r^3 ∫ (4/3) Pir^3 _____________________________________________________________

In[20]:= 12 Log@Exp@2DD

Out[20]= 24

In[21]:= Sin@Pi ê 6D

Out[21]=1

2

In[22]:= Tan@Pi ê 4D

Out[22]= 1

In[23]:= Abs@−17D

Out[23]= 17

In[24]:= Sqrt@25D ê H2^3L

Out[24]=5

8

Mathematica Intro.nb 7

Page 8: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[25]:= 4! − 2 ∗ 3 ∗ 4

Out[25]= 0

9. Commands for Decimal Representations of Real Numbers________________________

If you would like to obtain a decimal represenation for a particular real number, x, then execute the Mathematica instruction N[ x ]. For example, consider

In[26]:= N@ D

Out[26]= 2.71828

By default, Mathematica will calculate the decimal number to six significant digits. However, this can be changed by adding a digits option to the decimal converter operator.

In[27]:= N@ , 20D

Out[27]= 2.7182818284590452354

In[28]:= NB π , 25F

Out[28]= 1.772453850905516027298167

Note: An alternative way of obtaining the decimal representation for x is by entering: x // N.

In[29]:=

π êê N

Out[29]= 1.77245

10. Commands involving Symbolic Calculations___________________________________

We will apply some commands to polynomial and rational expressions to simplify, expand or collect like terms. First, we construct a polynomial expression:

In[30]:= 8 x^2 + 16 x + 6

Out[30]= 6 + 16 x + 8 x2

Observe that when Mathematica prints a polynomial it always does so in increasing powers of the variable.

8 Mathematica Intro.nb

Page 9: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[31]:= % ê H2 x + 1L

Out[31]=6 + 16 x + 8 x2

1 + 2 x

The percentage symbol "%" stands for "the last output ", so the result of the above instruction is a rational expression in x.

One way to simplify the above rational expression is to use the instruction Simplify[expr] which performs a sequence of algebraic and other transformations on expr, and returns the simplest form it finds.

In[32]:= Simplify@%D

Out[32]= 6 + 4 x

Another way of simplifying the expression is to invoke the instruction Apart[expr]. This instruction writes the rational expression expr in terms of its partial fractions decomposition.

In[33]:= ApartB6 + 16 x + 8 x2

1 + 2 xF

Out[33]= 6 + 4 x

Yet another way to fully simplify the rational expression is to invoke the instruction Factor[poly].

In[34]:= FactorB6 + 16 x + 8 x2

1 + 2 xF

Out[34]= 2 H3 + 2 xL

Note that Factor[poly] factors the polynomial poly over the integers.

The instruction Expand[expr] is used to write the expression expr as a sum of simple terms. For example, consider

In[35]:= Expand@H2 x + 3L H4 x + 2LD

Out[35]= 6 + 16 x + 8 x2

In order to displaythe polynomial in decreasing powers of x we may inmvoke the TraditionalForm instruction:

In[36]:= TraditionalForm@%DOut[36]//TraditionalForm=

8 x2 + 16 x + 6

In the above instruction, Mathematica expanded the product of the two linear expressions in x. Of course, this instruction may be applied to more complicated polynomial expressions.

Mathematica Intro.nb 9

Page 10: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[37]:= Expand@2 Hx^2 + xL + 5 x + 3 + 3 H2 x^2 + 3 x + 1LD

Out[37]= 6 + 16 x + 8 x2

11. Commands to Assign Names or Values to Expressions__________________________

It is often useful to assign names to mathematical expressions. This can always be accomplished with an instruction of the form name = expr which assigns to the variable called name the value expr. Here, expr may denote any mathematical expression. For example, consider the following calculations involving assigned variables:

In[38]:= y = 8 x2 + 16 x + 6

Out[38]= 6 + 16 x + 8 x2

In[39]:= Y = y ê H2 x + 1L

Out[39]=6 + 16 x + 8 x2

1 + 2 x

In[40]:= Simplify@YD

Out[40]= 6 + 4 x

In[41]:= Z = y ∗ Y

Out[41]=I6 + 16 x + 8 x2M2

1 + 2 x

In[42]:= Z1 = Simplify@ZD

Out[42]= 4 H1 + 2 xL H3 + 2 xL2

In[43]:= Z2 = Expand@Z1D

Out[43]= 36 + 120 x + 112 x2 + 32 x3

An instruction of the form expr/.varØa will replace the variable var with the value of a in the expression expr.For example consider the following instructions:

In[44]:= Z1 ê. x → 2

Out[44]= 980

10 Mathematica Intro.nb

Page 11: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[45]:= Y ê. x → −3 ∗ v

Out[45]=6 − 48 v + 72 v2

1 − 6 v

12. Commands for Defining Functions__________________________________________ The simplest way to define a function f in Mathematica is with a mapping instruction of the form :

f [ x_ ] : = expr

which assigns the name of f to the mapping of the variable x into the expression expr (where, of course, expr involves x in the case the function f is to be nonconstant). Note the underscore after the variable x in the definition. It must be inserted in order for the kernel to recognize that x denotes the independent variable in this definition of the function f. Consider the following examples :

In[46]:= f@x_D:=x3−2 x+1

In[47]:= f@xD

Out[47]= 1 − 2 x + x3

In[48]:= f@5D

Out[48]= 116

In[49]:= g@x_D := Cos@3 xD

In[50]:= g@PiD

Out[50]= −1

In[51]:= f@g@xDD

Out[51]= 1 − 2 Cos@3 xD + Cos@3 xD3

Note the difference between the functions f and g defined above and the expression y defined in Mathematica by:

In[52]:= y = x + Abs@xD

Out[52]= x + Abs@xD

To evaluate this expression y when x = 5, we must use the more complicated command :

In[53]:= y ê. x → 5

Out[53]= 10

However, we can convert this expression into a function, k, with an instruction of the form : k[x_]= y/. x Æ x which assigns the name k to the mapping of the variable x into the expression y.

Mathematica Intro.nb 11

Page 12: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[54]:= k@x_D = y ê. x → x

Out[54]= x + Abs@xD

In[55]:= k@5D

Out[55]= 10

Sometimes, we may need to define a piecewise function in Mathematica. To illustrate one way of doing this, consider the following example. Suppose that

f (x) = x2 if x < -1-2 + x if - 1 < x < 1x3 otherwise

To define this function, we may enter the instruction:

In[56]:= f@x_D := Piecewise@88x^2, x <= −1<, 8x − 2, −1 < x < 1<<, x^3D

In[57]:= f@xD

Out[57]=

x2 x ≤ −1−2 + x −1 < x < 1

x3 True

In general, the instruction f[x_] = Piecewise@88val1, cond1<, 8val2, cond2<, ..., 8valn, condn<<, val D assigns the value vali to the function f at x in case the condition condi is TRUE. It assigns the value val to x

whenever none of the conditions condi , i = 1, 2, ..., n are TRUE. Examples_______________________________________________________________________________________

In[58]:= p@x_D := Piecewise@88x^2, x < 1<, 82 − x, 1 <= x ≤ 2<, 82 x − 2, x > 2<<D

In[59]:= p@xD

Out[59]=

x2 x < 12 − x 1 ≤ x ≤ 2−2 + 2 x x > 20 True

In[60]:= q@x_D := Piecewise@88x, x < 3<<, 3D

In[61]:= q@xD

Out[61]= ∂ x x < 33 True

In[62]:= p@4D

Out[62]= 6

In[63]:= p@2D

Out[63]= 0

12 Mathematica Intro.nb

Page 13: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[64]:= q@5D

Out[64]= 3

In[65]:= q@a + bD

Out[65]= ∂ a + b a + b < 33 True

In[66]:= PiecewiseExpand@Abs@xD, x ∈ RealsD

Out[66]= ∂ −x x < 0x True

13. Commands for Solving Equations___________________________________________

An instruction of the form Solve [equa , vars ] asks Mathematica to solve the equation equa in terms of the variables vars. For example, consider

In[67]:= y = SolveAx5 + 12 x3 − 4 x == 0, xE

Out[67]= :8x → 0<, :x → − 2 K−3 + 10 O >, :x → 2 K−3 + 10 O >,

:x → − 2 K3 + 10 O >, :x → 2 K3 + 10 O >>

Each of the five solutions listed above has a unique name. To pick out the second solution, we enter:

In[68]:= x ê. y@@2DD

Out[68]= − 2 K−3 + 10 O

Similarly, the fourth solution is designated by:

In[69]:= x ê. y@@4DD

Out[69]= − 2 K3 + 10 O

Mathematica can often solve equations which have an infinite number of solutions with the Reduce instruction. Reduce[ expr, vars, dom ] executes the reduction over the domain dom. Common choices of dom are Reals, Integers and Complexes.

Recall how the function k (x) = x + | x | which was defined above and consider the command:

In[70]:= k@xD

Out[70]= x + Abs@xD

Mathematica Intro.nb 13

Page 14: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[71]:= Reduce@x + Abs@xD 0, xD

Out[71]= Re@xD ≤ 0 && Im@xD 0

In[72]:= Reduce@x + Abs@xD 0, x, RealsD

Out[72]= x ≤ 0

However, sometimes it may be too complicated for Mathematica to express all of the solution to an equation in terms of the usual operations of algebra.

In[73]:= Solve@x^6 + 12 ∗ x^3 − 4 ∗ x == 0, xD

Out[73]= 98x → 0<, 9x → RootA−4 + 12 12 + 15 &, 1E=,

9x → RootA−4 + 12 12 + 15 &, 2E=, 9x → RootA−4 + 12 12 + 15 &, 3E=,

9x → RootA−4 + 12 12 + 15 &, 4E=, 9x → RootA−4 + 12 12 + 15 &, 5E==

In such case, to find all numerical values of an expression involving Root, we may use the instruction N[%] :

In[74]:= N@%D

Out[74]= 88x → 0.<, 8x → −2.23744<, 8x → −0.582156<,8x → 0.57288<, 8x → 1.12336 − 2.02449 <, 8x → 1.12336 + 2.02449 <<

For a nonpolynomial equation the Solve and Reduce instructions may still be invoked to find its solutions.

In[75]:= Solve@Cot@π ∗ xD 0, xD

Solve::ifun :

Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution

information.à

Out[75]= ::x → −1

2>, :x →

1

2>>

In[76]:= Reduce@Cot@π ∗ xD 0, x, RealsD

Out[76]= C@1D ∈ Integers && x

π

2+ π C@1D

π

14. Instructions for Plotting___________________________________________________

I . Plotting Expressions and Functions of a Single Variable. In order to plot the expression expr involving the single variable var over the interval [ a , b ] , we use an insrtuction of the form: Plot [ expr , {var , a , b} ] . For example, suppose

14 Mathematica Intro.nb

Page 15: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[77]:= y = 3 x4 − 4 x3

Out[77]= −4 x3 + 3 x4

In[78]:= Plot@y, 8x, −2, 2<D

Out[78]=

-2 -1 1 2

10

20

30

40

Note that the general shape of this graph is not easily recognized due to the large values that y attains when x lies in the interval [ -2 , -1 ]. We can restrict the range of the plotted curve to the interval [ c , d ] by entering : Plot [ expr , {var , a , b} , PlotRange Ø { c , d } ]

In[79]:= Plot@y, 8x, −2, 2<, PlotRange → 8−1, 4<D

Out[79]=

-2 -1 1 2

-1

1

2

3

4

Now consider the following function described in terms of the absolute valus\e and the signum functions:

In[80]:= h@t_D := Abs@tD1

3 × Sign@tD

Mathematica Intro.nb 15

Page 16: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[81]:= h@tD

Out[81]= Abs@tD1ê3 Sign@tD

The signum function is denoted Sign in Mathematica, is defined by Sign( x ) = -1 if x < 0

0 if x = 01 if x > 0

.

Therefore, we can express the function h more simply as: h ( t ) = t1

3 . In order to plot h over the interval x œ [ -10 , 10 ] we input:

In[82]:= Plot@h@tD, 8t, −10, 10<D

Out[82]=-10 -5 5 10

-2

-1

1

2

In[83]:= PlotBt1

3 , 8t, −10, 10<F

Out[83]=

-10 -5 5 10

0.5

1.0

1.5

2.0

As before, we may restrict the plot of the range of this function to the interval [ c , d ] by utilizing the Plot option: PlotRange Ø { c , d }

16 Mathematica Intro.nb

Page 17: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[84]:= Plot@h@tD, 8t, −10, 10<, PlotRange → 8−1.5, 1.5<D

Out[84]=-10 -5 5 10

-1.5

-1.0

-0.5

0.5

1.0

1.5

The option PlotStyle Æ {Thickness[ n ] } may be inserted in a plot command to control the width of the plot of the curve. The value of n denotes the thickness of lines as a fraction of the total width of the graphic.

In[85]:= Plot@h@tD, 8t, −5, 5<, PlotRange → 8−1.5, 1.5<, PlotStyle → [email protected]<D

Out[85]=-4 -2 2 4

-1.5

-1.0

-0.5

0.5

1.0

1.5

The color of the curve can also be sepecified using the PlotStyle option in a Plot instruction.

Mathematica Intro.nb 17

Page 18: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[86]:= Plot@h@tD, 8t, −5, 5<, PlotRange → 8−1.5, 1.5<, PlotStyle → 8Orange, [email protected]<D

Out[86]=-4 -2 2 4

-1.5

-1.0

-0.5

0.5

1.0

1.5

II . Plotting Functions of One Variable Possessing Jump Discontinuities.

Recall the piecewise defined function f from the example in Section 11 :

In[87]:= Clear@fD

In[88]:= f@x_D := Piecewise@88x^2, x <= −1<, 8x − 2, −1 < x < 1<<, x^3D

In[89]:= f@xD

Out[89]=

x2 x ≤ −1−2 + x −1 < x < 1

x3 True

In[90]:= Plot@f@xD, 8x, −3, 2<, PlotStyle → 8Thick<D

Out[90]=

-3 -2 -1 1 2

-2

2

4

6

8

III . Plotting Several Functions or Expressions Together.

The forms of the commands for plotting several functions or expressions together are analogous to those described above in section I. However, the set of functions/expressions to be plotted must be enclosed by curly braces " { } " within the Plot instruction. Consider the following examples:

( a ) Plotting two or more expressions together

18 Mathematica Intro.nb

Page 19: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[91]:= w = 3 x4 − 4 x3 − 12 x2 + 5

Out[91]= 5 − 12 x2 − 4 x3 + 3 x4

In[92]:= v = 10 x3 − 43 x

Out[92]= −43 x + 10 x3

In[93]:= Plot@8w, v<, 8x, −2, 2<, PlotStyle → 8Red, Green, Thick<D

Out[93]=-2 -1 1 2

-30

-20

-10

10

20

30

In[94]:= Plot@8w, v<, 8x, −2, 2<, PlotStyle → 8Green, Red, Thick<D

Out[94]=-2 -1 1 2

-30

-20

-10

10

20

30

( b ) Plotting two or more functions together

In[95]:= g@x_D := x2

In[96]:= h@x_D := x3

Mathematica Intro.nb 19

Page 20: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[97]:= Plot@8g@xD, h@xD<, 8x, −2, 2<, PlotStyle → 8Blue, Magenta, Thick<D

Out[97]=

-2 -1 1 2

-4

-2

2

4

6

8

( c ) Plotting functions and expressions together

In[98]:= Plot@8g@xD, h@xD, w, v<, 8x, −2, 2<,PlotStyle → 8Orange, Green, Blue, Magenta, [email protected]<D

Out[98]=-2 -1 1 2

-30

-20

-10

10

20

30

IV. Plotting curves together with different linestyles for distinction.

20 Mathematica Intro.nb

Page 21: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[99]:= Plot@8g@xD, h@xD, w, v<, 8x, −2, 2<,PlotStyle → 88Dashing@TinyD, Red<, 8Dashing@SmallD, Blue<, 8Dashing@MediumD,

Darker@GreenD<, 8Dashing@LargeD, Black<<D

Out[99]=-2 -1 1 2

-30

-20

-10

10

20

30

In[100]:= Plot@8g@xD, h@xD, w, v<, 8x, −2, 2<,PlotStyle → [email protected], [email protected], [email protected], [email protected]<D

Out[100]=-2 -1 1 2

-30

-20

-10

10

20

30

V. Plotting Equations in Two Variables.

The instruction: ContourPlot [ equ , { x, xmin , xmax }, { y, ymin , ymax } ] plots the equation equ in the variables x and y over the ranges [ xmin , xmax ] and [ ymin , ymax ] for the variables x and y respectively. Consider the following example.

Mathematica Intro.nb 21

Page 22: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[101]:= ContourPlot@x^4 + H−4 + yL y^3 + 2 x^2 H−2 − 2 y + y^2L 0, 8x, −4.7, 4.7<, 8y, −3, 5<D

Out[101]=

-4 -2 0 2 4

-2

0

2

4

In order to use the same scale for a unit length along both the x and y axes, we include the option AspectRatioÆAutomatic in the Plot instruction.

In[102]:= ContourPlot@x^4 + H−4 + yL y^3 + 2 x^2 H−2 − 2 y + y^2L 0,8x, −4.7, 4.7<, 8y, −3, 5<, PlotPoints → 40, AspectRatio → AutomaticD

Out[102]=

-4 -2 0 2 4

-2

0

2

4

To make the x and y axes appear in the graph of the cardioid, and to eliminate the box cucumscribing it, we incude the option AxesÆTrue, FrameÆFalse in the Plot instruction.

22 Mathematica Intro.nb

Page 23: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[103]:= ContourPlot@x^4 + H−4 + yL y^3 + 2 x^2 H−2 − 2 y + y^2L 0, 8x, −4.7, 4.7<,8y, −3, 5<, AspectRatio → Automatic, Axes → True, Frame → FalseD

Out[103]=

-4 -2 2 4

-2

2

4

To adjust the color and thickness of the graph, we use the option ContourStyleÆ{ Color, Thickness[ .N1N2N3] } in the ContourPlot instruction.

In[104]:= M = ContourPlot@x^4 + H−4 + yL y^3 + 2 x^2 H−2 − 2 y + y^2L 0,8x, −4.7, 4.7<, 8y, −3, 5<, AspectRatio → Automatic, Axes → True, Frame → False,ContourStyle → 8Magenta, [email protected]<, ContourShading → 8Purple<D

Out[104]=

-4 -2 2 4

-2

2

4

Mathematica Intro.nb 23

Page 24: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[105]:= RegionPlot@x^4 + H−4 + yL y^3 + 2 x^2 H−2 − 2 y + y^2L <= 0,8x, −4.7, 4.7<, 8y, −3, 5<, AspectRatio → Automatic, Axes → True,Frame → False, BoundaryStyle → 8Magenta, Thick<, PlotStyle → YellowD

Out[105]=

-4 -2 2 4

-2

2

4

15. Instructions for Adding Labels to Graphs____________________________________

In order to add text to a plot, we must

(i) assign a name to the plot, e.g. name1=Plot[....] or name1 = ContourPlot[...]text

(ii) construct a text instruction of the form: name2 = Graphics[Text["expr",{xo, yo<, TextStyle Æ 8Fontsize Æ nmbr<, clrDD; in order to add the text expr centered at the point (xo, yo) with font size nmbr and in the color clr.

(iii) Execute the instruction: Show[ name1, name2 ] in order to display both the plot and the text in a single coordinate plane. For instance, we may add a text desription to the graph of the ardioid plotted above.

In[106]:= T = GraphicsATextA"The cardioid: x4+Hy−4Ly3+2x2Hy2−2y−2L=0",

80, 4.5<, TextStyle → 8FontSize → 12, Bold, Orange<EE;

24 Mathematica Intro.nb

Page 25: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[107]:= Show@M, TD

Out[107]=

The cardioid: x4+Hy-4Ly3+2x2Hy2-2y-2L=0

-4 -2 2 4

-2

2

4

Observe that the cardiod passes has the intercepts: ( 0 , 0 ), ( +2 , 0 ), ( 0 , 4 ). We may highlight these points by adding blue circular disks at each of these intercepts in the figure using the instruction: Disk[ { x , y } , r ] .

In[108]:= D1 = Graphics@8Blue, Disk@80, 0<, 0.075D<D;D2 = Graphics@8Blue, Disk@82, 0<, 0.075D<D;D3 = Graphics@8Blue, Disk@8−2, 0<, 0.075D<D;D4 = Graphics@8Blue, Disk@80, 4<, 0.075D<D;

Mathematica Intro.nb 25

Page 26: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[112]:= Show@M, T, D1, D2, D3, D4D

Out[112]=

The cardioid: x4+Hy-4Ly3+2x2Hy2-2y-2L=0

-4 -2 2 4

-2

2

4

16. Commands for Differentiation and Integration_________________________________ Enter the instruction D [ expr , var ] to differentiate the expression expr with respect to the variable

In[113]:= S = t4 − t3 + t2

Out[113]= t2 − t3 + t4

In[114]:= D@S, tD

Out[114]= 2 t − 3 t2 + 4 t3

In[115]:= F@x_D = Cos@xD − Sin@xD

Out[115]= Cos@xD − Sin@xD

In[116]:= D@F@xD, xD

Out[116]= −Cos@xD − Sin@xD

The derivative of a function, F, may also be found using the instruction F ' [ x ].

In[117]:= F'@xD

Out[117]= −Cos@xD − Sin@xD

26 Mathematica Intro.nb

Page 27: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[118]:= F'@πD

Out[118]= 1

Enter the instruction:

Integrate [ expr , var ]

to obtain an antiderivative, (no constant of integration appears), of the given expression expr with respect to the variable var . Alternatively, you may use the integral operator located in the Calculus section of the Basic Commands palette.

In[119]:= Integrate@S, tD

Out[119]=t3

3−

t4

4+

t5

5

In[120]:= ‡ S t

Out[120]=t3

3−

t4

4+

t5

5

In[121]:= Integrate@F@xD, xD

Out[121]= Cos@xD + Sin@xD

In[122]:= ‡ F@xD x

Out[122]= Cos@xD + Sin@xD

In[123]:= TraditionalForm@Integrate@F@xD, xDDOut[123]//TraditionalForm=

sinHxL + cosHxL

In[124]:= sinHxL + cosHxL

Out[124]= Cos@xD + Sin@xD

To obtain the definite integral of an expression, we use a command of the form

Integrate [ expr , {var , a , b } ] . Alternatively, you may also use the Basic Commands Pallette to enter the definite integral.

Mathematica Intro.nb 27

Page 28: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[130]:= Integrate@S, 8t, 0, 2<D

Out[130]=76

15

In[131]:= ‡0

2S t

Out[131]=76

15

In[132]:= Integrate@S, 8t, 0, 2<D

Out[132]=76

15

In[133]:= Integrate@F@xD, 8x, 1, 5<D

Out[133]= −Cos@1D + Cos@5D − Sin@1D + Sin@5D

28 Mathematica Intro.nb

Page 29: Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system

In[134]:= ‡1

5F@xD x

Out[134]= −Cos@1D + Cos@5D − Sin@1D + Sin@5D

17. Inserting a New Input Cell Between Two Existing Instructions____________________

In order to insert a new line of input between two existing Mathematica instructions, just slide the mouse vertically in the notebook window to the desired location. The vertical bar "I" tracing the mouse's position will chage to a horizontal one " >--< " when positioned between two adjacent input cells. When the horizontal bar is attained, right click the mouse. Then a pop-up menu and a vertical line across the page should appear. Now select Insert New Cell Ø Input from this menu. A new input cell now will appear between the two existing instructions.

18. Instructions for Adding Text to Mathematica Notebooks_________________________

Mathematica may be used as a word processor to add text to a notebook file. Once an instruction is executed, the program is expecting a new instruction. In order to enter text to the document instead, simply click on [Create Text Cell] from the Basic Calculator portion of the Basic Math assistant Palette. All characters entered now will appear in Times Roman font by default. This type further indicates that these symbols are in text format. Mathematica instuctionss cannot be entered while in text mode. After you have completed typing all your comments in the section you may return to executing Mathematica instructions by clicking [Create Input Cell] on the Basic Calculator portion of the Basic Math assistant Palette. \

To insert mathematical symbols or expressions while in text mode, you may use the features of theTypestting portion of the Basic Math Assistant Palette. The palette works much like the Equation Editor does in MS Word. You may also copy TraditionalForm output from a Mathematica instruction into a text cell. You may also insert a new text cell between two existing cells using a procedurs analogous to that described in section 17 for nserting a new inpur cell between two existing cells.

Mathematica Intro.nb 29