• MATH 135: AMC I
  • Preface
  • R Studio Orientation
    • Getting Oriented
      • First login
      • The RStudio window
      • Packages
    • RMarkdown
  • RStudio Quick Reference
    • Loading Mosaic and MosaicCalc Packages
    • Troubleshooting
    • Functions and Data
      • Common Functions
      • Defining Your Own Function
      • Creating Lists of Data
      • Plotting Data
      • Plotting Functions
      • Plotting Data and Functions
      • Fitting a Function to Data
      • Plotting Surfaces in 3D
    • Differentiation
      • Estimating the Derivative
      • Estimating the Partial Derivative
      • Gradient Search
      • Constrained Optimization
    • Integration
      • Estimating the Definite Integral
    • Differential Equations
      • Euler’s Method
      • Slope Fields
      • Creating a Trajectory in RStudio
      • SIR Model
  • Intro to RStudio
    • Getting Started
    • R Markdown files
      • Formatting Regular Text
      • Code Chunks
    • Writing Code
      • Basic Calculations
      • Variables, Lists and Sequences
      • Functions
      • Plotting
    • Troubleshooting
      • You Try
  • I Functions
  • 1.A Functions
    • Steps of the Modeling Cycle
    • Goals
    • Activities
      • Life insurance
      • Bat species
      • Slope and concavity
      • Relative change of the Dow Jones average
      • Measuring rainfall
    • Solutions
      • Life insurance
      • Bat species
      • Slope and concavity
      • Relative change of the Dow Jones average
      • Measuring rainfall
  • 1.B Linear Functions
    • Goals
    • Linear Funtions
    • Activities
      • Graph of a linear function
      • Height and weight of American men
      • Scooter rates
      • Oil reserves
    • Solutions
      • Graph of a linear function
      • Height and weight of American men
      • Scooter rates
      • Oil reserves
  • 1.C Average Rate of Change
    • Goals
    • Activities
      • AROC of a function
      • Measuring daily rainfall
      • Sketching a function
      • Slope and Concavity
      • Example Function
      • Vinyl LPs
      • Relative change of the Dow Jones average
    • Solutions
      • AROC of a function
      • Measuring daily rainfall
      • Sketching a function
      • Slope and Concavity
      • Example Function
      • Vinyl LPs
      • Relative change of the Dow Jones average
  • 1.D Exponential and Logarithmic Functions
    • Goals
    • Activities
      • Growing population
      • Consumer price index
      • Two data sets
      • Interest rate for an investment
      • Exponential decay
      • Doubling time
      • People and vehicles
    • Solutions
      • Growing population
      • Consumer price index
      • Two data sets
      • Interest rate for an investment
      • Exponential decay
      • Doubling time
      • People and vehicles
  • 1.E Power Functions
    • Goals
    • Activities
      • Examples and non-examples
      • Descriptions of power functions
      • Allometry of pectoral fins
      • Specific heat
    • Solutions
      • Examples and non-examples
      • Descriptions of power functions
      • Allometry of pectoral fins
      • Specific heat
  • 1.F Periodic Functions
    • Goals
    • Activities
      • Periodic function examples
      • Find a periodic formula
      • Variable star
      • Tides and currents
      • Rainfall in Seattle
    • Solutions
      • Periodic function examples
      • Find a periodic formula
      • Variable star
      • Tides and currents
      • Rainfall in Seattle
  • 1.G Multivariable Functions
    • Goals
    • Activities
      • Matching Contours
      • Plotting in RStudio
      • Cross Sections
      • Plotting in RStudio
    • Solutions
      • Matching Contours
      • Plotting in RStudio
      • Cross Sections
      • Plotting in RStudio
  • Unit 1 Summary
    • Functions
    • Linear Functions
    • Average Rate of Change
    • Exponential and Logarithmic Functions
    • Power Functions
    • Periodic Functions
    • Multivariable Functions
  • II Units and Estimation
  • 2.A Dimensions and Units
    • Activities
      • Find the Dimension
      • Dimensionally Feasible
      • Geometric Formulas for a Cylinder
      • Unit Conversions
    • Solutions
      • Find the Dimension
      • Dimensionally Feasible
      • Geometric Formulas for a Cylinder
      • Unit Conversions
  • 2.B Dimensional Analysis
    • Performing a Dimensional Analysis
    • Activities
      • Vocal Chord Frequency
      • Mixer Power
      • Velocity of an Ocean Wave
    • Solutions
  • 2.C Fermi Estimation
    • Working Fermi Problems
    • Some Useful Data
    • Activities
    • Solutions
  • Unit 2 Summary
    • Dimensions and Units
    • Dimensional Analysis
    • Fermi Estimation
  • III Computational Derivatives
  • 3.A The Derivative
    • The Derivative
    • Activities
      • Studying
      • Marginal Cost
      • Farmland in the United States
      • A Moving Particle
    • Solutions
      • Studying
      • Marginal Cost
      • Farmland in the United States
      • A Moving Particle
  • 3.B The Second Derivative
    • Interpreting the First and Second Derivative
    • Activities
      • Estimating the first and second derivatives from a graph
      • Function Matching
      • A Plot of Velocity
      • Characterize the Derivatives
    • Solutions
      • Estimating the first and second derivatives from a graph
      • Function Matching
      • A Plot of Velocity
      • Characterize the Derivatives
  • 3.C Partial Derivatives
    • Activities
      • Estimating From Data
      • Estimating From a Contour Plot
      • Estimating Using Desmos
    • Solutions
      • Estimating From Data
      • Estimating From a Contour Plot
      • Estimating Using Desmos
  • 3.D Local Linear Approximation
    • Activities
      • Estimating Sugar Solution Temperature
      • Comparing Linearizations
      • Estimating a Function
      • Estimating the Windchill
    • Solutions
      • Estimating Sugar Solution Temperature
      • Comparing Linearizations
      • Estimating a Function
      • Estimating the Windchill
  • Unit 3 Summary
    • The Derivative
    • The Second Derivative
    • Partial Derivatives
    • Local Linear Approximation
  • IV Symbolic Derivatives
  • 4.A Rules of Differentiation
    • Derivatives of Familiar Functions
      • Constant Rule
      • Linear Function Rule
      • Power Rule
      • Trig Rules
      • Exponential Rules
      • Logarithmic Rules
    • Arithmetic Rules for Derivatives
      • Constant Multiple Rule
      • Sum and Difference Rules
    • Rules for Combinations of Functions
      • Product Rule
      • Quotient Rule
      • Chain Rule
  • 4.B Symbolic Differentiation
    • Activities
      • Basic Derivative Practice
      • More Basic Derivative Practice
      • Product Rule and Quotient Rule Practice
    • Solutions
      • Basic Derivative Practice
      • More Basic Derivative Practice
      • Product Rule and Quotient Rule Practice
  • 4.C The Chain Rule
    • Activities
      • Chain Rule Practice
      • More Chain Rule Practice
      • Conceptual Chain Rule Problems
      • Visual Chain Rule Problem
    • Solutions
      • Chain Rule Practice
      • More Chain Rule Practice
      • Conceptual Chain Rule Problems
      • Visual Chain Rule Problem
  • 4.D The Gradient
    • Activities
      • Partial Derivative Practice
      • Second Partial Derivative Practice
      • Gradient Practice
      • Gradient on a Contour Diagram
      • Gradient and the Direction of Greatest Increase
    • Solutions
      • Partial Derivative Practice
      • Second Partial Derivative Practice
      • Gradient Practice
      • Gradient on a Contour Diagram
      • Gradient and the Direction of Greatest Increase
  • Unit 4 Summary
    • Symbolic Derivatives
    • Partial Derivatives
    • The Gradient
  • V Optimization
  • 5.A 1D Optimization
    • Activities
      • Characterize the Extrema
      • First Derivative Test
      • Second Derivative Test
      • Cupcake Store
    • Solutions
      • Characterize the Extrema
      • First Derivative Test
      • Second Derivative Test
      • Cupcake Store
  • 5.B 2D Optimization
    • Summary
    • Activities
      • Characterize the Extrema
      • Classifying a critical point using a small circle of values
      • The 2D Second Derivative Test
      • Classify the Critical Points
      • Optimizing Flight Control
    • Solutions
      • Characterize the Extrema
      • Classifying a critical point using a small circle of values
      • The 2D Second Derivative Test
      • Classify the Critical Points
      • Optimizing Flight Control
  • 5.C Gradient Search
    • Gradient Search Example
    • Activities
      • Finding a Local Minimum
    • Solutions
      • Finding a Local Minimum
  • 5.D Constrained 2D Optimization
    • Creating a Contour Plot for 2D Optimization
    • Activities
      • Constrained Production
      • Constrained Optimization on a Circle
      • Estimating the Lagrange Multiplier
    • Solutions
      • Constrained Production
      • Constrained Optimization on a Circle
      • Estimating the Lagrange Multiplier
  • Unit 5 Summary
    • 1D Optimization
    • 2D Optimization
    • Gradient Search
    • Constrained Optimization
  • VI Integration
  • 6.A Accumulating Change
    • Activities
      • Growing Bacteria
      • The Change in World Population
      • Speed Tests
      • Estimating Area with RStudio
    • Solutions
      • Growing Bacteria
      • The Change in World Population
      • Speed Tests
      • Estimating Area with RStudio
  • 6.B The Definite Integral
    • Introduction
    • Activities
      • Drive My Car
      • Accelerate My Car
      • Making Cupcakes
      • Area Between Curves
    • Solutions
      • Drive My Car
      • Accelerate My Car
      • Making Cupcakes
      • Area Between Curves
  • Rules of Integration
    • The Fundamental Theorem of Calculus
    • Integrals of Familiar Functions
      • Constant Rule
      • Power Rules
      • Trig Rules
      • Exponential Rules
      • Logarithmic Rules
    • Arithmetic Rules for Integrals
      • Constant Multiple Rule
      • Sum and Difference Rules
      • Substitution Rule
    • Rules for Endpoints
      • Decomposition Rule
      • Zero Integral Rule
  • 6.C The Indefinite Integral
    • Activities
      • Indefinite Integral Practice
      • Definite Integral Practice
      • Area Between Curves
      • Average Value
      • Modeling Population Growth
      • Zombie Apocalypse
    • Solutions
      • Indefinite Integral Practice
      • Definite Integral Practice
      • Area Between Curves
      • Average Value
      • Modeling Population Growth
      • Zombie Apocalypse
  • Unit 6 Summary
    • Accumulating Change
    • The Definite Integral
    • The Indefinite Integral
  • VII Differential Equations
  • 7.A Modeling Change
    • Activities
      • Building Differential Equations
      • Solve the Differential Equation
      • Verifying Solutions to Differential Equations
    • Solutions
      • Building Differential Equations
      • Solve the Differential Equation
      • Verifying Solutions to Differential Equations
  • 7.B Population Models
    • Important Models
      • Exponential Growth/Decay
      • Exponential Growth/Decay with Constant Input/Output
      • Constrained Growth
      • Newton’s Law of Cooling
    • Activities
      • Drug Metabolization
      • Elementary, My Dear Watson
      • Optimal Coffee Temperature
      • Constrained Rabbit Population
      • Constrained Deer Population
    • Solutions
      • Drug Metabolization
      • Elementary, My Dear Watson
      • Optimal Coffee Temperature
      • Constrained Rabbit Population
      • Constrained Deer Population
  • 7.C Euler’s Method
    • Introduction
      • Euler’s Method in RStudio
    • Activities
      • Euler’s Method for Two Steps
      • Calculating an Euler Approximation
      • Estimating Drug Metabolization
      • Euler’s Method with Different Step Sizes
    • Solutions
      • Euler’s Method for Two Steps
      • Calculating an Euler Approximation
      • Estimating Drug Metabolization
      • Euler’s Method with Different Step Sizes
  • 7.D Slope Fields
    • Creating a Slope Field in RStudio
    • Creating a Trajectory in RStudio
    • Activities
      • Match the Slope Field
      • Draw Some Trajectories
      • Removing a Pollutant
      • Slope Fields for Population Models
      • Trajectories for Population Models
    • Solutions
      • Match the Slope Fields
      • Draw Some Trajectories
      • Removing a Pollutant
      • Slope Fields for Population Models
      • Trajectories for Population Models
  • 7.E SIR Model
    • The SIR Model for the Spread of Disease
      • Plotting an SIR Slope Field and an SIR Trajectory
    • Activities
      • Threshold Population for an SIR Model
      • Ending an Outbreak Before Everyone Gets Sick
      • Managing an Outbreak
      • Changing Infection Rate Versus Changing Removal Rate
    • Solutions
      • Threshold Population for an SIR Model
      • Ending an Outbreak Before Everyone Gets Sick
      • Managing an Outbreak
      • Changing Infection Rate Versus Changing Removal Rate
  • Unit 7 Summary
    • Differential Equations
    • Population Models
    • Euler’s Method
    • Slope Fields
    • SIR Model
  • Published with bookdown

MATH 135: AMC I

5.C Gradient Search

Gradient Search Example

Let’s use gradient search to find the maximum of the function \[ f(x,y) = - x^4 - x^3 + 10 x y + 2y - 8 y^2 \] whose partial derivatives are \[ \frac{\partial f}{\partial x} = -4 x^3 -3 x^2 + 10 y \quad \mbox{and} \quad \frac{\partial f}{\partial x} = 10 x + 2 - 16 y \]

First, you define the partial derivatives and then choose your starting point (newx, newy). In this case, we start at (1,1).

partialx = makeFun( -4*x^3 -3*x^2 + 10*y ~ x&y)
partialy = makeFun(10*x + 2 - 16*y ~ x&y)
newx = 1
newy = 1

Next, you repeatedly run the following code block, which updates the current point by moving 0.1 times the gradient vector. This takes a small step in the uphill direction.

slopex=partialx(newx, newy)
slopey=partialy(newx, newy)
newx = newx + 0.05*slopex
newy = newy + 0.05*slopey
# print new partial derivatives
c(partialx(newx, newy), partialy(newx, newy))
# print new point
c(newx, newy)

Repeatedly run this code block until the partial derivatives are essentially zero (at least two zeros after the decimal point). Congrats! You have found your local maximum.

You will end at the point (1.04,0.77). But note that starting at another initial point might take you to a different local maximum!

You can check that this critical point is a local maximum (and not a saddle point) by using the same method that we used for 2D Optimizatiion

f = makeFun( -x^4 - x^3 + 10*x*y + 2* y - 8* y^2 ~ x&y)
a=1.0405
b=0.7753
r = 0.1
theta = seq(0,2*pi,pi/10)
f(a,b) - f(a+r*cos(theta), b+r*sin(theta))
##  [1] 0.10152236 0.06983861 0.04586756 0.03913009 0.05231624
##  [6] 0.07998000 0.11073639 0.13203698 0.13536588 0.11957064
## [11] 0.09102447 0.06080448 0.04028374 0.03696369 0.05199590
## [16] 0.08002000 0.11113281 0.13426810 0.14099672 0.12862949
## [21] 0.10152236

Activities

Finding a Local Minimum

  1. How should I change to the code if I want to find a local minimum instead of a local maximum? (Hint: we want to take a small step downhill.)
  2. Use gradient search to find a local minimum of \[f(x,y) = x^2 + 2 x y + 3 x + 4 y + 5 y^2.\]

Solutions

Finding a Local Minimum

  1. In order to find a local minimum, we need to move in the opposite direction of the gradient. This is the “downhill direction.” All we need to do in the code is replace the two + 0.1 with - 0.1.

  2. We want to find the local minimum of \[f(x,y) = x^2 + 2 x y + 3 x + 4 y + 5 y^2.\] We have \[ f_x(x,y) = 2x +2y + 3, \qquad f_y(x,y) = 2x + 4 + 10y. \] Here is our updated code to walk downhill. We set up our partials and pick our starting point.

partialx = makeFun(2*x +2*y + 3 ~ x&y)
partialy = makeFun(2*x + 4 + 10*y ~ x&y)
newx = 1
newy = 1

Now we repeatedly run this “move downhill” block until the partial derivatives are (basically) zero.

slopex=partialx(newx, newy)
slopey=partialy(newx, newy)
newx = newx - 0.05*slopex
newy = newy - 0.05*slopey
# print new partial derivatives
c(partialx(newx, newy), partialy(newx, newy))
## [1] 4.7 7.3
# print new point
c(newx, newy)
## [1] 0.65 0.20

If we run this 20 or 30 times, the partial derivatives become zero, and we estimate our local minimum to be the point \((-1.37, -0.13)\).