222 - Lagrange Multiplier Solving

3213 days ago by Professor222

Solving a 3D problem maximization/minimization problem with one constraint using the method of Lagrange Multipliers.

John Travis

Mississippi College

 

var('x,y') # To start things off, enter the function f that you want to use. #f = y^3-3*y*x^2-3*y^2-3*x^2+1 #f = 3*x+2*y+4 #f = x^2-y^2+x*y #f = x^3*y^2 #f = 2*y+(2+pi)*x #f = x^2/4+10*x+3*y^2/20+12*y #f = x^2-y^2 #f = (x+y)/(x^2+y^2+1) # f=2*x^3-3*x^2*y+3*x^2-y^3+3*y-4 f = 3*x+y+10 show(f) 
       

                                
                            

                                
# Next, we need a constraint equation of the form g(x,y)= 0. (Note, "C" is inside g.) # Also, the constraint curve needs to be entered as a vector valued function r(t) = <u(t),v(t)> var('t') #g(x,y) = x^2+y^2-1 #u(t)=cos(t) #v(t)=sin(t) #g(x,y)=3*x^2+2*y^2/4-10 #u(t) = sqrt(10/3)*cos(t) #v(t) = sqrt(5)*sin(t) #g(x,y)=x+y-1000 #u(t)=t #v(t)=1000-t # g(x,y) = 2*y-x^2 #u(t) = t #v(t) = t^2/2 g(x,y)= x^2*y-6 u(t) = t v(t) = 6/t^2 tstart=1 tend=2 
       

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

fx = diff(f,x) fy = diff(f,y) gradf = [fx,fy] gx = diff(g,x) gy = diff(g,y) gradg = [gx,gy] var('lamb') solns_boundary = solve([gx==lamb*fx, gy==lamb*fy, g==0],(x,y,lamb),solution_dict=True) print 'Extrema on the boundary may occur at the lagrange points' show((sol[x],sol[y]) for sol in solns_boundary) 
       
Extrema on the boundary may occur at the lagrange points
Extrema on the boundary may occur at the lagrange points
real_solns = [] for sol in solns_boundary: if sol[x]==conjugate(sol[x]): real_solns.append(sol) show((sol[x],sol[y]) for sol in real_solns) 
       

                                
                            

                                
Curve = parametric_plot3d((u(t),v(t),f(x=u(t),y=v(t))),(t,tstart,tend),color='red') 
       
F = plot3d(f,(x,-5,5),(y,-5,5),color='yellow') Curve = parametric_plot3d((u(t),v(t),f(x=u(t),y=v(t))),(t,tstart,tend),color='red') Points = point3d([(sol[x],sol[y],f(x=sol[x],y=sol[y])) for sol in real_solns]) show(F+Curve+Points,stereo='redcyan',spin=true,perspective_depth=true ) 
       
print 'Numerical Approximations to the exact Lagrange solutions:' for k in range(len(solns_boundary)): show((N(solns_boundary[k][x]),N(solns_boundary[k][y]))) 
       
Numerical Approximations to the exact Lagrange solutions:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(0.000000000000000, -4.47213595499958\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(0.000000000000000, 4.47213595499958\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(-2.01566827545025 - 0.0586392610913160i, 0.335454562851329 - 2.11409790833570i\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(-2.01566827545025 + 0.0586392610913160i, 0.335454562851330 + 2.11409790833570i\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1.94670275820887 - 0.153385864321230i, -0.956144218023746 - 1.87375510628445i\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1.94670275820887 + 0.153385864321230i, -0.956144218023745 + 1.87375510628445i\right)
Numerical Approximations to the exact Lagrange solutions:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(0.000000000000000, -4.47213595499958\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(0.000000000000000, 4.47213595499958\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(-2.01566827545025 - 0.0586392610913160i, 0.335454562851329 - 2.11409790833570i\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(-2.01566827545025 + 0.0586392610913160i, 0.335454562851330 + 2.11409790833570i\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1.94670275820887 - 0.153385864321230i, -0.956144218023746 - 1.87375510628445i\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1.94670275820887 + 0.153385864321230i, -0.956144218023745 + 1.87375510628445i\right)