-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathQplot.m
49 lines (41 loc) · 799 Bytes
/
Qplot.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function Qplot(A,b,c,r,n)
%
% Plots graph of quadratic function
% Q(v) = v' * A * v - v * b + c
% where v = [ x y]
% A is a 2 by 2 matrix, b a vector and c a scalar
% optional arguments:
% r - range of plot -r <= x & y <=r
% default = 5
% n - number of mesh points
% default = 25
%
if nargin < 4, r = 5; end
if nargin < 5, n = 25; end
x = linspace(-r,r,n);
y = x;
max = -10000; min = 10000;
for i=1:n,
for j=1:n,
v = [x(j),y(i)];
t = (1/2)* v * A * v' - v * b + c;
if t > max
max = t;
end
if t < min
min = t;
end
z(i,j) = t;
end
end
surf(x,y,z);
colormap hsv;
shading interp;
if abs(det(A)) > .01
del = (max - min)/7;
x0 = A\b;
l = line([x0(1) x0(1)], [x0(2) x0(2)], [min-del,max+del]);
set(l,'color',[1 0 1]);
else
'Matrix close to singular'
end