Lab 6 Solutions % Question 1 % (b) % File lab6q1a.m: function y = lab6q1a(x) y = x^4 - 2*x^2 + 5; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Results: >> x=fminbnd('lab6q1a',0,10) x = 1.0000 >> x=fmin('lab6q1a',0,10) Warning: FMIN is obsolete and has been replaced by FMINBND. FMIN now calls FMINBND which uses the following syntax: [X,FVAL,EXITFLAG,OUTPUT] = FMINBND(FUN,x1,x2,OPTIONS,P1,P2,...) Use OPTIMSET to define optimization options, or type 'edit fmin' to view the code used here. FMIN will be removed in the future; please use FMINBND with the new syntax. (Type "warning off MATLAB:fmin:ObsoleteFunction" to suppress this warning.) > In \\Rome\local\win\matlab\6.5\toolbox\matlab\funfun\fmin.m at line 54 x = 1.0000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (c) % File lab6q1c.m: function y = lab6q1c(x) y = 4*x^3 - 4*x; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Results [there are 3 zeros of f'(x)=0]: >> x=fzero('lab6q1c',2) x = 1 >> x=fzero('lab6q1c',-2) x = -1 >> x=fzero('lab6q1c',0.5) x = 9.7894e-026 %-------------------------------------------------------------------------- % Question 2 >> B=[7 2 -4 ; 2 4 -2 ; -4 -2 -7]; >> eig(B) ans = -8.2321 3.0000 9.2321 >> p=poly(B) p = 1.0000 -4.0000 -73.0000 228.0000 >> roots(p) ans = -8.2321 9.2321 3.0000 %-------------------------------------------------------------------------- % Question 3 >> A=[7 -2 1 ; -2 10 -2 ; 1 -2 7]; >> [P,D]=eig(A) P = 0.1826 0.8944 -0.4082 -0.3651 0.4472 0.8165 -0.9129 0 -0.4082 D = 6.0000 0 0 0 6.0000 0 0 0 12.0000 >> Ptran=P' Ptran = 0.1826 -0.3651 -0.9129 0.8944 0.4472 0 -0.4082 0.8165 -0.4082 >> P*Ptran ans = 1.0000 -0.0000 -0.0000 -0.0000 1.0000 0.0000 -0.0000 0.0000 1.0000 >> Ptran*P ans = 1.0000 0.0000 0.0000 0.0000 1.0000 0 0.0000 0 1.0000 >> B=P*D*Ptran B = 7.0000 -2.0000 1.0000 -2.0000 10.0000 -2.0000 1.0000 -2.0000 7.0000 %-------------------------------------------------------------------------- % Question 4 % (a) % File lab6q4a.m: function y = lab6q4a(x) y = 6*x(1)^2 + 8*x(2)^3 + 3*(2*x(1)+x(2)-1)^2; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Results: % Convergence depends on initial point: [1,1] converges but [1.1,1.1] % diverges. >> x=fminsearch('lab6q4a',[1,0]) x = 0.2500 0.2500 >> x=fminsearch('lab6q4a',[1,1]) x = 0.2500 0.2500 >> x=fminsearch('lab6q4a',[1.1,1.1]) Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: -17697348702218202000000000000000... x = 1.0e+043 * -0.1916 -6.0479 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (b) % File lab6q4b.m: function grad_f = lab6q4b(x) grad_f(1) = 12*x(1) + 12*(2*x(1)+x(2)-1); grad_f(2) = 24*x(2)^2 + 6*(2*x(1)+x(2)-1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Results: % f0 is the value of grad_f at the solution x. It should be zero. % There are 2 zeros: % x = [0.2500 0.2500], i.e. [1/4 1/4], agrees with the minimum in Part (a); % x = [0.4444 -0.3333], i.e. [4/9,-3/9], cannot be found using the method % of Part (a), since it is not a minimum of f. >> [x,f0]=fsolve('lab6q4b',[1 1]) Warning: Cannot determine from calling sequence whether to use new (2.0 or later) FSOLVE function or grandfathered FSOLVE function. Assuming new syntax; if call was grandfathered FSOLVE syntax, this may give unexpected results. To force new syntax and avoid warning, add options structure argument: x = fsolve(@sin,3,optimset('fsolve')); To force grandfathered syntax and avoid warning, add options array argument: x = fsolve(@sin,3,foptions); > In \\Rome\local\win\matlab\6.5\toolbox\optim\fsolve.m (parse_call) at line 389 In \\Rome\local\win\matlab\6.5\toolbox\optim\fsolve.m at line 101 Optimization terminated successfully: First-order optimality is less than options.TolFun. x = 0.2500 0.2500 f0 = 1.0e-014 * -0.0888 0.2665 >> [x,f0]=fsolve('lab6q4b',[-1 -1]) Warning: Cannot determine from calling sequence whether to use new (2.0 or later) FSOLVE function or grandfathered FSOLVE function. Assuming new syntax; if call was grandfathered FSOLVE syntax, this may give unexpected results. To force new syntax and avoid warning, add options structure argument: x = fsolve(@sin,3,optimset('fsolve')); To force grandfathered syntax and avoid warning, add options array argument: x = fsolve(@sin,3,foptions); > In \\Rome\local\win\matlab\6.5\toolbox\optim\fsolve.m (parse_call) at line 389 In \\Rome\local\win\matlab\6.5\toolbox\optim\fsolve.m at line 101 Optimization terminated successfully: First-order optimality is less than options.TolFun. x = 0.4444 -0.3333 f0 = 1.0e-012 * -0.0009 0.1643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (c) >> x=[1/4,1/4]; >> H(1,1) = 36; >> H(1,2) = 12; >> H(1,2) = 12; >> H(2,2) = 24*x(2) + 6; >> H H = 36 12 0 12 >> eig(H) ans = 36 12 % Since both eigenvalues are positive, [1/4,1/4] is a local minimum. >> x=[4/9,-3/9]; >> H(1,1) = 36; >> H(1,2) = 12; >> H(1,2) = 12; >> H(2,2) = 24*x(2) + 6; >> H H = 36 12 0 -2 >> eig(H) ans = 36 -2 % Since one eigenvalue is positive and the other negative, [4/9,-3/9] is % a saddle point. %-------------------------------------------------------------------------- % Question 5 % Plot the level contours of the function as in Q4(d) and the minimum point % [sqrt(3*pi/2),sqrt(3*pi/2)]. The minimum is not unique. >> xx=0:0.01:4; >> yy=0:0.01:4; >> [x,y]=meshgrid(xx,yy); >> z=sin(x.*y)-cos(x-y); >> contour(xx,yy,z,100) >> hold on >> plot(sqrt(3*pi/2),sqrt(3*pi/2),'x') %-------------------------------------------------------------------------- % Question 5 % (c) % File lab6q6c.m: function grad_L = lab6q6c(x) % x(1)=x, x(2)=y, x(3)=s, x(4)=lambda. grad_L(1) = 2*x(1) - 6 + 4*x(4); grad_L(2) = 4*x(2) - 6 + 2*x(4); grad_L(3) = 2*x(3)*x(4); grad_L(4) = 4*x(1) + 2*x(2) + x(3)^2 - 12; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Results: >> [x,f0]=fsolve('lab6q6c',[1 1 1 1]) Warning: Cannot determine from calling sequence whether to use new (2.0 or later) FSOLVE function or grandfathered FSOLVE function. Assuming new syntax; if call was grandfathered FSOLVE syntax, this may give unexpected results. To force new syntax and avoid warning, add options structure argument: x = fsolve(@sin,3,optimset('fsolve')); To force grandfathered syntax and avoid warning, add options array argument: x = fsolve(@sin,3,foptions); > In \\Rome\local\win\matlab\6.5\toolbox\optim\fsolve.m (parse_call) at line 389 In \\Rome\local\win\matlab\6.5\toolbox\optim\fsolve.m at line 101 Optimization terminated successfully: First-order optimality is less than options.TolFun. x = 2.3333 1.3333 -0.0000 0.3333 f0 = 1.0e-009 * -0.0000 -0.0000 -0.1333 0.1687 % Optimal solution: x*=2.3333, y*=1.3333, s*=-0.0000, lambda*=0.3333, % i.e. x*=7/3, y*=4/3, s*=0, lambda*=1/3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (d) % File lab6q6d.m: function f = lab6q6d(x) % x(1)=x, x(2)=y. f = x(1)^2 + 2*x(2)^2 - 6*x(1) - 6*x(2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Results: % x = [2.3333 1.3333], i.e. x = [7/3 4/3.] >> x0=[1 1]; >> A=[4 2]; >> b=12; >> Aeq=[]; >> beq=[]; >> lb=[0 0]; >> ub=[]; >> [x f]=fmincon('lab6q6d',x0,A,b,Aeq,beq,lb,ub) Warning: Large-scale (trust region) method does not currently solve this type of problem, switching to medium-scale (line search). > In \\Rome\local\win\matlab\6.5\toolbox\optim\fmincon.m at line 213 Optimization terminated successfully: First-order optimality measure less than options.TolFun and maximum constraint violation is less than options.TolCon Active Constraints: 3 x = 2.3333 1.3333 f = -13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (e) % Results: % x = [2.3333 1.3333], i.e. x = [7/3 4/3.] >> H=[2 0 ; 0 4]; >> f=[-6 -6]; >> A=[4 2]; >> b=12; >> Aeq=[]; >> beq=[]; >> lb=[0 0]; >> ub=[]; >> [x z]=quadprog(H,f,A,b,Aeq,beq,lb,ub) Warning: Large-scale method does not currently solve this problem formulation, switching to medium-scale method. > In \\Rome\local\win\matlab\6.5\toolbox\optim\quadprog.m at line 213 Optimization terminated successfully. x = 2.3333 1.3333 z = -13.0000