Lab 9: Approximating Functions

MAT 229, Spring 2025

How do we use series to approximate functions?

Our first goal in this lab is to use an infinite series of polynomials to approximate a rational function, motivated by geometric series. We’ll see that this works okay for some values of the domain, but fails miserably for others. 

Then we’ll go on to see how we might approximate the sine function everywhere with a simple polynomial. We’ll be able to do a convincing job with just a handful of terms. 

Our key commands this week include 
	Sum
	Plot
	Table
	TableForm
	If -- If/Then/Else structure in Mathematica
	D -- derivatives
	/. -- to replace a variable with a particular value

Part I: Generalizing the Geometric Series

Geometric series have the form (Underoverscript[Σ, (k=0), ∞])a (r^k). A geometric series converges if and only if |r|<1. If it does converge it converges to (a/(1-r)).
Thus,  (Underoverscript[Σ, (k=0), ∞])a (r^k)= (a/(1-r)), and, in particular, (Underoverscript[Σ, (k=0), ∞]) (r^k)= (1/(1-r)). 

If you don’t mind, I’d like to replace the variable “r” with the variable “x”: (Underoverscript[Σ, (k=0), ∞]) (x^k)= (1/(1-x)).

Say what? (Is that legitimate?)

	1. What is the domain of the rational function  gs(x)=(1/(1-x))?
	2. For what values of x do you expect this trick to work, based on your understanding of the geometric series? 
	3. So it may be that this strategy has limited utility.... Let’s see!

Let’s plot that function  gs(x)=(1/(1-x)) against a partial sum, (Subscript[S, n])(x), on the interval [-0.8,0.8]:

	1. with n=5
	2. with n=10
	
How well does the partial sum do? Where is it failing?

gs[x_]:=1/(1-x)
partialgs[x_,n_]:=Sum[x^k,{k,0,n}]
Plot[{gs[x],partialgs[x,20]},{x,-.8,.8}]

Try plotting gs(x) on the interval [-2,0.8], with n=10. 
	
How well does the partial sum do? Where is it failing? Where is it doing well?

gs[x_]:=1/(1-x)
nSum=10
partialgs[x_,n_]:=Sum[x^k,{k,0,n}]
Plot[{gs[x],partialgs[x,nSum]},{x,-2,.8}]
gs[-.9]
partialgs[-.9,nSum]

Define totalSum(x)=(Underoverscript[Σ, (k=0), ∞]) (x^k), and 
	1. plot it and gs[x] on [-0.8,0.8]: how well does it do?
	2. Then do the same on  [-2,0.8]: how well does it do, and what happens? 
	
Mathematica tells us explicitly what’s wrong!

totalSum[x_]:=Sum[x^k,{k,0,Infinity}]
Plot[totalSum[x],{x,-2,.8}]

Part II: Faking Sine: Sins of Calculus

We use symmetries, periodicities, etc. to construct sin, an approximation to Sine, on the smallest interval necessary. Then we fake it with a polynomial!

First let’s just make sure that we’ve got the logic structure of Sine down correctly. Here are the facts that allow us to compute any value of sine, given sine on a small interval -- the interval [0,π/2]:

	1. Sine is an odd function, so sin(-x)=-sin(x). This implies that we need sine only the positive reals, and we can deduce it on the negatives using this symmetry:
	
	2. Sine is periodic with period 2π : therefore, if x is greater than 2π , we can subtract multiples of 2π  from x until the value falls into the interval [0,2π ):
	
	3. Sine is odd about π on the interval  [0,2π ]. This means that we can take a value of x on the interval [π,2π], and evaluate it as -sin(2π-x). Note that in this case 2π-x will be on the interval [0,π]. 
	4. Finally, Sine is even about π/2 on the interval  [0,π]: hence we can compute a value of x on the interval [π/2,π], and evaluate it as sin(π-x). Note that in this case π-x will be on the interval [0,π/2]:

So let’s define sin(x), using these symmetries and periodicities, but the true Sine function on the smallest interval possible. We’ll use Mathematica’s If command, which is of the form If[condition, then, else]

sin[x_]:=
If[x<0,-sin[-x], 
If[x>2π,sin[x-Floor[x/(2π)]*2π],
If[x>π,-sin[2π-x],
If[x>Pi/2,sin[π-x],
Sin[x]
]
]
]
]

Now plot both Sin and sin on the interval [-π,3π], to make sure that our method is working. Plotting the difference between the two functions is also generally a good idea, to see where we’re fitting well, and where there might be issues.

Plot[{Sin[x],sin[x]},{x,-Pi,3Pi}]
Plot[Sin[x]-sin[x],{x,-Pi,3Pi}]

What we’re going to do now is define something called a “Taylor Polynomial”, which will match the sine function exactly at one point: the point x=0. So the following polynomial will match Sine’s value, and all its derivatives at 0 up to the 11th derivative!

For now, I’ll just give it to you. Down the road you’ll be able to figure them out for yourself:

t11[x_]:=x-((x^3)/6)+((x^5)/120)-((x^7)/5040)+((x^9)/362880)-((x^11)/39916800)

Differentiate t11 eleven times, and compare it to the third derivative of sine at 0, just to check that it’s working. 

We can do this in a Table (and don’t forget to use TableForm, to make things look nicer). For that you’ll want to use the “D” command, and we’ll also need to evaluate those derivatives at a point (x=0), for which we use the  “/.” command.

Clear[x]
TableForm[Table[{D[Sin[x],{x,n}],D[t11[x],{x,n}]},{n,0,11}]/. {x->0}]

Real sins:

Now let’s define sinner, incorporating this fake, plot it, and plot its error -- how bad is it?

sinner[x_]:=
If[x<0,-sinner[-x], 
If[x>2Pi,sinner[x-Floor[x/(2Pi)]*2Pi],
If[x>Pi,-sinner[2Pi-x],
If[x>Pi/2,sinner[Pi-x],
t11[x]
]
]
]
]

Plot both Sin and sin on the interval [-π,3π], to make sure that our method is working. Plotting the difference between the two functions is also generally a good idea, to see where we’re fitting well

Plot[{Sin[x],sinner[x]},{x,-Pi,3Pi}]
Plot[Sin[x]-sinner[x],{x,-Pi,3Pi},PlotRange->All]

It it were in your calculator, would you be able to spot the fake? 

Hint: it’s in your calculator!:)

Real cosinner:

Now that we’ve faked sine, it’s easy enough to fake a few other functions. 

Define cosinner in terms of sinner, and tansinner in terms of sinner and cosinner. Plot them on [-Pi,3Pi] to verify that you’re correct.

cosinner[x_]:=sinner[x+Pi/2]
Plot[{Cos[x],cosinner[x]},{x,-Pi,3Pi}]
tansinner[x_]:=sinner[x]/cosinner[x]
Plot[{Tan[x],tansinner[x]},{x,-Pi/2,Pi/2}]