-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_set.m
135 lines (121 loc) · 3.11 KB
/
level_set.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function save_X = level_set(P,phi, c,refine)
if (nargin<4)
refine = 1;
end
% if (nargin==0) %testing
% %%% test set
% clc
% clear all
% close all
% refine = 1;% if (nargin==0) %testing
% %%% test set
% clc
% clear all
% close all
% refine = 1;
%
% zstar = [5 1.4]; %v, h
% robot = parms;
% TE = 0.5*robot.m*zstar(1)*zstar(1) + robot.m*robot.g*zstar(2);
% [x,F,INFO] = lyapmain(zstar);
% p11 = x(1);
% p12 = 0;
% p22 = x(2);
% P = [p11 p12; p12 p22];
% c = 1; %find level set X'*P*X - c = 0
% end
%
% zstar = [5 1.4]; %v, h
% robot = parms;
% TE = 0.5*robot.m*zstar(1)*zstar(1) + robot.m*robot.g*zstar(2);
% [x,F,INFO] = lyapmain(zstar);
% p11 = x(1);
% p12 = 0;
% p22 = x(2);
% P = [p11 p12; p12 p22];
% c = 1; %find level set X'*P*X - c = 0
% end
if (det(P)< 0)
error('P is not positive definite');
end
% semi_major = norm(max(eig(inv(P))))
% semi_minor = norm(min(eig(inv(P))))
% area = pi*semi_major*semi_minor
% bound = sqrt(c)*semi_major
if (P(1,2)~=0)
error('Formula for ellipse does not apply to non-diagonal P matrix');
end
a = sqrt(c/P(1,1));
b = sqrt(c/P(2,2));
%area = pi*a*b
%x = linspace(-a,a,round(a)*10*refine+1);
% if (nargin==0)
% for i=1:length(x)
% xxx(i,1) = x(i)+zstar(1);
% x2= xxx(i);
% yyy(i,1) = (TE - 0.5*robot.m*x2*x2)/(robot.m*robot.g);
% end
% end
%save_X = [];
theta = linspace(0,2*pi,10*refine+1);
%theta = theta(1:end-1); %ignore last point (repeat of 0)
cc = cos(phi);
ss = sin(phi);
x = a*cc*cos(theta) - b*ss*sin(theta);
y = a*ss*cos(theta) + b*cc*sin(theta);
%x = zstar(1) + a*c*cos(theta) - b*s*sin(theta);
%y = zstar(2) + a*s*cos(theta) + b*c*sin(theta);
% x = a*cos(theta);
% y = b*sin(theta);
save_X = [x', y'];
% for j=1:2
% for i=1:length(x)
% if (i==1)
% y0 = 0;
% end
% %[y,fval,exitflag] = fsolve(@curve,y0,options,x(i),P,c);
% if (j==1) %positive y solution
% lb = 0;
% ub = [];
% else %negative y solution
% ub = 0;
% lb = [];
% end
% %X = fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)
% if (j==1)
% xx = x(i);
% [y,fval,exitflag] = fmincon(@dummy,y0,[],[],[],[],lb,ub,@curve,options,xx,P,c);
% else
% xx = x(length(x)-i+1);
% [y,fval,exitflag] = fmincon(@dummy,y0,[],[],[],[],lb,ub,@curve,options,xx,P,c);
% end
% if (exitflag==1)
% save_X = [save_X; xx y];
% end
% y0 = y;
% end
% end
%
% if (nargin==0)
% %save_X
% %[2*length(x) length(save_X)]
% figure(1)
% patch(save_X(:,1)+zstar(1),save_X(:,2)+zstar(2),'ro-'); hold on;
% plot(xxx,yyy,'k','Linewidth',2);
% %[xx' yy']
% axis('equal');
% grid on
% figure(2)
% plot(save_X(:,1)+0*zstar(1),save_X(:,2)+0*zstar(2),'ko-'); hold on
% %plot(x,yy,'k','Linewidth',2);
% axis('equal');
% grid on
% figure(1)
% end
% function C = dummy(y,x,P,c)
% C = 1;
%
% function [cin,ceq] = curve(y,x,P,c)
% X= [x y]';
% ceq = X'*P*X - c;
% cin = [];