Figure 1.6:

Contours of constant f(x)=x^TAx.

Code for Figure 1.6

Text of the GNU GPL.

main.m


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
% definite
% A = [2, 0; 0, 1];
% theta = pi/4;
% semi-definite
% A = [0, 0; 0, 1];
% theta = -pi/4;
% indefinite
A = [-1, 0; 0, 1];
theta = pi/8;
%theta = 0;
% rotate
rot = [cos(theta), -sin(theta); sin(theta), cos(theta)];
A = rot'*A*rot;
npts = 51;
v = [0;1;2;3;-1;-2;-3];
x = linspace(-3,3,npts);
y = x;
xy = [kron(x,ones(1,npts)); kron(ones(1,npts),y)];
z = reshape(diag(xy'*A*xy),npts,npts);
c = contour(x,y,z,v);
% parse for data file and gnuplot
ic = 1;
file = fopen("quadhyp.dat", "w");
for i = 1:2*length(v)
  len = c(2,ic);
  dump = c(:,ic+1:ic+len);
  ic = ic + len+1;
  fprintf (file, '%f %f \n', dump);
  if(i == 2*length(v))
    break
  end%if
  fprintf (file, '\n \n');
end
fclose (file);