Question: In Matlab: Demo code: clc %clear command window clf %clear current figure clear all %clear workspace variables format compact %show less whitespace format long %show
In Matlab:

Demo code:
clc %clear command window clf %clear current figure clear all %clear workspace variables format compact %show less whitespace format long %show accuracy with more digits tolg=1e-9; %tolerance on the gradient tolx=1e-8; %tolerance on the value of x
Problem = 2 %1-4 z=[2.5;2.5;2.5]; Algorithm='D'; % N - Newton's Method % D - Diagonal Scaled Steepest Descent % S - Steepest Descent % B - BFGS switch Problem case 1 A=[0.339,-0.033,0.066;-0.033,1.0,0.199;0.066,0.199,0.704]; b=[1.017;2.1;1.3]; f=@(x) 0.5*x'*A*x-b'*x; g=@(x) A*x-b; H=@(x) A; M=@(x) diag(A); case 2 A=[0.011,-0.001,0.01;-0.001,1.0,0.2;0.01,0.2,0.14]; b=[0.041;2.197;0.57]; f=@(x) 0.5*x'*A*x-b'*x; g=@(x) A*x-b; H=@(x) A; M=@(x) diag(A); case 3 A=[0.339,-0.033,0.066;-0.033,1.0,0.199;0.066,0.199,0.704]; b=[2.985;3.902;2.094]; f=@(x) 0.5*(x.^2)'*A*(x.^2)-b'*(x.^2); g=@(x) 2*(A*(x.^2)-b).*x; H=@(x) diag(2*A*(x.^2))+4*(x*x').*A-2*diag(b); M=@(x) 2*A*(x.^2)+4*x.^2.*diag(A)-2*b; case 4 A=[0.011,-0.001,0.01;-0.001,1.0,0.2;0.01,0.2,0.14]; b=[0.105;4.191;1.03]; f=@(x) 0.5*(x.^2)'*A*(x.^2)-b'*(x.^2); g=@(x) 2*(A*(x.^2)-b).*x; H=@(x) diag(2*A*(x.^2))+4*(x*x').*A-2*diag(b); M=@(x) 2*A*(x.^2)+4*x.^2.*diag(A)-2*b; end
fc=0; %counter for function evaluations gc=0; %counter for gradient evaluations hc=0; %counter for hessian evaluations dhc=0; %counter for hessian diagonals evaluations
%Calculate Initial State switch Algorithm case 'N' fk=f(z); fc=fc+1; gk=g(z); gc=gc+1; Hk=H(z); hc=hc+1; case 'S' fk=f(z); fc=fc+1; gk=g(z); gc=gc+1; case 'D' fk=f(z); fc=fc+1; gk=g(z); gc=gc+1; Mk=M(z); dhc=dhc+1; end
for k=1:10000 switch Algorithm case 'N' %Calculate Descent Direction d=-Hk\gk; %Calculate Descent Step slope=gk'*d; [zn,cn]=Armijo(z(:,k),d,fk,f,slope,0.5,0.1,1.0); z(:,k+1)=zn; fc=fc+cn; if norm(z(:,k+1)-z(:,k))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
