% SDQ script demonstrates steepest descent % with a quadratic objective function. % % Requires: sampleq.m [function M-file] % close all clc % % Coefficients of the quadratic objective function % % q(x) = (1/2) x'*H*x - b'*x + c % H = [7 5; 5 6]; b = [15; -30]; c = 21; % % Minimizer xstar = H \ b; % % First draw a contour map, showing the minimizer as a magenta star. % x = linspace(xstar(1)-2,xstar(1)+3,65); y = linspace(xstar(2)-3,xstar(2)+2,65); Z = sampleq(x,y,H,b,c); contour(x,y,Z,15); axis('equal') xlabel('x'), ylabel('y') hold on plot(xstar(1),xstar(2),'m*') % % User clicks mouse to set initial point for steepest descent % xc = zeros(2,1); title('Click to select starting point for Steepest Descent') [xc(1),xc(2)] = ginput(1) title('Minimizing a quadratic function by Steepest Descent') m = 0; % m counts completed iterations while (m<10) % Quit when 10 iterations completed pc = b - H*xc; % Steepest descent direction = residual t = (pc'*pc)/(pc'*H*pc); % Calculate distance to new xc xn = xc + t*pc; % Show the line segment from current point to new point. % Add the contour thru the new point to the plot. plot([xc(1) xn(1)],[xc(2) xn(2)],'r') qc = sampleq(xn(1),xn(2),H,b,c); contour(x,y,Z,[qc qc]); xc = xn; % Move to the new point m = m+1; % Bump the iteration counter end