Introduction to Mathematica : Project 0 Mathematica is a computer algebra system which can do symbolic and numerical computations. We will use Mathematica for projects in Math 2080. You'll submit your projects as Mathematica notebooks (like this one). Your notebook will include written explanations for your work as well as computations needed to answer the project questions. This assignment will give you an introduction to Mathematica and some of the features you will need for Math 2080 projects. Mathematica is very well documented. You should utilize the Wolfram Documentation Center (https://reference.wolfram.com/language/ ) as a reference for functions, symbols, and syntax in Mathematica. The quick introductory math tutorial (https://www.wolfram.com/language/fast-introduction-for-math-students/en// ) may also be useful. To complete Project 0, read through this document carefully and complete all exercises in the notebook using Mathematica. Unlike future projects in this course, this project will count for participation points rather than as a project component. Future projects may be completed in groups of up to 3 students. This assignment, however, must be submitted individually so that everyone has a basic introduction to using Mathematica. You are welcome to talk with other classmates as you work through the exercises. After completing this assignment, save your Mathematica Notebook and upload it to the Project 0: Introduction to Mathematica Assignment on Gradescope. Name: Hudson Wunderlich Part 1: Formatting Mathematica notebooks are organized in cells, indicated by brackets on the right. There are numerous cell styles, but the ones most relevant to you are input cells, where you will type code for computations, and text cells, where you will type explanations. The toolbar at the top of the notebook has a dropdown menu to select the cell style. In input cells, you can define functions, plot graphs, and perform computations. To evaluate an input cell, press Shift+Enter. You will see the output in a cell appearing below the input cell. In[n] and Out[n] Printed by Wolfram Mathematica Student Edition 2 Multivar Lab 1.nb label successive inputs and outputs. The % symbol refers to the most recent output. Mathematica uses standard symbols such as * for multiplication, / for division, and ^ for exponentiation. Use parentheses ( ), not braces { }, square brackets [ ] or angle brackets < > for grouping mathematical expressions. Exercise: ◼ Add an input cell below this line and calculate 2+2 using Mathematica. In[2]:= 2+2 Out[2]= 4 ◼ Add an input cell below this line and calculate 1+2+3 using Mathematica. In[3]:= 1+2+3 Out[3]= 6 ◼ Add an input cell below this line and calculate 1+2+3+4 by using % to represent the previous answer and adding 4. In[4]:= %+4 Out[4]= 10 ◼ Add an input cell below this line and calculate the quantity 4 In[6]:= (1 + 2)3 64(1/2) using Mathematica. 4 * (((1 + 2) ^ 3) / 64 ^ (1 / 2)) 27 Out[6]= 2 In your project explanations, you will need to be able to typeset mathematical symbols. If you are familiar with typesetting in T EX, you can click the Insert T EX button on the toolbar at the top of the screen ($x$), and type mathematics using T EX commands. Otherwise, you can click the Insert Math button (with symbols ∫, π, and Σ) for a toolbox of mathematical symbols. If you click the insert menu and then click Special Character, you will see a toolbox of various letters and symbols which may be useful. In your project explanations, you will need to be able to typeset mathematical symbols. If you are familiar with typesetting in TEX, you can click the Insert TEX button on the toolbar at the top of the screen ($x$), and type mathematics using T EX commands. Otherwise, you can click the Insert Math button (with symbols ∫, π, and Σ) for a toolbox of mathematical symbols. If you click the insert menu Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb and then click Special Character, you will see a toolbox of various letters and symbols which may be useful. Exercise: In the text cell below, use either TEX commands or the mathematical symbols toolbox to type an integral involving a square root, a fraction, and the number π. In[7]:= frac {Sqrt4} {2 } x Out[7]= {2 frac Sqrt4 x } Part 2: Variables and Functions Variable names in Mathematica start with letters and may also contain numbers. For example in the following, we divide a variable named a1 by 2. a1 / 2 O u t [ ] = a1 2 A space between variables or numbers indicates multiplication. So "a b" means a times b, and "ab" is a variable named ab. Exercise: There are three variables in the expression below: a b + 5 ab ^ 2 Type the names of the three variables: 1. a 2. b 3.ab You can set a variable equal to a quantity using the = symbol. Examples: Below, we set x equal to 2. Afterwards, subsequent calculations will treat x as the number 2. Printed by Wolfram Mathematica Student Edition 3 4 Multivar Lab 1.nb x = 2 O u t [ ] = 2 (1 + 2 x) ^ 2 O u t [ ] = 25 To clear the value from x and treat it formally as a variable, use the Clear function. I n [ ] : = Clear[x] After running the cell above, x is just a variable, no longer equal to 2. You can see this from the output below. (1 + 2 x) ^ 2 O u t [ ] = (1 + 2 x)2 Notice that the Clear function uses square brackets [ ]. The square brackets are used to define and evaluate functions in Mathematica. You can define your own function f(x) with the construction f[x_]:= x_ means that x is a pattern that can have any value substituted for it. := means that any argument passed to f is substituted into the right hand side upon evaluation Examples: In this example, we define a function f (x) 1 + 2 x and then evaluate it at 2, 5, 2.11, ab, and 3 x2 + 72. I n [ ] : = f[x_] := 1 + 2 x f[2] O u t [ ] = 5 f[5] O u t [ ] = 11 f[2.11] O u t [ ] = 5.22 f[ab] O u t [ ] = 1 + 2 ab Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb f[3 x ^ 2 + 72] O u t [ ] = 1 + 2 72 + 3 x2 Exercise: ◼ Define the function g (x) (1 + x)3 in Mathematica (make sure the variable x is cleared). ◼ Calculate g(2),g(-1), and In[8]:= clear[x] Out[8]= clear[x] In[9]:= clear[g] Out[9]= clear[g] In[10]:= g[x_] := (1 + x) ^ 3 In[15]:= g[2] g (x + h) - g (x) h using Mathematica. Out[15]= 27 In[16]:= g[- 1] Out[16]= 0 In[17]:= frac {g[x + h] - g[x]} {h} Out[17]= frac h - (1 + x)3 + (1 + h + x)3 Built-In Functions In Mathematica, the special constants π and e are Pi and E, respectively (notice the capital letters). Mathematica has A LOT of built-in functions. Built in functions start with capital letters and utilize square brackets [ ]. We give some examples of built-in functions below. There are more built in functions listed under the Basic Math Assistant found under the Palettes menu. Consult the Wolfram Documentation Center for even more of them. Printed by Wolfram Mathematica Student Edition 5 6 Multivar Lab 1.nb Examples: Sin[Pi / 2] O u t [ ] = 1 Cos[2 Pi] O u t [ ] = 1 ArcTan[1] O u t [ ] = π 4 E^2 O u t [ ] = 2 The Natural logarithm in Mathematica is Log, not Ln. Log[E ^ 2] O u t [ ] = 2 Log[10] O u t [ ] = Log[10] The function N[ ] gives numerical approximations of numbers. N[Log[10]] O u t [ ] = 2.30259 You can also include a decimal with your number to output a numerical approximation instead of an exact answer. N[Log[10.]] O u t [ ] = 2.30259 Sqrt[4] O u t [ ] = 2 Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb Exercise: Calculate the following using Mathematica: ◼ sin(π/3) In[21]:= Sin[Pi / 3] Out[21]= 3 2 ◼ arccos 3 2 In[24]:= ArcCos[3 ^ (1 / 2) / 2] Out[24]= π 6 ◼ A numerical approximation of the natural logarithm of 100, ln(100) In[25]:= N[Log[100]] Out[25]= 4.60517 Part 3: Vectors In Mathematica, n-dimensional vectors are represented by lists of length n. These are written between curly braces { }. Examples: In the input cells below, we define two vectors v and w, and compute the length (Norm) of v, the scalar multiple -5w, and the sum, cross product and dot product of v and w. Notice that we have a semicolon ";" after we define the vectors. When we run the cell, this defines the vectors but suppresses the output. I n [ ] : = v = {1, 5, 7}; w = {Pi, 10, - 3}; Norm[v] O u t [ ] = 5 3 -5 w O u t [ ] = {- 5 π, - 50, 15} Printed by Wolfram Mathematica Student Edition 7 8 Multivar Lab 1.nb v + w O u t [ ] = {1 + π, 15, 4} Dot[v, w] O u t [ ] = 29 + π We can also use the symbol "." to compute the dot product. v.w O u t [ ] = 29 + π Cross[v, w] O u t [ ] = {- 85, 3 + 7 π, 10 - 5 π} I n [ ] : = Clear[v, w] Exercise: Redefine the vectors v and w so that v=〈2,-3,5〉 and w=〈-1,2,1〉. Then answer the following questions about v and w. ◼ Use Mathematica to compute 5v-6w. In[26]:= v = {2, - 3, 5}; w = {- 1, 2, 1}; (5 * v) - (6 * w) Out[28]= {16, - 27, 19} ◼ Compute the dot product of v and w, and find the angle between v and w using Mathematica. In[35]:= Dot[v, w] N[ArcCos[(v.w) / (Norm[v] Norm[w])]] Out[35]= -3 Out[36]= 1.77081 ◼ Compute the cross product of v and w In[30]:= Cross[v, w] Out[30]= {- 13, - 7, 1} Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb Part 4: Calculus Mathematica can compute integrals, limits and derivatives. Examples: Below we compute the indefinite integral of e-x with respect to x by selecting the appropriate function from the Basic Math Assistant Palette (in the cloud version, under Insert, Special Characters) ∫^(-x) x O u t [ ] = - -x We can also compute this integral using the Integrate function, as shown below. Note that this function takes 2 arguments: the function you want to integrate and the variable of integration, separated by a comma. Integrate[E ^ (- x), x] O u t [ ] = - -x We can similarly compute definite integrals and even improper integrals, either by using a Palette or with the Integrate command. Integrate[E ^ (- x), {x, 2, Infinity}] O u t [ ] = 1 2 Below we compute the derivative of x5 - x with respect to x. Notice that this uses the same notation as the Integrate function. D[x ^ 5 - Sqrt[x], x] O u t [ ] = 1 2 + 5 x4 x Finally, we compute lim x2 x2 + 2 x - 4 3 x2 - 1 . Limit[(x ^ 2 + 2 x - 4) / (3 x ^ 2 - 1), x 2] O u t [ ] = 4 11 The arrow used in our limit command is typeset by typing - followed by >. Printed by Wolfram Mathematica Student Edition 9 10 Multivar Lab 1.nb Exercise: Use Mathematica to compute the following: ◼ Compute f ′ (x) if f (x) tan 3 x2 In[39]:= D[Tan[3 (x ^ 2)], x] Out[39]= 6 x Sec3 x2 2 1 1 0 4 + x2 ◼ Compute In[41]:= dx Integrate[(1) / (4 + (x ^ 2)), {x, 0, 1}] Out[41]= 1 1 ArcTan 2 2 ◼ Compute lim x0 In[43]:= 1 x Limit[1 / x, x 0] Out[43]= Indeterminate Part 5: Graphing To generate a graph of a function of 1 variable, use the syntax Plot[f[x],{x,min,max}]. Here the first argument is the function f, and the second argument {x,min,max} is the domain of the plot. Example: Plot[Sin[x], {x, 0, 2 Pi}] O u t [ ] = 1.0 0.5 1 2 3 4 5 6 -0.5 -1.0 Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb Exercise: 2 Plot e-x on the interval [-2,2]. Example: Use ParametricPlot to plot a set of parametric equations : ParametricPlot[{2 Cos[t] - Cos[2 t], 2 Sin[t] - Sin[2 t]}, {t, 0, 2 Pi}] O u t [ ] = 2 1 -3 -2 1 -1 -1 -2 In Math 2080, we will also see many 3D plots of curves and surfaces. We can use Plot3D to plot a Cartesian curve or surface : Printed by Wolfram Mathematica Student Edition 11 12 Multivar Lab 1.nb Plot3D[x ^ 2 - y ^ 2, {x, - 3, 3}, {y, - 3, 3}] O u t [ ] = ParametricPlot3D will plot parametric curves and surfaces in 3 dimensions: ParametricPlot3D[{Sin[t], Cos[t], t / 10}, {t, 0, 20}] O u t [ ] = Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb ParametricPlot3D[{(2 - Cos[r]) * Cos[t], (2 - Cos[r]) * Sin[t], Sin[r]}, {r, 0, 2 Pi}, {t, 0, 2 Pi}] O u t [ ] = If you want to graph two functions on the same plot, you can do the following: A = Plot[Cos[t], {t, 0, 2 Pi}]; B = Plot[Sin[t], {t, 0, 2 Pi}, PlotStyle Orange]; Show[A, B] O u t [ ] = 1.0 0.5 1 2 3 4 5 6 -0.5 -1.0 An alternate method is: Printed by Wolfram Mathematica Student Edition 13 14 Multivar Lab 1.nb Plot[{Cos[t], Sin[t]}, {t, 0, 2 Pi}] O u t [ ] = 1.0 0.5 1 2 3 4 5 6 -0.5 -1.0 Part 6: Solving Equations Commands like Solve find exact solutions to equations . Use == to set sides equal to one another and indicate what you are solving for after the comma. Example: Solve[x ^ 2 + 2 x - 6 0, x] O u t [ ] = x - 1 - 7 , x - 1 + 7 NSolve[x ^ 2 + 2 x - 6 0, x] O u t [ ] = {{x - 3.64575}, {x 1.64575}} Note that if there are decimals in your equation, Mathematica will give approximate results automatically. Solve[x ^ 2 + 2 x - 6.0 0, x] O u t [ ] = {{x - 3.64575}, {x 1.64575}} You can use lists to solve systems of equations for more than one variable. Solve[{x ^ 2 + 5 y, 7 x - 5 y}, {x, y}] O u t [ ] = {{x 2, y 9}, {x 5, y 30}} Exercise: ◼ Use Mathematica to find an exact solution to the equation2 x3 - 5 x2 - x + 6 0. Printed by Wolfram Mathematica Student Edition Multivar Lab 1.nb In[45]:= Solve[2 (x ^ 3) - 5 (x ^ 2) - x + 6 0, x] Out[45]= {x - 1}, x 3 2 , {x 2} ◼ Use Mathematica to find a numerical solution to the equation cos (x) x. In[46]:= NSolve[Cos[x] x, x] Out[46]= {{x 0.739085}, {x - 2.48689 + 1.80936 }, {x - 9.10999 + 2.95017 }, {x - 15.488 + 3.45661 }, {x - 21.819 + 3.79031 }, {x - 28.1316 + 4.03995 }, {x - 34.435 + 4.23954 }, {x - 40.7329 + 4.40585 }, {x - 47.0274 + 4.54842 }, {x - 53.3196 + 4.67319 }, {x - 59.6102 + 4.78411 }, {x - 65.8995 + 4.88396 }, {x - 72.1878 + 4.97474 }, {x - 78.4754 + 5.05797 }, {x - 84.7625 + 5.1348 }, {x - 91.0491 + 5.20615 }} Conclusion You have reached the end of Project 0.This notebook provides a quick introduction to some of the Mathematica features used in Math 2080, but Mathematica has much greater capabilities. For instructions on how to use Mathematica for computations not addressed in this lab as well as for general Mathematica troubleshooting, the Wolfram Documentation Center (https://reference.wolfram.com/language/ ) is your friend! Go back through the notebook and make sure that you have completed all exercises. Then save your notebook and upload it to the Project 0: Introduction to Mathematica Assignment on Gradescope. Printed by Wolfram Mathematica Student Edition 15