Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This is a short post for my students in the CUNY MS Data Analytics program on sketching curves in R.
Graphing functions
Suppose we want to find the derivative of
f <- function(x) (-4*x^5 + 3*x^2 - 5) / x^2 xs <- -10:10 plot(xs, f(xs), type='l')
This gives us the graph below. You may notice that there is a big gap between -1 and 1. Why is this? The short answer is that 0 is undefined for
Let’s try again with a spacing of 0.1. What’s the best way to do this? If we want to use the syntactic sugar, then we need to scale the interval ourselves. For our example the scaling is easy. For the more general case, what is the best way to model the scaling? Getting back to the original discussion, here are two equivalent alternatives.
xs <- (-100:100) / 10 xs <- seq(-10, 10, by=0.1)
Note that in the first form the parentheses are mandatory due to the operator precedence rules of R.
Working with this interval, we get a more precise representation of the function. However, I still have this uneasy feeling that I don’t really know what this function looks like near 0. Let’s “zoom” into 0 by increasing the resolution by another order of magnitude. At a spacing of 0.01, this function looks very different from what we started with.
Exercises
- Write a function that takes an integer sequence and scales it to a given precision. For example, given the sequence -5:5, write a function s such that s(-5:5, 0.1) returns the sequence c(-5.0, -4.9, -4.8, …, 4.9, 5.0). Do not use the seq function in your answer.
- Reproduce the graph of f within the domain [-4, 4] and precision 0.2 using the function above to generate the x values.
Composing the derivative
What do these graphs tell us about the derivative? It appears mostly well-behaved except when
Returning to the original motivation for this discussion, the question is whether these curves can shed any light on the behavior of the derivative for this function. Now that we’ve deconstructed
g <- function(x) -4*x^5 + 3*x^2 - 5 h <- function(x) x^-2 xs <- seq(-2, 2, by=0.02) plot(xs, g(xs), type='l') lines(xs, h(xs), col='blue')
As expected,
What else does this graph tell us? It is useful to remember that the original function
Let’s write functions to represent the first derivative and overlay them as dotted lines onto the graph.1
g1 <- function(x) -20*x^4 + 6*x h1 <- function(x) -2 * x^-3 lines(xs, g1(xs), lty=3) lines(xs, h1(xs), col='blue', lty=3)
Now things are getting interesting. It takes a bit more effort to picture what the derivative of f looks like given these four curves. From a graphical perspective the product rule tells us to sum the product of the dotted black line and the solid blue line with the product of the dotted blue line and the solid black line.
To make it easier, here are the two products (g’h in orange and h’g in brown) along with the sum, which of course is f’ (in black).
Decomposing a function into smaller functions can be a useful exercise when looking to assess the relative impact of the constituent functions. Working from the opposite direction, it can also help in function approximation. Usually it is easier to build up a complex function from smaller functions rather than starting with a complicated function. I will explore this idea in a future post.
Exercises
- Reproduce the last graph
The derivative and constants
Here is another example of using graphs to help illuminate the behavior of functions. Let’s look at why a constant has a derivative of 0. Consider the function
Graphically, it is easy to see that the derivative of
While vertical shifts have no effect on the derivative, horizontal shifts do. Why is this? Simply put, it is because a horizontal shift modifies each
Notes
[1] See this handy reference for plot styles
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.