In: Advanced Math
On Matlab use BFGS Method to find the minimum of the following function: f(x) = x13 - 2x2x12 + x12 - x1using initial point (x0, y0) = (1, 2)T to start, and stop when f changes less than 0.0001
clear all;
clc;
H=eye(2,2);
syms a;
f=@(x,y) x^3-2*y*x^2+x^2-x;
g=@(x,y) deal(3*x^2-4*x*y+2*x-1,-2*x^2);
% f=@(x,y) 100*(y-x.^2).^2+(1-x).^2;
% g = @(x,y) deal( 400*x.^3-400*x*y+2*x-2,200*y-200*x.^2);
X=[1 2 ]';
N=7;
sum=X;
n=nargin(f);
for i=1:N
[u v]=g(X(1),X(2));
grad=[u v]';
d=-H*grad;
sum=sum+a*d;
z=f(sum(1),sum(2));
fd=matlabFunction(z);
derivative= matlabFunction(diff(fd(a)));
derivative1=matlabFunction(diff(derivative(a)));
my_val = 1;
n = 10;
for i=1:n
my_val=my_val-(derivative(my_val)/subs(sym(derivative1),{a},{my_val}));
end
alpha=my_val;
X=X+alpha*d;
sum=X;
deltax=alpha*d;
[n m]=g(X(1),X(2));
grad1=[n m]';
deltag=grad1-grad;
H=H+(deltax-H*deltag)*(deltax-H*deltag)'/(deltag'*(deltax-H*deltag));
%H=H+(deltax*deltax')/(deltax'*deltag)-(H*deltag)*(H*deltag)'/(deltag'*H*deltag);
% d=-H*grad1;
if (abs(n)< 1e-4&&abs(m)<1e-4)
break
end
end
% disp('The Solution is (X,Y):')
disp(vpa(X))