For this lab, instead of writing answers in a box, please include a transcript of your session with Sage

(or Maple, or…). Make sure to highlight, or draw attention to, the information asked for in boxes.

In this lab, we’ll continue to approximate functions. However, instead of using Taylor’s

method – which is great for approximating a function near a point – we’ll be using functions

sin(mx) and cos(nx) to approximate periodic functions.

Let p be a positive number. A function f (x) is p-periodic if

f (x + p) = f (x).

(If this is true, then it’s true that for every integer n, f (x + np) = f (x). See if you can figure out

why.)

The period of a function is the smallest positive p such that f (x + p) = f (x).

–3 π –5

2 π –2 π –3 2 π –π –1 2 π 1 2 π π 3 2 π 2 π 5 2 π 3 π

–1. 0

–0. 5

0. 5

1. 0

4 p

sin(x) + 4

3p sin(3x) + 54p sin(5x) + 74p sin(7x).

For example, sin(x) and cos(x) are periodic, with period 2p, while tan(x) is periodic with

period p.

–

52

π –2 π –3

2 π –π –1 2 π 1 2 π π 3 2 π 2 π 5 2 π

–2. 0

–1. 0

1. 0

2. 0

tan(p/4) = tan(p/4 + p)

Here is another example. The floor of a number, bxc, is the largest integer which less than or

equal to x:

Colorado State University 1 Math 161: Calculus II

Fall 2017

Name/Section Lab #3 Due: 29 November 2017

-4 -2 2 4

-4

-2

4 2

It is not periodic, but we can use it to define a periodic function:

f (x) = (1–1 b bx xc c is even is odd

-4 -2 2 4

-1

-0.5

0.5

1

Colorado State University 2 Math 161: Calculus II

Fall 2017

Question 1

Consider the periodic function

f (x) = x

p

– 2k if (2k – 1)p ≤ x < (2k + 1)p for some integer k.

Enter this function into the computer algebra system of your choice. (Look at the appendix

below for the piecewise definition of a function in Sage.)

Graph this function on –10 ≤ x ≤ 10.

What is the period of f (x)?

Colorado State University 3 Math 161: Calculus II

Fall 2017

Question 2

In a Taylor expansion, we try to approximate a function with a polynomial. In a Fourier

expansion, we try to approximate a (periodic) function with a sum of trigonometric functions.

Specifically, we’ll try to make an approximation

f (x) ≈ b0

2 + a1 sin(x) + b1 cos(x) + a2 sin(2x) + b2 cos(2x) + · · ·

=

12

b0 +

¥∑

m=1

am sin(mx) +

¥∑

n=1

bn cos(nx)

with a good choice of coefficients am and bn.

If f and g are two different functions, define:

h f (x), g(x)i = 1

p Z–pp f (x)g(x) dx.

You can implement this in Sage with:

def ip(f,g):

return(integral(f*g,(x,-pi,pi))/pi)

If you find that this takes too long to run, you can insist on numeric integration, as in

def ipfast(f,g):

return(numerical_integral(f*g/pi,-pi,pi)[0])

For each m 2 f1, 2, 3g, and each n 2 f1, 2, 3g, show that

hsin(mx), sin(nx)i = hcos(mx), cos(nx)i = (1 if 0 if m m = 6= nn

hcos(mx), sin(nx)i = 0

In fact, this pattern always holds (except if m = n = 0) – you can prove it, if you like, using

the work on trigonometric integrals we did earlier in the semester.

Pick some nonzero numbers b0, a1, a2, b1, and b2, and let

g(x) = b0

2 + a1 sin(x) + b1 cos(x) + a2 sin(2x) + b2 cos(2x).

Compute the numbers

hg(x), 1i, hg(x), sin(x)i, hg(x), cos(x)i, hg(x), sin(2x)i, hg(x), cos(2x)i.

What is the relation between these numbers and the numbers you chose above?

Colorado State University 4 Math 161: Calculus II

Fall 2017

Question 3

In general, given a function f, its dth Fourier approximation is

fd = 1

2h f(x), 1i · 1 +

d∑

m=1

h f(x), sin(mx)i sin(mx) +

d∑

n=1

h f(x), cos(nx)i · cos(nx).

Let f(x) be the function from Problem 1. For d = 1, 2, 3, 4, 5, 6, compute fd(x), and simultaneously plot f(x) and fd(x). (Thus, you should have six different graphs here.)

Question 4

As d increases, what do you notice about fd(x), compared to f(x)?

If instead, we tried different Maclaurin polynomials Pd(x), what would happen?

You might want to try this graphically with the different Taylor polynomials before you write

your answer. (The Taylor series of f(x) centered at a = 0 is the same as the Taylor series of

g(x) = px , since these two functions are the same in a small neighborhood of 0.)

Colorado State University 5 Math 161: Calculus II

Fall 2017

Appendix: Defining functions in Sage

Here are two different ways of defining the squaring function in Sage:

sage : s(x) = x^2 1

sage : def t(x): 2

….: return (x ^2) 3

sage : s (5) 4

25 5

sage : t (6) 6

36 7

We can define piecewise functions in Sage, too. The idea is to define the function on different,

disjoint intervals. The only problem is that, for example, [–5, 0] and [0, 5] intersect at 0, and so

the program gets unhappy.

The solution to this is to either alternate between closed intervals and open intervals:

sage : f = piecewise ([ 8

….: ([0 ,1] ,x ^3) , 9

….: ((1 ,2) ,1-x ^2) , 10

….: ([2 ,3] ,x -2) 11

….: ] ) 12

sage : plot (f ,0 ,3) 13

Graphics object consisting of 1 graphics primitive 14

0.5 1 1.5 2 2.5 3

-3

-2

-1

1

or to systematically use half-open intervals:

sage : g = piecewise ([ 15

….: ( RealSet . closed_open ( -3 , -1) ,sin (x)), 16

….: ( RealSet . closed_open ( -1 ,1) ,cos (x)), 17

….: ( RealSet . closed (1 ,3) ,x ^2) 18

….: ]) 19

sage : plot (g , -3 ,3) 20

Colorado State University 6 Math 161: Calculus II

Fall 2017

Graphics object consisting of 1 graphics primitive 21

-3 -2 -1 1 2 3

8 6 4 2

(The vertical lines are an artifact of the graphing.)

Colorado State University 7 Math 161: Calculus II

Fall 2017