% Example of the lagrange function for a 1-d constrained minimization problem % f(x)=x^2 --> min subject to constraint x=1 % this problem is trivially simple: the function needs to be minimized just on a single point % the solution is, of course, minimum at x=1 % the Lagrange's function for this problem looks like % L(x,lambda)=x^2+lambda*(x-1) % and Lagrange's methods consists in minimizing L(x, lambda) over space of TWO variables x and lambda % let us check if Lagrange's method still gives the right answer x=1 (value of lambda in unimportant) figure(1); n=20; % note: y plays the role of lambda [x,y] = meshgrid([-3:1/n:3],[-5:1/n:5]); z = x.^2+y.*(x-1); surf(x,y,z, 'FaceColor', 'green', 'EdgeColor', 'none'); xlabel('original variable'); ylabel('artificial variable'); camlight left; %%% add a line to indicate where the critical point occure hold; [x,y] = meshgrid(1,[-5:1/n:5]); plot3(x,y,x.^2+y.*(x-1),'r-'); [x,y] = meshgrid([-3:1/n:3],-2); plot3(x,y,x.^2+y.*(x-1),'b-');