clear all
for i=1:2,
if i==1,
A=[42 0 ; 0 20]; b=[-2 -20]';x0=[0 -1];
else
A=[-18 0 ; 0 20]; b=[-2 10]';x0=[0 .5];
end
x1=[];
x2=[];
delta=[];
lambda=[];
if i==1,
lambda=0:1:1000;
else
lambda=18.5:1:1000.5;
end
for ilambda=lambda,
  xs=-(A+ilambda*eye(2))\b;
  x1=[x1 xs(1)];
  x2=[x2 xs(2)];
  delta=[delta norm(xs)];
end
%x1=[x1 0];
%x2=[x2 0];
%x1=[x1 1];
%x2=[x2 1];
%x1=x1(length(x1):-1:1);
%x2=x2(length(x2):-1:1);
if i==1,
x=[-3:.05:3];y=[-3:.05:3];
z=zeros(length(x),length(y));
for i=1:length(x),
for j=1:length(y),
  p=[x(i);y(j)];
  z(i,j)=b'*p+.5*p'*A*p;
end
end
%y=y-1;     %  shift to trust region center - do NOT shift back
%x2=x2-1;     %  shift to trust region center - do NOT shift back
disp('solutions of the trust region subproblem at point 0, -1 but centered at origin')
figure(2)
plot(lambda,delta)
title('lambda versus delta values at point 0, -1 but centered at origin')
xlabel('lambda values')
ylabel('delta values')
figure(1)
contour(x,y,z',25);
colorbar
hold on
xy=-A\b;
plot(xy(1),xy(2),'xb')
x=[-1.1:.05:1.1];y=[-1.1:.05:1.1];
z=zeros(length(x),length(y));
for i=1:length(x),
for j=1:length(y),
  p=[x(i);y(j)];
  z(i,j)=p'*p;
end
end
contour(x,y,z',25);
plot(x1,x2,'xg')
title('solutions of the trust region subproblem at point 0, -1 but centered at origin')
xlabel('x_1 values')
ylabel('x_2 values')
hold off
else
x=[-2:.05:2];y=[-2:.05:2];
z=zeros(length(x),length(y));
for i=1:length(x),
for j=1:length(y),
  p=[x(i);y(j)];
  z(i,j)=b'*p+.5*p'*A*p;
end
end
%x2=x2+.5;     %  shift to trust region center  -- not shifted
disp('solutions of the trust region subproblem at point 0, 0.5 but centered at origin')
figure(4)
plot(lambda,delta)
title('lambda versus delta values at point 0, 0.5 but centered at origin')
xlabel('lambda values')
ylabel('delta values')
figure(3)
contour(x,y,z',25);
colorbar
hold on
plot(x1,x2,'xg')
x=[-1.1:.05:1.1];y=[-1.1:.05:1.1];
z=zeros(length(x),length(y));
for i=1:length(x),
for j=1:length(y),
  p=[x(i);y(j)];
  z(i,j)=p'*p;
end
end
contour(x,y,z',25);
title('solutions of the trust region subproblem at point 0, 0.5 but centered at origin')
xlabel('x_1 values')
ylabel('x_2 values')
hold off
end
end
hold off
