Skip to main content
Top

2023 | Book

Applied Calculus with R

insite
SEARCH

About this book

This textbook integrates scientific programming with the use of R and uses it both as a tool for applied problems and to aid in learning calculus ideas. Adding R, which is free and used widely outside academia, introduces students to programming and expands the types of problems students can engage. There are no expectations that a student has any coding experience to use this text.
While this is an applied calculus text including real world data sets, a student that decides to go on in mathematics should develop sufficient algebraic skills so that they can be successful in a more traditional second semester calculus course. Hopefully, the applications provide some motivation to learn techniques and theory and to take additional math courses. The book contains chapters in the appendix for algebra review as algebra skills can always be improved. Exercise sets and projects are included throughout with numerous exercises based on graphs.

Table of Contents

Frontmatter
Chapter 1. A Brief Introduction to R

Before we begin learning R we should address two questions. What is R? From the R Project: “R is a language and environments for statistical computing and graphics,” and “One of R’s strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed.” [37] In short R is a programming language that is particularly useful for computations and graphics.

Thomas J. Pfaff
Chapter 2. Describing a Graph

We take a moment here to informally provide common terminology used to describe a graph. This will allow us to learn some of language of a graph now. Eventually we will use calculus to identify parts of the graph with these characteristics. Figure 2.1 has these definitions which are informally given in M-Box 2.1. A graph is increasing when it is going up or rising. A graph is decreasing when it is going down or falling. A graph is concave up when it is curved upward. A graph is concave down when it is curved downward. An inflection point is where the concavity changes. A local max (or maximum) is a local high point of the graph. Graphs can have more than one local max. A local min (or minimum) is a local low point of the graph. Graphs can have more than one local min. A global max (or maximum) is the absolute highest point on the graph on a fixed interval. A global max may or may not be the same as a local max. A global min (or minimum) is the absolute lowest point on the graph on a fixed interval. A global min may or may not be the same as a local min.

Thomas J. Pfaff
Chapter 3. The Function Gallery

Each graph below is a real world data set with a fitted curve. The fitted curves will be used throughout the text. Note that each graph caption provides the function of the fitted curve, as well as, the beginning and end of the data set where appropriate. These functions are here to generally demonstrate the idea of curve fitting as a way functions are created and specifically to see how well the curves fit the particular data. Examples that are current will be updated and posted on the companion web site https://sustainabilitymath.org/acr/ along with the data and R code for the curve fitting. The exercises at the end of this chapter will help you get used to using functions with units and context. TIP: Once you type these functions into an R script you should save the code so you don’t have to type the function into R repeatedly. WARNING: If you copy and paste the functions in the captions from a pdf into R it may not work, especially if you are using a mac. The most common problem is the negative sign is incorrect, but it can be deleted and retyped.

Thomas J. Pfaff

Change and the Derivative

Frontmatter
Chapter 4. How Fast is CO2 Increasing?

In the Function Gallery, chapter 3 , we were introduced to functions that modeled data. The exercises were focused on the units and finding x-values and y-values, but there is much more information in these graphs. In this chapter we begin to develop quantifying how fast a function changes. We will start with how fast a function changes over an (input) interval by using a secant line which effectively averages the rate of change over an input range. We then begin to develop the notion of how fast a function changes at a specific input value with the goal of quantifying this speed. The analogy here is the speed of a car at moment in time. Once we can quantify change at a point we will be able to explicitly calculate key values of a function that were introduced in Describing a Graph, chapter 2 . We will use the model in figure 4.1, which measures the average amount of carbon dioxide, $$CO_2$$ C O 2 , in the atmosphere each year with the model constructed with data from 1950 through 2017. The function gallery, chapter 3 , has an updated CO2 function with more recent data.

Thomas J. Pfaff
Chapter 5. The Idea of the Derivative

In Chapter 4 we estimated that in 2017 $$CO_2$$ C O 2 levels were increasing at a rate of 2.32 ppm per year. This is an instantaneous measure of change or in a sense the speed of the function at 2017. At this point the 2.32 ppm is an estimate of the slope of the tangent line. Before we turn to calculating tangent line slopes explicitly we want to introduce terminology and notation for the slope of the tangent line and then develop an understanding of the information provided by the slope of the tangent line. In M-Box 5.1 we introduced notation for the derivative or the instantaneous rate of change at a point. In the context of CO2 we write $$CO2'(67) \approx 2.32$$ C O 2 ′ ( 67 ) ≈ 2.32 ppm per year. The prime notation is used to represent the instantaneous rate of change of a specific original function, in this case CO2(t). Note the relationship between the function and the derivative of the function. We use f(a) for the value of the function at $$x=a$$ x = a and $$f'(a)$$ f ′ ( a ) for the derivative or the instantaneous rate of change of the function f(x) at $$x=a$$ x = a .

Thomas J. Pfaff
Chapter 6. Formulas Quantifying Change

We continue with the CO2 function example from chapter 4 chapter and provide some context about changing atmospheric CO2 levels by using different formulas to quantify change. We will focus on the period of the data, which is 1950 to 2017. The total change, M-Box 6.1, in CO2 levels from 1950 to 2017 is $$ CO2(67)\, \text {ppm}-CO2(0)\, \text {ppm} = 95.3 \, \text {ppm}.$$ C O 2 ( 67 ) ppm - C O 2 ( 0 ) ppm = 95.3 ppm . In other words, from 1950 through 2017 atmospheric CO2 levels increased by 95.3 ppm. A related calculation is the average rate of change over a time period, M-Box 6.2. In this case $$ (CO2(67)\,\text {ppm}-CO2(0)\,\text {ppm})/(67 \, \text {year} - 0 \, \text {year}) = 1.42 \text {ppm per year}.$$ ( C O 2 ( 67 ) ppm - C O 2 ( 0 ) ppm ) / ( 67 year - 0 year ) = 1.42 ppm per year . In other words, from 1950 through 2017 CO2 levels increased on average by 1.42 ppm per year. Two details to note. First, the input for the CO2(t) function is years after 1950, but when we report the results we don’t say 67 years after 1950; we say 2017. Similarly, we use year as the unit in the denominator in the calculation in 6.1. The average rate of change is also the slope of the secant line, as seen in Chapter 4 , and averages the change over the time period. This does not mean CO2 necessarily increased by 1.42 ppm per year as this is the yearly average over that time period.

Thomas J. Pfaff
Chapter 7. The Microscope Equation

If we are in a car and we look at the speedometer and it says we are traveling 60 mph, then about how far will we travel in the next minute? 30 seconds? Two minutes? Traveling at 60 mph is the same as a rate of 1 mile per minute and we would answer one mile, a half a mile, and two miles, but we should recognize that in our responses we are assuming the speed is constant. These answers are approximations as we might speed up or slow down, but unless our change in speed is drastic, say slamming on the breaks, the results are likely reasonably close or, in other words, a good estimate. On the other hand, if we wanted to estimate how far we travel over the next hour we would say 60 miles, but we would recognize that this response may not be great as our speed may well change over the course of an hour. Our reasoning here is formalized as the microscope equation in M-Box 7.1.

Thomas J. Pfaff
Chapter 8. Successive Approximations to Estimate Derivatives

In the chapter How Fast is CO2 Increasing?, chapter 4 we estimated the slope of the tangent line at $$x=67$$ x = 67 (or 2017) of the CO2 function in figure 4.1 using $$\begin{aligned} {{\textbf {{\textsf {(CO2(a+h)-CO2(a-h))/(a+h-(a-h))}}}}} \end{aligned}$$ ( CO 2 ( a + h ) - CO 2 ( a - h ) ) / ( a + h - ( a - h ) ) $$a=67$$ a = 67 and $$h=0.01$$ h = 0.01 to get 2.324202 ppm per year. The calculation is presented R Code 4.2 . The question is, how accurate is our estimate of the slope of the tangent line of CO2 at $$x=67$$ x = 67 (or 2017)? Is $$h=0.01$$ h = 0.01 small enough so that the secant line that straddles the tangent line provides an accurate slope estimation? If we look at figure 8.1 which is the CO2 function from figure 4.1 zoomed in with a tangent line at 2017, $$x=67$$ x = 67 , (dashed red line). Two square points are added at $$(67-0.01, CO2( 67-0.01 ) )$$ ( 67 - 0.01 , C O 2 ( 67 - 0.01 ) ) and $$(67+0.01, CO2( 67+0.01 ) )$$ ( 67 + 0.01 , C O 2 ( 67 + 0.01 ) ) . Note that it appears that the tangent line connects the two points and hence is the same as the second line connecting the two points. Based on this graph it would seem that our secant line slope approximation of the tangent line slope at $$x=67$$ x = 67 is good (whatever good means?).

Thomas J. Pfaff
Chapter 9. The Derivative Graphically

In the previous chapter we estimated the slope of the tangent line at one specific point. We now take a qualitative look at the slopes of tangent lines and sketch the graph of the derivative of a function. Consider figure 9.1 which is the graph of $$f(x)=x^2$$ f ( x ) = x 2 with five tangent lines at the points labeled P1 through P5. The slopes of the tangent lines at P1 and P2 are negative, while the slopes at P4 and P5 are positive. If we were to order the slopes at the points we would have P1 < P2 < P3 < P4 < P5. Let $$| \text {P1} |,$$ | P1 | , etc. represent the absolute value of the slope of the tangent line. How would we order the absolute value of the tangent line slopes? The answer is $$| \text {P3} |< | \text {P4} |< | \text {P2} |< | \text {P1} | < | \text {P5} |$$ | P3 | < | P4 | < | P2 | < | P1 | < | P5 | .

Thomas J. Pfaff
Chapter 10. The Formal Derivative as a Limit

As we move from successively approximating the slope of tangent line to a formal algebraic definition, we will capture the idea of successive approximation with that of a limit. Let’s first review figure 10.1, which is a repeat of figure 8.4 .

Thomas J. Pfaff
Chapter 11. Basic Derivative Rules

The definition of the derivative function given in M-Box 10.2 is time-consuming to apply to every function for which we want a derivative. For example, the derivative of $$f(x)=3x^2$$ f ( x ) = 3 x 2 , $$f(x)=5x^2$$ f ( x ) = 5 x 2 , and $$f(x)=7x^2$$ f ( x ) = 7 x 2 would each be a similar yet separate calculation. It turns out we can use the definition of the derivative in M-Box 10.2 to derive general rules so that we do not need to use the limit formula each time. M-Box 11.1 lists basic rules of derivatives. Note that all of these rules are derived by definition of the derivative in M-Box 10.2 . They are not definitions but the result of applying a definition. We prove rule 11.2 and note that the case $$n=2$$ n = 2 for rule 11.3 was done in example 10.2 .

Thomas J. Pfaff
Chapter 12. Product Rule

The CO2 emissions of a country, in metric tons, can be thought of as made up of two parts.

Thomas J. Pfaff
Chapter 13. Quotient Rule

From the proceeding section we recognized that multiplying two functions creates interesting dynamics and so it should not be surprising that dividing functions also creates interesting dynamics. Let us start with an applied example.

Thomas J. Pfaff
Chapter 14. Chain Rule

Before moving to our last derivative result, the chain rule, you might consider reviewing the Appendix on function composition, F . The chain rule is considered the most challenging of the three results, the produce rule, quotient rule, and chain rule, partly due to function composition itself being confusing. We are going to use $$\sin (x^2)$$ sin ( x 2 ) as a main example, which is a composition of $$\sin (x)$$ sin ( x ) and $$x^2$$ x 2 . In other words, if $$f(x)= \sin (x)$$ f ( x ) = sin ( x ) and $$g(x)=x^2$$ g ( x ) = x 2 then $$h(x)=f(g(x)) = \sin (x^2)$$ h ( x ) = f ( g ( x ) ) = sin ( x 2 ) . In this example, $$g(x)=x^2$$ g ( x ) = x 2 is the inside function while $$f(x)=\sin (x)$$ f ( x ) = sin ( x ) is the outside function, because g(x) is inside f(x) in $$h(x)=f(g(x))$$ h ( x ) = f ( g ( x ) ) . Now, if we are given $$h(x)= = \sin (x^2)$$ h ( x ) = = sin ( x 2 ) how do we know this is a composition and how do we know which is the inside function. What may help it to consider what we would do if we were to evaluate h(x) at some value of x, say $$x=5$$ x = 5 . We would first do $$5^2$$ 5 2 to get 25 after that we would then evaluate $$\sin (25)$$ sin ( 25 ) . Here $$x^2$$ x 2 is the inside function because we did that first, squared 5, and then took that output and used it to find $$\sin (25)$$ sin ( 25 ) , making $$\sin (x)$$ sin ( x ) the outside function. We will consider both how function composition changes functions along with the impact on the derivative.

Thomas J. Pfaff
Chapter 15. Derivatives with R

The DerivRDeriv DerivativeR package in R has a function that performs symbolic differentiation. The first line in R Code box 15.1 loads the Deriv package with the library command (this assumes the packages has been installed on your computer with install.packages(“Deriv") as noted in Chapter 1 ). Recall that a package has to be loaded only once per session and so we won’t have library(Deriv) in the examples below after the first example. The second line defines the CO2 function. The third line sets CO2_p to the derivative of CO2 using the Deriv function. We use the convention of adding _p to denote the derivative of a function in R since we don’t have the option of using a prime. In other words, we use f_p for the derivative of f in R because we cannot use $$f'(x)$$ f ′ ( x ) . The last line outputs the derivative function. Note that Deriv(CO2) by itself will produce this output but by assigning CO2_p to Deriv(CO2) we can use the CO2_p function. For example, R Code box 15.2 outputs the derivative of CO2 for 2017 with CO2_p(67), which we estimated in Chapter 4 and calculated in example 11.8 .

Thomas J. Pfaff
Chapter 16. End Behavior of a Function - L’Hospital’s Rule

One of our motivations for this chapter is that it is common to hear someone refer to exponential growth any time they see a graph that is concave up and increasing. This is not true. In the function gallery, for example, both the global temperature and CO2 models are increasing and concave up but the functions are not exponential functions as they are quadratic polynomials. As we will see in this chapter, there is a big difference in the growth of an exponential function as compared to a quadratic polynomial. We start with an example.

Thomas J. Pfaff

Applications of the Derivative

Frontmatter
Chapter 17. How Do We Know the Shape of a Function?

Consider the graph of $$f(x)=x^3-3003x^2+3006000x$$ f ( x ) = x 3 - 3003 x 2 + 3006000 x in figure 17.1. Can we conclude the function does not have any local maximums, local minimums, or inflection points? If we think we do not have the correct window (domain and range of the graph) how do we decide what window to use? In this chapter we will use the derivative, both the first and second, to completely understand the shape of a graph.

Thomas J. Pfaff
Chapter 18. Finding Extremes

A person throws a ball vertically into the air at a speed of 26.8 m/s (about 60mph) and leaving their hand 1.8 meters (about 6ft) above the ground.

Thomas J. Pfaff
Chapter 19. Optimization

Suppose we want to build a fenced in area adjacent to a house and we are limited to 100 feet of fencing based on how fencing is sold. What is the largest area that can be enclosed? This type of problem, one where we want to maximize or minimize something, in this case the area of the rectangle, given some constraint, in this case a limit on the perimeter, is an optimization problem.

Thomas J. Pfaff
Chapter 20. Derivatives of Functions of Two Variables

The idea of the derivative generalizes to functions of more than one variable where can capture the rate of change relative to one of the variables. Consider the function $$V(r,h)=\pi r^2 h$$ V ( r , h ) = π r 2 h which is the volume of a cylinder as seen in figure 20.1. This is the function of the two variables r and h.

Thomas J. Pfaff
Chapter 21. Related Rates

Consider the equation for the area of a circle.

Thomas J. Pfaff
Chapter 22. Surge Function

Figure 22.1 is from the Function Gallery chapter, but we repeat it here as it is central to this chapter. In the chapter our goal is to understand the surge function in general, a function of the form and then study the impact of repeated doses of a chemical over time. For example, in figure 22.1 we see the data and model for one dose of ethanol, but what happens when another dose is taken, say, an hour later, and then again in another hour? We begin more generally or abstractly and then study a few specific examples in the exercises.

Thomas J. Pfaff
Chapter 23. Differential Equations - Preliminaries

A differential equation is an equation involving a function or functions and their derivatives. Differential equations are used to model real world phenomenon. Before analyzing various differential equation models we have some preliminary work to do. In building differential equation we will use the language of variables beginning proportional. In analyzing differential equation we will use a for loop in R. We cover both of these concepts in this chapter. First is M-Box which translates the statement x is proportional to y into the equation $$x=ky$$ x = k y for some constant k. This will be helpful in translating statements about real world situation into differential equations to analyze. Note that there is not anything particularly special about using k as the constant. Other letters can be used especially if they make more sense in context, such as say r if the context is related to a growth rate.

Thomas J. Pfaff
Chapter 24. Differential Equations - Population Growth Models

One of the simplest differential equation models starts with the observation that a population will grow at a rate proportional to its size, assuming no resource limitations. In other words, the larger the population the faster it grows.

Thomas J. Pfaff
Chapter 25. Differential Equations - Predator Prey

The previous differential equation models involved only one species. We now consider a differential equation model with two species that interact. One classic example explores the interaction between, say, foxes (predators) and rabbits (prey). Let R(t) and F(t) represent the number of rabbits and foxes time t. We assume that rabbits will grow proportional to their population with growth constant $$b_1$$ b 1 .

Thomas J. Pfaff
Chapter 26. Differential Equations - SIR Model

Epidemiology is the study of the incidence, distribution, and possible control of diseases. In this section we consider a model to understand how a disease moves through a population. We let S(t) be the number of people susceptible to the disease, I(t) be the number of people infected with the disease, and R(t) be the number of people recovered from the disease at time t, which is often days. For now we assume a person can only get the disease once and is then immune to the disease once recovered. We will build a set of differential equations with these three functions to model the spread of a disease through a population.

Thomas J. Pfaff
Chapter 27. Project: The Gini Coefficient—Prelude to Section III

The distribution of energy consumption in the U.S. (2014 data) and World (2011 data) can be modeled by $$ECus(x) = 7.2038917391x^6 -17.8551679663x^5+ 16.5816140612x^4-7.0654275059x^3+ 1.7077246274x^2+ 0.4260396828x$$ E C u s ( x ) = 7.2038917391 x 6 - 17.8551679663 x 5 + 16.5816140612 x 4 - 7.0654275059 x 3 + 1.7077246274 x 2 + 0.4260396828 x and $$ECw(x)= 678.0352163746x^9-2796.2519054480x^8+4802.0852334478x^7-4441.8091503689x^6+ 2389.4054597788x^5-751.8800491391x^4+ 132.3874503758x^3-11.3747211453x^2+0.3569478992x$$ E C w ( x ) = 678.0352163746 x 9 - 2796.2519054480 x 8 + 4802.0852334478 x 7 - 4441.8091503689 x 6 + 2389.4054597788 x 5 - 751.8800491391 x 4 + 132.3874503758 x 3 - 11.3747211453 x 2 + 0.3569478992 x . Both functions and the related data are shown in figure 3.8 in the Function Gallery chapter.Gini coefficient

Thomas J. Pfaff

Accumulation and the Integral

Frontmatter
Chapter 28. Area Under Curves

Up to this point we have developed techniques to extra information about curves related to their rate of change. We can now quantify how fast a curve is increasing or decrease, identify maximum and minimum points, and identify inflection points. There is still more valuable information in the graphs that we would like to quantify. For example, in the Function Gallery figure 3.8 has data and models to represent distribution of energy consumption in the U.S. and World. The line $$y=x$$ y = x would represent perfect equality of the distribution of energy. The area between the curve and $$y=x$$ y = x is used to quantify how much the given resource, in this case energy, deviates from equality; known as the Gini coefficient (technically the Gini coefficient is this area divided by 2). The problem now is how do we calculate this area? Here is another example.

Thomas J. Pfaff
Chapter 29. The Accumulation Function

In Chapter 9 we sketched graphs of the derivative of a function before we had a formal algebraic way to computer derivatives. We realized that we can view the derivative as a function related to the original function. Here we will proceed in a similar way and qualitatively graph how much (signed) area is accumulated between a function and the x-axis (note: we will often just say the area under the curve) as we move along the x-axis.

Thomas J. Pfaff
Chapter 30. The Fundamental Theorem of Calculus

From Chapter 29 we recognize that there is a relationship between the accumulation function and the derivative. Specifically, if we start with the graph of f(x) and sketch its accumulation function A(x) then the slope or derivative graph of A(x) is just f(x) again. This relationship is formalized with The Fundamental Theorem of Calculus given in M-Box 30.1. Note that the starting point, given by a in the formula, is not specified. In fact, any a will do as the effect of a is to simply shift A(x) up or down. Shifting a function up or down does not change its derivative.

Thomas J. Pfaff
Chapter 31. Techniques of Integration - The u Substitution

This chapter and the next add to our techniques for finding antiderivatives. The basic integration techniques given in M-Box 30.4 basically take the basic derivative rules from M-Box 30.4 and reverses them.

Thomas J. Pfaff
Chapter 32. Techniques of Integration - Integration by Parts

In deriving a formula for reversing the product rule we start with the product rule $$ (f(x)g(x))' =f'(x)g(x) + f(x)g'(x) $$ ( f ( x ) g ( x ) ) ′ = f ′ ( x ) g ( x ) + f ( x ) g ′ ( x ) and then integrate both sides with respect to x to get $$ \int (f(x)g(x))'dx =\int f'(x)g(x) dx + \int f(x)g'(x) dx. $$ ∫ ( f ( x ) g ( x ) ) ′ d x = ∫ f ′ ( x ) g ( x ) d x + ∫ f ( x ) g ′ ( x ) d x . Now $$\int (f(x)g(x))'dx =f(x)g(x)$$ ∫ ( f ( x ) g ( x ) ) ′ d x = f ( x ) g ( x ) and so $$ f(x)g(x) =\int f'(x)g(x) dx + \int f(x)g'(x) dx $$ f ( x ) g ( x ) = ∫ f ′ ( x ) g ( x ) d x + ∫ f ( x ) g ′ ( x ) d x or $$ \int f(x)g'(x) dx = f(x)g(x) - \int f'(x)g(x) dx. $$ ∫ f ( x ) g ′ ( x ) d x = f ( x ) g ( x ) - ∫ f ′ ( x ) g ( x ) d x .

Thomas J. Pfaff
Backmatter
Metadata
Title
Applied Calculus with R
Author
Thomas J. Pfaff
Copyright Year
2023
Electronic ISBN
978-3-031-28571-4
Print ISBN
978-3-031-28570-7
DOI
https://doi.org/10.1007/978-3-031-28571-4

Premium Partner