# 222 - Topic 09 - Multivariate Extrema

## 667 days ago by Professor222

Extrema for Multivariate Functions

John Travis

Mississippi College

This sage worksheet takes a given multivariate function and compute various derivatives, critical points and the other stuff necessary to locate critical points and aid in describing any possible extreme values.  Students using this for preparing homework should copy the results of each cell into a word processor and prepare a report with all conclusions.  In other words, let Sage do the calculus and the student do the interpretation/application.  If pasting formulas into a Word document, the formulas might look funny.  That's ok.

Don't forget to get a nice perspective on your surface with all the points plotted (at the bottom) before pasting it into your document.

%auto # To start things off, enter the function f that you want to use. var('x,y') # f = x^2 - 3*x*y - y^2 # f = x^2*y+y^3-12*y # f = 2*x^3-3*x^2*y+3*x^2-y^3+3*y-4 # f = y^3-3*y*x^2-3*y^2-3*x^2+1 # f = x*sin(y) # this one has infinitely many critical points so the solver has fits f = (1/2-x^3+y^2)*e^(1-x^2-y^2) show(f)

(Be certain to evaluate each cell--in order--from the top.  The results from each active cell should print below the cell.)

 The gradient vector  The gradient vector 
fxx = diff(fx,x) fyy = diff(fy,y) fxy = diff(fx,y) print 'The collection of second partial derivatives' show([fxx,fyy,fxy])
 The collection of second partial derivatives  The collection of second partial derivatives 
solns = solve(grad,x,y,solution_dict=True) print(html('The collection of solutions to grad(f)=0')) show((sol[x],sol[y]) for sol in solns)
 The collection of solutions to grad(f)=0  The collection of solutions to grad(f)=0 
# To display the results nicely to determine extreme nature... numerical = 'true' for sol in solns: if numerical: html('At critical point $%s$,'%str(latex((sol[x],sol[y])))) d = (fxx(x=sol[x],y=sol[y])*fyy(x=sol[x],y=sol[y])-fxy(x=sol[x],y=sol[y])^2).n() html(' d = $%s$ '%str(latex(d))+'with $f_{xx} = %s.'%str(latex((fxx(x=sol[x],y=sol[y])).n()))) else: html('At critical point$%s$,'%str(latex((sol[x],sol[y])))) d = (fxx(x=sol[x],y=sol[y])*fyy(x=sol[x],y=sol[y])-fxy(x=sol[x],y=sol[y])^2) html(' d =$%s$'%str(latex(d))+'with$f_{xx} = %s.'%str(latex((fxx(x=sol[x],y=sol[y])))))
 At critical point $\left(0, 0\right)$,  d = $-7.38905609893065$ with $f_{xx} = -2.71828182845905.$ At critical point $\left(-1, 0\right)$,  d = $3.00000000000000$ with $f_{xx} = -3.00000000000000.$ At critical point $\left(-\frac{1}{2} \, \sqrt{3} + \frac{1}{2}, 0\right)$,  d = $4.09792171044052$ with $f_{xx} = 1.91110153464245.$ At critical point $\left(\frac{1}{2} \, \sqrt{3} + \frac{1}{2}, 0\right)$,  d = $12.0792937243984$ with $f_{xx} = 4.70932592374470.$ At critical point $\left(0, -\frac{1}{2} \, \sqrt{2}\right)$,  d = $10.8731273138362$ with $f_{xx} = -3.29744254140026.$ At critical point $\left(0, \frac{1}{2} \, \sqrt{2}\right)$,  d = $10.8731273138362$ with $f_{xx} = -3.29744254140026.$ At critical point $\left(-\frac{2}{3}, -\frac{1}{18} \, \sqrt{11} \sqrt{6}\right)$,  d = $-3.29384795201535$ with $f_{xx} = 0.315932863601581.$ At critical point $\left(-\frac{2}{3}, \frac{1}{18} \, \sqrt{11} \sqrt{6}\right)$,  d = $-3.29384795201535$ with $f_{xx} = 0.315932863601581.$ At critical point ,  d = with  At critical point ,  d = with  At critical point ,  d = with  At critical point ,  d = with  At critical point ,  d = with  At critical point ,  d = with  At critical point ,  d = with  At critical point ,  d = with
# Determine the ranges of the extrema components to get bounds for a nice graph minx = min(sol[x] for sol in solns) maxx = max(sol[x] for sol in solns) miny = min(sol[y] for sol in solns) maxy = max(sol[y] for sol in solns) print 'range of x values: ',(minx,maxx) print 'range of y values: ',(miny,maxy)
 range of x values: (-1, 1/2*sqrt(3) + 1/2) range of y values: (-1/2*sqrt(2), 1/2*sqrt(2)) range of x values: (-1, 1/2*sqrt(3) + 1/2) range of y values: (-1/2*sqrt(2), 1/2*sqrt(2))
# Use this cell if the above cell returns a range of values that is not definite # minx = # maxx = # miny = -pi # maxy = pi # Solns = needs to be a finite string of values...how to get rid of the possible z?? terms?
print 'A nice plot of the given surface with critical points.' Surf = plot3d(f,(x,minx-1,maxx+1),(y,miny-1,maxy+1),color='yellow') delx = (maxx-minx)/6 dely = (maxy-miny)/6 Points = [] G = Graphics() for sol in solns: z = f(x=sol[x],y=sol[y]) Points.append((sol[x],sol[y],z)) G += point3d((sol[x],sol[y],z),size=20) G += text3d('(%s,'%str(sol[x])+'%s,'%str(sol[y])+'%s)'%str(z),(sol[x]+delx,sol[y]+dely,z+1),horizontal_alignment='left', vertical_alignment='bottom') # Dots = point3d(Points,size=10) (Surf+G).show()
 A nice plot of the given surface with critical points. A nice plot of the given surface with critical points.
# The whole thing in one cell var('x,y') @interact def _(f = input_box(2*x^3-3*x^2*y+3*x^2-y^3+3*y-4,label='$f(x,y) =$')): fx = diff(f,x) fy = diff(f,y) grad = [fx,fy] fxx = diff(fx,x) fyy = diff(fy,y) fxy = diff(fx,y) solns = solve(grad,x,y,solution_dict=True) for sol in solns: html('At critical point $%s$,'%str(latex((sol[x],sol[y])))) d = (fxx(x=sol[x],y=sol[y])*fyy(x=sol[x],y=sol[y])-fxy(x=sol[x],y=sol[y])^2) html(' d = $%s$ '%str(latex(d))+'with $f_{xx} = %s.'%str(latex(fxx(x=sol[x],y=sol[y])))) solns = solve(grad,x,y,solution_dict=True) minx = min(sol[x] for sol in solns) maxx = max(sol[x] for sol in solns) miny = min(sol[y] for sol in solns) maxy = max(sol[y] for sol in solns) print html('A nice plot of$f(x,y) = %s\$ with critical points.'%str(latex(f))) Surf = plot3d(f,(x,minx-1,maxx+1),(y,miny-1,maxy+1),color='yellow') delx = (maxx-minx)/6 dely = (maxy-miny)/6 Points = [] G = Graphics() for sol in solns: z = f(x=sol[x],y=sol[y]) Points.append((sol[x],sol[y],z)) G += point3d((sol[x],sol[y],z),size=20) G += text3d('(%s,'%str(sol[x])+'%s,'%str(sol[y])+'%s)'%str(z),(sol[x]+delx,sol[y]+dely,z+1),horizontal_alignment='left', vertical_alignment='bottom') # Dots = point3d(Points,size=10) (Surf+G).show()

## Click to the left again to hide and once more to show the dynamic interactive window

var('x,y,z') implicit_plot3d((x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=[1,3,5], region=lambda x,y,z: x<=0.2 or y>=0.2 or z<=0.2).show()