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.
29
Embed
Introduction to Mathematica by David Maslanka. 1. …mypages.iit.edu/~maslanka/Introtomath.pdfIntroduction to Mathematica by David Maslanka. Mathematica is a computer algebra system
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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.
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.
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
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.
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.____________________________________________________________________________________________________
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
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
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.
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
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 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
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
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
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
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
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
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.
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
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
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 }
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.
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:
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.
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.
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",
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 ] .
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
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.
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
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
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.