# Untitled

## 506 days ago by pub

Problem: Find the extreme values of the function $f(x,y)=(x-1)(y-2)$ in the region where $(x-1)^2+(y-2)^2 \le 2$ .

#our first plot of the first function; it is explicitly defined z=f(x,y) var ('x y') plot3d((x-1)*(y-2), (x,-5,5),(y,-5,5), figsize=4, opacity=.7)
#our first plot of the second function; you need to see that it is implicitly defined (a circle) var ('x y') implicit_plot((x-1)^2+(y-2)^2==2, (x,-5,5),(y,-5,5), figsize=3)
#we "straighten out" the above plot (make its zoom 1-1) and add axes var ('x y') implicit_plot((x-1)^2+(y-2)^2==2, (x,-2,4),(y,-1,5), axes=1, aspect_ratio=1, figsize=3)
#if we want we can graph the inequality using region_plot var ('x y') region_plot((x-1)^2+(y-2)^2<=2, (x,-2,5),(y,0,5), axes=1, figsize=3)
#we make it's zoom: 1-1 var ('x y') region_plot((x-1)^2+(y-2)^2<=2, (x,-2,4),(y,-1,5), axes=1, aspect_ratio=1, figsize=3)

The following were purposely done incorrectly so that the user can see the problems.

1. In the first, we have tried to use implicit_plot in a 3d plot. Sage requires implicit_plot3d.
2. In the second, we did not include a range on z.
var ('x y') p1=plot3d((x-1)*(y-2), (x,-5,5),(y,-5,5)) c1=implicit_plot((x-1)^2+(y-2)^2==2, (x,-2,4),(y,-1,5)) show(p1+c1)
 Traceback (click to the left of this block for traceback) ... NotImplementedError: 3D plotting not implemented for ContourPlot defined by a 150 x 150 data grid Traceback (most recent call last): File "", line 1, in File "_sage_input_38.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyICgneCB5JykKcDE9cGxvdDNkKCh4LTEpKih5LTIpLCAoeCwtNSw1KSwoeSwtNSw1KSkKYzE9aW1wbGljaXRfcGxvdCgoeC0xKV4yKyh5LTIpXjI9PTIsICh4LC0yLDQpLCh5LC0xLDUpKQpzaG93KHAxK2MxKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in File "/tmp/tmpunlAuw/___code___.py", line 6, in exec compile(u'show(p1+c1) File "", line 1, in File "base.pyx", line 128, in sage.plot.plot3d.base.Graphics3d.__add__ (sage/plot/plot3d/base.c:2811) File "/home/sage/sage-4.8/local/lib/python2.6/site-packages/sage/plot/plot.py", line 1348, in plot3d g = Graphics3dGroup([g.plot3d(**kwds) for g in self.__objects]) File "/home/sage/sage-4.8/local/lib/python2.6/site-packages/sage/plot/primitive.py", line 83, in plot3d raise NotImplementedError, "3D plotting not implemented for %s" % self._repr_() NotImplementedError: 3D plotting not implemented for ContourPlot defined by a 150 x 150 data grid
var ('x y') p1=plot3d((x-1)*(y-2), (x,-5,5),(y,-5,5)) c1=implicit_plot3d((x-1)^2+(y-2)^2==2, (x,-2,4),(y,-1,5) ) show(p1+c1)
 Traceback (click to the left of this block for traceback) ... TypeError: implicit_plot3d() takes exactly 4 arguments (3 given) Traceback (most recent call last): File "", line 1, in File "_sage_input_39.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyICgneCB5JykKcDE9cGxvdDNkKCh4LTEpKih5LTIpLCAoeCwtNSw1KSwoeSwtNSw1KSkKYzE9aW1wbGljaXRfcGxvdDNkKCh4LTEpXjIrKHktMileMj09MiwgKHgsLTIsNCksKHksLTEsNSkgKQpzaG93KHAxK2MxKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in File "/tmp/tmpUgOtHZ/___code___.py", line 5, in c1=implicit_plot3d((x-_sage_const_1 )**_sage_const_2 +(y-_sage_const_2 )**_sage_const_2 ==_sage_const_2 , (x,-_sage_const_2 ,_sage_const_4 ),(y,-_sage_const_1 ,_sage_const_5 ) ) File "lazy_import.pyx", line 199, in sage.misc.lazy_import.LazyImport.__call__ (sage/misc/lazy_import.c:1509) TypeError: implicit_plot3d() takes exactly 4 arguments (3 given)
var ('x y z') p1=plot3d((x-1)*(y-2), (x,-5,5),(y,-5,5)) c1=implicit_plot3d((x-1)^2+(y-2)^2==2, (x,-2,4),(y,-1,5),(z,-1,10) ) show(p1+c1)
#now fix up the double plot so we can see the region of the first function inside the cylinder (second function) where we are to look for extreme values var ('x y z') xmn=-1; xmx=3; ymn=0; ymx=4; zmn=-1; zmx=5 p2=plot3d((x-1)*(y-2), (x,xmn,xmx),(y,ymn,ymx), opacity=.8) c2=implicit_plot3d((x-1)^2+(y-2)^2==2,(x,xmn,xmx),(y,ymn,ymx),(z,zmn, zmx), color='green', axes=1, opacity=.5 ) show(p2+c2)

Start of analytical solution. As always, to find extreme values, we need to find the stationary points. Here we look for stationary points within the region and on the boundary of the region.

However, because our region is closed and bounded, the Extreme Value Theorem guarantees us a maximum and minimum.

This means that we need only calculate the function values at the stationary points. The largest is the maximum; the smallest is the minimum.

f(x,y)=(x-1)*(y-2) pdx(x,y)=diff(f,x) print pdx
 (x, y) |--> y - 2 (x, y) |--> y - 2
pdy(x,y)=diff(f,y) print pdy
 (x, y) |--> x - 1 (x, y) |--> x - 1

Stationary points are points where pdx=0 and pdy=0. Here easy result: $(x,y)=(1,2)$

This result and the method used to obtain it (i.e. solving using our eyeballs) is particular to this problem.

We are looking only at points inside the region. Is (1,2) in the region?

#we define leftside(x,y)=(x-1)^2+(y-2)^2 leftside(1,2)
 0 0

Since 0≤2, the point falls in the region. So we need to test it. Because we have the extreme value theorem, we do not need to test this point with our "big" formulas.  We "just" need to find the stationary points on the boundary of the region and calculate the value of the function on all these points. The biggest value is the maximum, the smallest the minimum.

Just in case you want to see how to get that this is a saddle point for the whole of the function (not just restricted to the region), here you  go...

We now find the 2nd partials and use our tests on this point.

pdxx(x,y)=diff(pdx,x) pdyy(x,y)=diff(pdy,y) pdxy(x,y)=diff(pdx,y) Disc=pdxx(1,2)*pdyy(1,2)-pdxy(1,2)^2 print Disc
 -1 -1
#Using typical notation, we repeat the above A(x,y)=diff(pdx,x) B(x,y)=diff(pdy,y) C(x,y)=diff(pdx,y) D(x,y)=A*B-C^2 D(1,2)
 -1 -1

Since D<0 at the stationary point, this point is a saddle point. Our conclusion is based on the test formulas for extreme values and we did not use Sage to come to this conclusion.

Notice that this result agrees with our image above.

We move to the boundary. The boundary of the circle is now a constraint and we use a Lagrange multiplier to look for stationary points.

var('lam') con(x,y)=(x-1)^2+(y-2)^2-2 G(x,y,lam)=f(x,y)+lam*con(x,y)
Gx=diff(G,x); Gy=diff(G,y); Glam=diff(G,lam) print Gx print Gy print Glam
 (x, y, lam) |--> 2*(x - 1)*lam + y - 2 (x, y, lam) |--> 2*(y - 2)*lam + x - 1 (x, y, lam) |--> (y - 2)^2 + (x - 1)^2 - 2 (x, y, lam) |--> 2*(x - 1)*lam + y - 2 (x, y, lam) |--> 2*(y - 2)*lam + x - 1 (x, y, lam) |--> (y - 2)^2 + (x - 1)^2 - 2

#We find the stationary points here by setting these 3 derivatives =0 and solving.

Setiing the first two equations equal to each other, we find $lam=\frac{1}{2}$ or $x=y-1$. Substituting this result into the third equation, we get $(y-2)^2=1$ which gives $y=3$ or $y=1$.

We get four points:(0,3, lam=0.5), (2,3, lam=-0.5), (0,1, lam=-0.5), (2,1, lam=0.5).

We note that we have written these points out as if we were going to test them with our big formulas (see below).

However, using the Extreme Value Theorem, we will calculate the value of the function on all five points (the 1 from the interior and these 4). The biggest value is the maximum, the smallest the minimum.

#Using typical notation, we repeat the above A1(x,y,lam)=diff(Gx,x) B1(x,y,lam)=diff(Gy,y) C1(x,y,lam)=diff(Gx,y) D1(x,y,lam)=A*B-C^2 D1(0,3,0.5)
 0.000000000000000 0.000000000000000
#Now adding the results of the constraints problem f(x,y)=(x-1)*(y-2) #Minimum is z=-1 at (x,y)=: (0,3) and (2,1). pt1=point3d((0,3,f(0,3)), size=30, color='red') pt2=point3d((2,1,f(2,1)), size=30, color='red') show(p2+c2+pt1+pt2)
#Maximum is z=1 at (x,y)=: (2,3) and (0,1). pt3=point3d((2,3,f(2,3)), size=30, color='yellow') pt4=point3d((0,1,f(0,1)), size=30, color='yellow') show(p2+c2+pt3+pt4)
#radius of cylinder is sqrt(2) var ('x y z') xmn=-.42; xmx=2.42; ymn=.58; ymx=3.42; zmn=-1.1; zmx=1.1 p2=plot3d((x-1)*(y-2), (x,xmn,xmx),(y,ymn,ymx), opacity=.8) c2=implicit_plot3d((x-1)^2+(y-2)^2==2,(x,xmn,xmx),(y,ymn,ymx),(z,zmn, zmx), color='green', opacity=.5 ) show(p2+c2,figsize=4)