Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168714
Image: ubuntu2004

Optimizing a Function

This worksheet will help support finding maximum and minimum values of a function. It will also help you visualize and support your work in finding intervals on which a function is increasing, decreasing, concave up, and concave down. 

The first and second derivatives of a function can help us understand where a function has maximum and minimum values.  For the code below, type in a function and then the computer will find the first and second derivatives, find when they are zero, and then graph the function along the way to show you how each new piece of information contributes to our knowledge about the function.

Make sure you choose an xrange that will include all of the parts of the function that you wish to see. If you evaluate all the code below and find critical points outside your viewing range, then come back and pick a new viewing range.

var('x,y,ypp') f(x)=4*x^3-x^4 xrange=(-2,5) yrange=(-10,30) p=plot(f,xrange,ymin=yrange[0],ymax=yrange[1]) p.show()

We can find the maximum and minimum values of the function by finding the critical values, the places where the slope is zero (or undefined). The code below:

  1. Finds f(x)f'(x)
  2. Solves f(x)=0f'(x)=0.
  3. Find the (x,y)(x,y) coordinates where f=0f'=0
  4. Adds to the graph each (x,y)(x,y) point as well as a small horizontal tangent line there.
df=f.diff() df(x)
-4*x^3 + 12*x^2
critical_values=solve(df(x)==0,x) critical_values
[x == 0, x == 3]
critical_points=[[cp,y==f(x).subs_expr(cp)] for cp in critical_values] critical_points
[[x == 0, y == 0], [x == 3, y == 27]]
for cp in critical_points: pt=(x.subs_expr(cp[0]),y.subs_expr(cp[1])) if (pt[0].imag()==0): p+=point(pt,pointsize=20,rgbcolor='black') p+=line([(pt[0]-1,pt[1]),(pt[0]+1,pt[1])],rgbcolor='black')
p.show()

Hyper critical values (where the 2nd derivative is zero) help us find inflection points and determine concavity. The code below:

  1. Finds f(x)f''(x)
  2. Solves for when f=0f'' =0
  3. Find the (x,y)(x,y) coordinates where f''=0
  4. Add to the graph the points found together with the tangent line there (note how the function crosses through the tangent line at inflection points).
df2=f.diff(2) df2
x |--> -12*x^2 + 24*x
hyper_critical_values=solve(df2(x)==0,x) hyper_critical_values
[x == 0, x == 2]
hyper_critical_points=[[cp,y==f(x).subs_expr(cp)] for cp in hyper_critical_values] hyper_critical_points
[[x == 0, y == 0], [x == 2, y == 16]]
for hcp in hyper_critical_points: pt=(x.subs_expr(hcp[0]),y.subs_expr(hcp[1])) if(pt[0].imag()==0): p+=point(pt,pointsize=20,rgbcolor='red') p+=plot(pt[1]+diff(f)(x=pt[0])*(x-pt[0]),(x,pt[0]-1,pt[0]+1),color='red')
p.show()

At each critical value, we can compute the second derivative to help us determin if the function has a maximum or minimum there.

  • If the second derivative is positive at a critical value, then the function is concave upwards where there is a horizontal tangent line, hence the function has a minimum there.
  • If the second derivative is negative at a critical value, then the function is concave upwards where there is a horizontal tangent line, hence the function has a minimum there.
second_der_at_critical_points=[[cp,ypp==f(x).diff(2).subs_expr(cp)] for cp in critical_values] second_der_at_critical_points
[[x == 0, ypp == 0], [x == 3, ypp == -36]]
for cp in critical_points: pt=(x.subs_expr(cp[0]),y.subs_expr(cp[1])) if(pt[0].imag()==0): dir=f.diff(2)(x=pt[0]) p+=arrow(pt,(pt[0],pt[1]+dir),rgbcolor='purple')
p.show()