Last active
October 17, 2015 15:50
-
-
Save JaneLSumner/001e67dc728e4a2e260d to your computer and use it in GitHub Desktop.
This code is intended to allow students to visualize the logic behind the mean value theorem. It was written for Political Science 508, a mandatory course for first-semester political science Ph.D. students at Emory University, by Jane Lawrence Sumner.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Understanding the Mean Value Theorem | |
#POLS 508, Fall 2015 | |
#TA: Jane Lawrence Sumner | |
# The Mean Value Theorem states that if the function is continuous on the closed | |
# interval and differentiable on the open interval, then there exists a point c | |
# at which the tangent to that point is parallel to the secant. | |
## In other words, there exists a point c such that f'(c)=(f(b)-f(a))/(b-a) | |
## What does that look like? | |
#### set your lower and upper bounds, function, and the derivative ##### | |
a <- -10 | |
b <- 10 | |
x <- seq(a,b,by=.0001) | |
fx <- x^3 | |
derivative <- 3*x^2 | |
#### plots the function #### | |
plot(x,fx,type="l") | |
fxb <- fx[which(x==b)] #function evaluated at b | |
fxa <- fx[which(x==a)] #function evaluated at a | |
slope.secant <- (fxb-fxa)/(b-a) #slope of the secant line | |
# this is what f'(c) must | |
#equal | |
intercept.secant <- fxb-slope.secant*b #y-intercept (for plotting) | |
abline(coef=c(intercept.secant,slope.secant)) #visualize the secent | |
#at what point(s) c is the derivative of the function equal to the slope of the secant | |
#line? (note: I call this cpt instead of c, since c is "concatenate" in R. It is | |
#possible to rename R functions as variables, but it's not a great idea.) | |
# (note 2: if you're getting errors, the odds are good it has to do with the | |
# rounding in the first line of cpt. As the secant slope becomes larger, the amount | |
# of rounding you should do decreases, so play around with this.) | |
cpt <- x[which(round(derivative,3) %in% round(slope.secant,3))] #first, locate them | |
cpt <- unique(round(cpt,2)) #because of how it matches | |
#to find x, there may be | |
#more than one that are | |
#approximately equal. | |
#this sorts that out. | |
cpt <- cpt[cpt %in% c(a,b)==0] #can't be a or b! | |
abline(v=cpt,lty=3) #plot that. | |
fxc <- fx[which(round(x,4) %in% round(cpt,4))] #function evaluated at c | |
intercept.tangent <- fxc-slope.secant*cpt #just rearrange, since we | |
#know the slope, x, and y | |
for(i in 1:length(intercept.tangent)){ | |
abline(coef=c(intercept.tangent[i],rep(slope.secant,length(intercept.tangent))[i])) | |
} | |
# the above line of code just plots a line for as many lines as there should be | |
# because we have only one secant slope, but two lines, I just repeat the value | |
# for as many lines as we have | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment