WORKING WITH FUNCTIONS IN MATHEMATICA With applications to Fourier Series Overview In this classnote we will learn how to use write and manipulate functions in Mathematica. The first step in this process is to firm up our understanding of the different types of equal signs used in Mathematica. In this discussion, I will introduce some other new Mathematica features as well as use some previously learned functions. At the end of this note, I will show how to use Mathematica to write functions and short programs to help us with Fourier series. Three Types of Equal Signs In Mathematica, there are three different types of equal signs commonly used. In standard mathematics, we use "=" for a variety of purposes, such as: • assigning a value to a variable, e.g., x=3 • defining a function whose value depends on a variable, e.g., f(x) = x2 • solving an equation, e.g.: ex - 3 x cos x = 2 x + 1 Mathematica uses a different set of symbols for each logical expression. Let's consider each in turn. ü Immediate Assignment We have already seen that to assign a value to a variable, we use the well - known equal sign, in other words, the expression : x=3 Assigns the value of 3 to the variable x. Unless and until we redefine the value of x (or clear the value of x), x will retain the value of 3. So, if below we set x=3 and evaluate several expressions: x=3 3 x^3 − x^2 + x 21 2 mfunctions.nb Integrate@y, 8y, 0, x<D 9 2 You can readily see that x has retained the value of 3. We can clear the value of x : Clear@xD x x And when we ask for the value of x, no number is returned since the Clear[x] statement erased any prior definition for x. ü Defining Functions and Delayed Assignment If we want to define a function of the variable x, we have to use the proper syntax to instruct Mathematica that x is a variable whose value will change. If we want to write f(x)=x, we input: f@x_D := x There are two important points here. First, the underscore following the x tells Mathematica that we intend x to be the variable of the function. Second, we cannot simply use "=" since we are not assigning x to any one value. We use the combination (without a space) of a colon and equal sign. This type of equality delays assignment of any specific value to x until a later set of calculations. Let' s investigate the properties of this type of equal sign. Clear@fD f@x_D := x Plot@f@xD, 8x, −2, 2<D 2 1 -2 -1 1 2 -1 -2 And we plot the function f (x) = x from - 2 to 2. Let' s see what another plot looks like : mfunctions.nb Plot@f@xD ^ 2, 8x, −2, 2<D 4 3 2 1 -2 -1 1 2 As expected, we obtain the parabola f HxL = x2 . We can use f(x) in calculations: f@3D 3 f@5D ^ 2 25 Exp@f@xDD ê. x → 4 4 We can define functions of more than one variable, such as the distance traveled by a projectile. Recalling your first semester physics, you know that in the absence of air friction, a projectile launched on a level plane with initial speed v0 and launch angle q has components of motion given as: x HtL = v0 cos HθL t; y HtL = v0 sin HθL t − 1 gt2 2 We can write these as functions : Clear@x, y, t, v0, g, θD g = 9.8; v0 = 20; θ = π ê 4; x@t_D := v0 Cos@θD t; g y@t_D := v0 Sin@θD t − t2 ; 2 The lines of code above write out the positions of the projectile in x and y as a function of t, and also define the acceleration of gravity, the launch speed, and the launch angle (in radians). We can see how long the particle is in the air by solving y (t) = 0 : Solve@y@tD 0, tD 88t → 0.<, 8t → 2.88615<< The statement above reminds us how to use the Solve function to solve the equation y(t)=0 for the variable t. This statement also introduces us to the third type of equal sign, the double equal (made by typing the equal sign twice with no intervening space). We use the double equal sign to indicate that the left and right hand sides of an equation are equal; it is the sign you use to solve equations, or to show equality between variables. The Solve statement in line [226] means to solve y (t) = 0, and to solve the equation in terms of t. The set of answers we receive, t = 0 and t = 2.89 s tell us when the projectile has no elevation. The first 3 4 mfunctions.nb answer corresponds to the time of launch; the second to the time of impact. We can plot y (t) as a function of time and see if the computed graph agrees with our result above : Plot@y@tD, 8t, 0, 2.88615<D 10 8 6 4 2 0.5 1.0 1.5 2.0 2.5 Remember that the graph above is not a trajectory (i.e., y (t) vs. x (t)), but a graph of the height as a function of time. Quick Mathematica note : one way to plot y (t) vs. x (t) is to make use of the ParametricPlot function : ParametricPlot@8x@tD, y@tD<, 8t, 0., 2.88615<D 10 8 6 4 2 10 20 30 40 And we calculate the expected parabolic trajectory for this projectile. What do we expect the range of the projectile to be? We can calculate the value of x (t) at the time of impact : x@tD ê. t → 2.88615 40.8163 or just try : x@2.88615D 40.8163 Or make use of the range equation : R= v0 2 sin2q v0 ^ 2 Sin@2 θD ê g 40.8163 And we see that we produce the same result from a variety of methods g mfunctions.nb 5 The double equal sign We have already seen the final type of equal sign in action; when we want to solve an equation or set two sides of an equation equal to each other. Solving most any polynomial equation is pretty straightforward : Solve@90 x ^ 4 − 207 x ^ 3 + 53 x ^ 2 + 94 x − 40 ::x → − 2 3 >, :x → 1 2 >, :x → 4 5 >, :x → 5 3 0, xD >> The output returns a set of four solutions to the quartic equation. We also saw the use of the double equal sign when we wanted to declare the equality of two different integers in a Fourier integral. Here is how we can set m = n œ Integers in an integral : Integrate@Cos@m xD Cos@n xD, 8x, −π, π<, Assumptions → Element@8m, n<, IntegersD && m nD π Look what happens if we try the same integral but use the single equal sign as the final condition : Integrate@Cos@m xD Cos@n xD, 8x, −π, π<, Assumptions → Element@8m, n<, IntegersD && m = nD Set::write : Tag Rule in Assumptions Ø Hm » nL œ Integers && m is Protected. à −SinIntegral@Hm − nL πD + SinIntegral@Hm + nL πD We get an error message telling us that since we have already defined m as a variable, we cannot equate it to another variable. We can use Solve with systems of equations, remembering to use braces for sets of equations and sets of variables. If we want to solve the set of equations : x+y-z =3 2x-3y+2z = 5 3x-y+z= 2 We would write : Solve@8x + y − z ::x → 5 3, 2 x − 3 y + 2 z , y → −6, z → − 4 31 4 5, 3 x − y + z 2<, 8x, y, z<D >> We can verify that these solutions in fact satisfy this system of equations : x + y − z ê. 8x → 5 ê 4, y → −6, z → −31 ê 4< 3 2 x − 3 y + 2 z ê. 8x → 5 ê 4, y → −6, z → −31 ê 4< 5 Or we can try to verify all three equations at once : 8x + y − z, 2 x − 3 y + 2 z, 3 x − y + z< ê. 8x → 5 ê 4, y → −6, z → −31 ê 4< 83, 5, 2< 6 mfunctions.nb And the substitution routines return the expected values. Using Functions to help with Fourier Analysis You will quickly get the hang of computing Fourier series; you will become very familiar with the choreography of finding the coefficients a0 , an , and bn . Each of these involves integration, so by now you are probably expert at using Mathematica for integration. ü Our First Example Let' s see if we can write simple programs utilizing functions to help us determine Fourier series. Let' s start with our old friend : f HxL = x, -p § x < p (1) We know that we want to calculate coefficients, multiply them by the appropriate trig function, and then sum the terms. Let' s see if we can do all this by use of functions. First, we clear all of our potential variables : In[12]:= Clear@f, a0, an, bnD f@x_D := x a0 = H1 ê πL Integrate@f@xD, 8x, −π, π<, Assumptions → Element@n, IntegersDD; an = H1 ê πL Integrate@f@xD Cos@n xD, 8x, −π, π<, Assumptions → Element@n, IntegersDD; bn = H1 ê πL Integrate@f@xD Sin@n xD, 8x, −π, π<, Assumptions → Element@n, IntegersDD; Print@8a0, an, bn<D :0, 0, − 2 H−1Ln n > The program we have written thus far defines f (x) = x (since this is a delayed assignment we don' t need to put a semi - colon after the line, no output will be calculated to be displayed), and also defines coefficients in the traditional way. The final print statement outputs the calculated coefficients; we can see that our program has computed the correct coefficients. Suppose now we wish to print out the first several terms of the Fourier series, we could add the command : In[19]:= Print@a0 ê 2 + Sum@an Cos@n xD + bn Sin@n xD, 8n, 1, 10<DD 2 Sin@xD − Sin@2 xD + 2 Sin@3 xD − 3 1 3 Sin@6 xD + 2 7 Sin@7 xD − 1 Sin@4 xD + 2 1 Sin@8 xD + 4 2 Sin@5 xD − 5 2 9 Sin@9 xD − 1 Sin@10 xD 5 And we have the first ten terms of the Fourier series printed out for us. We can produce a plot of this series : mfunctions.nb In[38]:= 7 Clear@fourier, ulD ul = 100; fourier@n_D := a0 ê 2 + Sum@an Cos@n xD + bn Sin@n xD, 8n, 1, ul<D; Plot@fourier@nD, 8x, −π, π<, Epilog → Point@882, 2<, 8−1.3, −1.3<<DD 3 2 1 Out[41]= -3 -2 -1 1 2 3 -1 -2 -3 You can see that I created another function (be careful not to use upper case letters in defining functions; Mathematica always capitalizes its own functions, so you can be sure to avoid confusion by defining functions and variables with lower case letters) to help with this plot. I also use the device of allowing the upper limit (this is the meaning of the variable "ul") of the summation to vary so we can see how including more terms in the summation allows the series to converge to the function. Finally, I add the epilog statement to add the points (2,2) and (-1.3,-1.3) to the graph. We know these point lie on the line y=x, so their position on the graph verifies that we have computed the proper function. ü The Square Wave In class, we considered the Fourier series for the function : g HxL = 0, |p § x < 0 +1, 0§x<p (2) Unlike our example in equation (1), we need to statements to describe this function; one statement for x < 0 and one for x > 0. We have a couple of ways we can write such functions in Mathematica. The most obvious is to define to functions, and calculate each coefficient by integrating over two distinct regions. If we call one function g1 and the other g2, we have : In[93]:= Clear@g1, g2, a0, an, bnD g1@x_D := 0 g2@x_D := 1 and we slightly change our definitions of the coefficients to reflect this difference : In[96]:= In[97]:= In[98]:= a0 = H1 ê πL Integrate@g1@xD, 8x, −π, 0<, Assumptions → Element@n, IntegersDD + H1 ê πL Integrate@g2@xD, 8x, 0, π<, Assumptions → Element@n, IntegersDD; an = H1 ê πL Integrate@g1@xD Cos@n xD, 8x, −π, 0<, Assumptions → Element@n, IntegersDD + H1 ê πL Integrate@g2@xD Cos@n xD, 8x, 0, π<, Assumptions → Element@n, IntegersDD; bn = H1 ê πL Integrate@g1@xD Sin@n xD, 8x, −π, 0<, Assumptions → Element@n, IntegersDD + H1 ê πL Integrate@g2@xD Sin@n xD, 8x, 0, π<, Assumptions → Element@n, IntegersDD; Notice that each integral is being done twice; once from -p to 0 and the second from 0 to p. What are our coefficients?: 8 mfunctions.nb In[99]:= Print@8a0, an, bn<D :1, 0, − −1 + H−1Ln nπ > And print the first several terms of the series : In[144]:= 1 + Clear@fourier, ulD ul = 100; fourier@n_D := a0 ê 2 + Sum@an Cos@n xD + bn Sin@n xD, 8n, 1, ul<D Print@a0 ê 2 + Sum@an Cos@n xD + bn Sin@n xD, 8n, 1, 11<DD 2 Sin@xD 2 π + 2 Sin@3 xD 3π + 2 Sin@5 xD 5π + 2 Sin@7 xD 7π + 2 Sin@9 xD 9π + 2 Sin@11 xD 11 π And plot out the series using terms from n = 1 to n = 100 : In[148]:= Plot@fourier@nD, 8x, −π, π<, Epilog → Point@88−1.5, 0<, 81.5, 1<<DD 1.0 0.8 0.6 Out[148]= 0.4 0.2 -3 -2 -1 1 2 3 ü The Square Wave and the Which Command The "Which" command provides a logical expression that allows us to evaluate a function like the one given in equation (2) in only one statement. The "Which" command has the general form : Which@condition1, value1, condition2, value2 ...D The command will return the value that is true; let' s see how this works in practice. We would implement the Which command for the function in equation (2) as : In[77]:= Clear@h, a0, an, bnD h@x_D := Which@−π ≤ x < 0, 0, 0 ≤ x < π, 1D Here, we define a new function h (x) in terms of the Which command. The "conditions" in this case are the regimes of the function; one condition is that x is between -p and 0, the other condition is that 0<x<p. If the first condition is true, i.e., if -p<x<0, then the value of the function will be 0. If the second condition is true, then the function will have a value of 1. We have already cleared the relevant variables and defined functions. We now define and integrate coefficients: mfunctions.nb In[153]:= 9 a0 = H1 ê πL Integrate@h@xD, 8x, −π, π<, Assumptions → Element@n, IntegersDD; an = H1 ê πL Integrate@h@xD Cos@n xD, 8x, −π, π<, Assumptions → Element@n, IntegersDD; bn = H1 ê πL Integrate@h@xD Sin@n xD, 8x, −π, π<, Assumptions → Element@n, IntegersDD; Print@8a0, an, bn<D :1, 0, − −1 + H−1Ln nπ > Clear@fourier, ulD ul = 100; fourier@n_D := a0 ê 2 + Sum@an Cos@n xD + bn Sin@n xD, 8n, 1, ul<D Print@a0 ê 2 + Sum@an Cos@n xD + bn Sin@n xD, 8n, 1, 11<DD 1 + 2 Sin@xD 2 In[168]:= π + 2 Sin@3 xD 3π + 2 Sin@5 xD 5π + 2 Sin@7 xD 7π + 2 Sin@9 xD 9π + 2 Sin@11 xD 11 π Plot@fourier@nD, 8x, −π, π<, Epilog → 8Red, PointSize@LargeD, Point@88−1.5, 0<, 81.5, 1<<D<D 1.0 0.8 0.6 Out[168]= 0.4 0.2 -3 -2 -1 1 2 3 And this time I make the verifying points large and red to be obvious!! And we have shown that we can use the Which command to define a function and reproduce the same coefficients, series expansions and graphs. On homeworks, you may use Mathematica to compute integrals for you, but you must make use of that information to determine coefficients and then write the Fourier series. You may of course (and you are encouraged to) use Mathematica to verify your answers. However, you cannot submit as a complete homework solution a program which defines a function and then computes both the coefficients and series expansion. Mathematica can compute integrals as part of your homework, but cannot do more than that. And ... to reinforce my comment in class today, the ONLY acceptable output is Mathematica output. Remember, we are trying to learn a new language, so this is the program you must use if you wish to submit computer generated output.