Project MOSAIC M-CASTS Whats an M-CAST A regularly scheduled but - - PowerPoint PPT Presentation

project mosaic m casts
SMART_READER_LITE
LIVE PREVIEW

Project MOSAIC M-CASTS Whats an M-CAST A regularly scheduled but - - PowerPoint PPT Presentation

Project MOSAIC M-CASTS Whats an M-CAST A regularly scheduled but informal way to share ideas about teaching and develop collaborations for further development of those ideas. Broadcast via Internet. Most are presentations, but some


slide-1
SLIDE 1

Project MOSAIC M-CASTS

What’s an M-CAST

  • A regularly scheduled but informal way to share ideas about teaching

and develop collaborations for further development of those ideas.

  • Broadcast via Internet.
  • Most are presentations, but some will be conversations and

discussions.

  • Scheduled for the 2nd and 4th Friday of each month. Special event

when there is a 5th Friday.

  • Video and audio is recorded for posting on the Internet. Experience is

that many more people see the recording than participate in the live event. We encourage you to lead your own M-CAST or propose a topic that you would like to hear more about. See the sign-up sheet at the MOSAIC Wiki: www.mosaic-web.org.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 1 / 30

slide-2
SLIDE 2

Upcoming M-CASTS

The 2nd and 4th Friday of each month. Time of day varies slightly: see www.mosaic-web.org. Aug 13, 2010 (today!) Danny Kaplan, Finite differences as derivatives Aug 27, 2010 Eric Marland, Animating Bifurcations, Sept 10, 2010 Vittorio Addona, Helping Students Understand Regression Coefficients: An Example of Modeling Body Fat Percentage

  • Sept. 24, 2010 Nicholas Horton, Being Warren Buffet
  • Oct. 8, 2010 Randall Pruim, Golfballs in the Yard
  • Oct. 22, 2010 Eric Cytrynbaum, Science One at UBC

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 2 / 30

slide-3
SLIDE 3

Finite Differences as Derivatives

Daniel Kaplan Macalester College August 13, 2010

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 3 / 30

slide-4
SLIDE 4

Background

  • For students, differentiation in an traditional calculus course appears

as a series of rules for symbolic manipulation: for instance x2 → 2x, sin x → cos x, the product rule, the quotient rule, the chain rule, etc.

  • Students work out derivatives in terms of limits, e.g.

d dx x2 = lim

h→0

(x + h)2 − x2 h = lim

h→0

x2 + 2xh + h2 − x2 h = lim

h→0 2x + h = 2x

  • In reform calculus courses, derivatives are also presented using graphs

and tables, e.g. a student may be asked to sketch the derivative of a function given only in graphical form.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 4 / 30

slide-5
SLIDE 5

Motivation

  • Connect the derivative more formally to the idea of a function.
  • Generalize the derivative beyond the particular symbolic rules learned

and encourage greater numerical computing fluency.

  • Emphasize that the inputs to functions can be different kinds of

things than the outputs.

  • Streamline calculation of the derivative to make 2nd and high-order

derivatives more accessible.

  • Permit derivatives to be computed on new types of functions, e.g.,

splines.

  • Allow exploration of limits in a non-algebraic way.
  • Support the idea of comparing two functions: how similar are they?

But mainly ... To let students work with and explore derivatives without having to master algebraic techniques.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 5 / 30

slide-6
SLIDE 6

Outline of this M-CAST

1 Computational background for instructors and for students 2 The finite-difference operator 3 Applications with the finite-difference operator:

  • Graphing derivatives.
  • The exponential function is its own derivative.
  • What’s the derivative of sin(x)?

4 Extensions after the M-CAST: How far wrong is the finite-difference

derivative, one-sided versus centered derivatives, numerics and very small h,“what kind of thing is this?”

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 6 / 30

slide-7
SLIDE 7

Computational Background: Instructor

The examples here are done in R.

A very similar syntax would apply in packages such as Mathematica, Maple, Matlab, or SAGE. Basic capabilities needed in the computation package:

1 Define a function from the command line. 2 Return a function from a function. 3 Graphing a function over an interval.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 7 / 30

slide-8
SLIDE 8

Defining a Function on the Command Line

R

> f = function(x) { x^2 + 3 }

Matlab

f = @(x) x^2 + 3 This function handle syntax is unknown to most Matlab users. Much superior to the older“in-line”functions based on character strings.

Mathematica

f[x_] := x^2 + 3 Each of these languages has its own syntax for identifying the arguments to the function and provides for the implicit return of values.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 8 / 30

slide-9
SLIDE 9

A Little More about Functions in R

Optional arguments and default values

Functions can take optional arguments with default values, e.g. > g = function(x, a=3) { a*x } > g(2) [1] 6 > g(2,a=10) [1] 20 We will use this syntax in defining the finite-difference derivative operator, but not in defining mathematical objects.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 9 / 30

slide-10
SLIDE 10

Still More about Functions in R

Unassigned parameters are captured from outside

You can use parameters that are not arguments and not defined inside the

  • function. The values are captured from outside. Lexical Scoping

> h = function(x) {b + x} > b = 10 > h(3) [1] 13 > b=1000 > h(3) [1] 1003

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 10 / 30

slide-11
SLIDE 11

Computational Background: Student Skills

  • Define a function, e.g.

> f = function(x) { x^2 - 3 }

  • Apply a function to an input, e.g.

> f(3) [1] 6

  • Plot a function over an interval:

> x = seq(0,2,length=100) > plot( x, f(x) )

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 11 / 30

slide-12
SLIDE 12

Streamlining Plotting

As a convenience it’s nice to let the computer handle the generation of the x points.

The curve operator in R:

> curve(f, from=0, to=3)

0.0 1.0 2.0 3.0 −2 2 4 6 x f (x)

Note that the first argument to curve is the name of the function to be

  • plotted. You can also write curve( f(x), from=0, to=3 )

You could have your students write mycurve so that they see that curve doesn’t do anything magical. (See Extensions at end.)

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 12 / 30

slide-13
SLIDE 13
  • By default, curve creates a new set of axes. You can set the vertical

scale with ylim and insert a line to mark the x axis: > curve(f, from=0, to=3, ylim=c(-10,10)) > abline(0,0)

  • For comparing functions, you can add new curves to an existing set of

axes and choose a color: > g = function(x) {4*x - 5} > curve( g, add=TRUE, col="blue")

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 13 / 30

slide-14
SLIDE 14

Computational Background: Student

Critical Computational Concepts

  • An operator an object that can be applied to an input to return an
  • utput.
  • Applying an operator to an input gives the output. It’s important to

distinguish between the operator and the output generated by the

  • perator when applied to an input.
  • A function can itself be an input to an operator.
  • Graphs are generated by evaluating a function f at a set of points x

and plotting out the pairs f(x) versus x.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 14 / 30

slide-15
SLIDE 15

Constructing New Functions from Old

When you want to construct a new function, you need to use the function operator: > curve( f, from=0, to=3 ) > g = function(x){ f(x) / 2 } > curve( g, add=TRUE, col="blue")

0.0 1.0 2.0 3.0 −2 2 4 6 x f (x)

For Discussion: The“friendlier”syntax

curve also lets you write > curve( f(x)/2, from=0, to=3 ) The argument must be x. Topic for discussion: Is this compact notation a good thing for beginners?

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 15 / 30

slide-16
SLIDE 16

The Finite-Difference Derivative Operator

> D = function(f, h=0.0001) { function(x) { (f(x+h) - f(x))/h } }

  • D takes a function as an input and returns a new function (of x) as an
  • utput.
  • h has been set to a“small”value. Working out what“small”should be

is an issue for student investigation.

  • I give this operator to my students, rather than having them try to

develop it on their own.

The built-in D operator

R already has a D operator that has some symbolic properties. But it’s very hard to use. I replace it with this one by giving my students an R workspace with D redefined. Better to use a package? The MOSAIC R package.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 16 / 30

slide-17
SLIDE 17

Using the Operator

  • Define a function to be differentiated:

> g = function(x) { exp(-.2*x)*sin(2*pi*x/10) } > g(0) [1] 0 > g(2) [1] 0.6375122 Note the choice of name for the new function, to remind us whence it was derived.

  • Take the derivative by applying D to the function. Give the result a

name. > gprime = D(g) > gprime(0) [1] 0.628306 > gprime(2) [1] 0.002633719

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 17 / 30

slide-18
SLIDE 18

Plot a function:

> curve(g,from=0,to=20)

5 10 15 20 −0.2 0.0 0.2 0.4 0.6 x g (x)

Plot the derivative

> curve(gprime,from=0,to=20)

5 10 15 20 −0.2 0.0 0.2 0.4 0.6 x gprime (x)

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 18 / 30

slide-19
SLIDE 19

The 2nd Derivative

Plot a function:

> gpp = D( D( g ) ) > curve(gpp,from=0,to=20)

5 10 15 20 −0.3 −0.2 −0.1 0.0 0.1 x gpp (x)

Some Questions for Students

  • At the x values where gprime is zero, what’s happening in g?
  • At the x values where gpp is zero, what’s happening in g?

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 19 / 30

slide-20
SLIDE 20

Exponential Functions

An Exercise

Consider the function f (x) = 10x. Show that the derivative f ′ = d

dx f is

similar to f , that is f ′(x) = constf (x). Do this by finding the const. > f = function(x){ 10^x } > fp = D( f ) A strategy is to evaluate both f and fp at some value of x and take the ratio: > fp(3) / f(3) [1] 2.30285 > fp(5) / f(5) [1] 2.30285

Question

Where does 2.30285 come from?

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 20 / 30

slide-21
SLIDE 21

Pay attention to the scales!

Graph reading is important in science. A basic skill is to interpret the scales correctly, e.g., is it big or small?

Graph fp(x)/f(x) to show it’s constant

> the.ratio = function(x){ fp(x)/f(x) } > curve( the.ratio, from=-3, to=3)

−3 −2 −1 1 2 3 2.30285 2.30285 2.30285 2.30285 x the.ratio (x)

  • The function isn’t constant, as it was

claimed to be.

  • Look at the vertical scale.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 21 / 30

slide-22
SLIDE 22

Exploring h

In the previous example, the natural logarithm emerged from comparing the function f (x) = 10x to its derivative. But the value wasn’t exactly right! Examine how the value converges as h becomes smaller: > log(10) [1] 2.302585 > fp = D(f, h=0.1) > fp(5)/f(5) [1] 2.589254 > fp = D(f, h=0.001) > fp(5)/f(5) [1] 2.305238 > fp = D(f, h=0.00001) > fp(5)/f(5) [1] 2.302612 > fp = D(f, h=0.000000001) > fp(5)/f(5) [1] 2.302585

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 22 / 30

slide-23
SLIDE 23

Exploring h

A Claim

The derivative of sin(x) is cos(x). Confirm this with the D operator, showing how the finite difference approaches cos(x) as h becomes small. > curve( cos, from=-5, to=5, col="gray", lwd=6 ) > f1 = D( sin, h=1.0 ) > curve( f1, add=TRUE, col='red') > f2 = D( sin, h=0.5 ) > curve( f2, add=TRUE, col='green') > f3 = D( sin, h=0.1 ) > curve( f3, add=TRUE, col='blue')

−4 −2 2 4 −1.0 −0.5 0.0 0.5 1.0 x cos (x)

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 23 / 30

slide-24
SLIDE 24

Extension: The Chain Rule

What’s the derivative of f (g(x))? > f = sin; > g=function(x){x^2} > h=function(x){f(g(x))} > hp = D(h); > fp = D(f); > gp = D(g);

Candidates:

> cand1=function(x){ fp(g(x)) } > cand2=function(x){ f( gp(x) ) } > cand3=function(x){ fp(g(x))*gp(x)} Test them: > hp(3) [1] -5.467614 > cand1(3) [1] -0.9111509 > cand2(3) [1] -0.2793195 > cand3(3) [1] -5.466996

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 24 / 30

slide-25
SLIDE 25

Extension: More about plotting

  • plot makes a new set of axes. Use lines and points to add to an

existing set of axes.

  • Useful arguments to plot:
  • Set y and x limits: e.g. ylim=c(0,10), xlim=c(-5,5).
  • Set the color: col="red".
  • To see the named colors, use

> colors()

  • Set the line width: lwd=3.

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 25 / 30

slide-26
SLIDE 26

Extension: Have students write their own curve

  • Make a plot in two steps:

> x = seq(-5,5,length=50) > plot( x, sin(x) ) Deficiencies:

  • Not enough points. Change length.
  • Want a line plot. Add argument type=’l’ to get a line plot.
  • How to overlay? Change color?
  • Write your own, one-step function

> mycurve = function(f, from=0, to=1, ...) { x = seq(from, to, length=1000); plot( x, f(x), type="l") } > mycurve(sin, from=-5, to=5)

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 26 / 30

slide-27
SLIDE 27

Extension: What happens when the deriv. is zero?

1 Define the function and its derivative

> f = function(x){exp(-.2*x)*sin(2*pi*x/3)} > fp = D(f)

2 Plot out the derivative and make some estimates of where it crosses

  • zero. (Not shown.)

3 Refine these guesses

> require(rootSolve) > zeros = multiroot(fp, start=c(1,2,4,7,8) ) > zeros$root [1] 0.7044933 2.2044933 3.7044933 6.7044933 8.2044933

4 Plot out the value of the function at those roots:

> curve( f, from=0, to=10) > points( zeros$root, f(zeros$root), col='red',pch=20)

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 27 / 30

slide-28
SLIDE 28

2 4 6 8 10 −0.5 0.0 0.5 x f (x)

  • What happened to the minimum near 5?

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 28 / 30

slide-29
SLIDE 29

Extension: How far off is the approximation?

> fdiff = function(h) { function(x){abs(cos(x) - D(sin,h=h)(x))} } > optimize(fdiff(.1), lower=0, upper=10,maximum=TRUE) $maximum [1] 1.537463 $objective [1] 0.04998611 > optimize(fdiff(.001), lower=0, upper=10,maximum=TRUE) $maximum [1] 1.570461 $objective [1] 5e-04

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 29 / 30

slide-30
SLIDE 30

Distributing the Software

To use these examples, all you need is to define the D operator: D = function(f, h=0.0001) { function(x) { (f(x+h) - f(x))/h } }

  • I distribute this, as well other R software and datasets, to students by

giving them an R workspace, math135.Rdata, that they use to start up R.

  • There are advantages to distributing this as an R package. Should we

make a MOSAIC package in R?

  • Other helpful packages: solveRoot and deSolve

Daniel Kaplan () Finite-Difference Derivatives August 13, 2010 30 / 30