-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjfun.m
40 lines (33 loc) · 818 Bytes
/
objfun.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
function obj = objfun(X, params)
m = params.m;
l = params.l;
J = m*l^2;
K = X(end-1);
Kd = X(end);
ix = 1:params.nstates;
iu = params.nstates+(1:params.ncontrols);
obj = 0;
for j = 1:params.NSU
for i = 1:params.NperSU-1
theta = X(ix(1));
dtheta = X(ix(2));
u = (X(iu)+K*theta+Kd*dtheta)/J;
obj = obj+1/2*u^2;
if theta > pi/2
obj = obj+0000*(pi/2-theta)^2;
end
ix = ix+params.nvarpernode;
iu = iu+params.nvarpernode;
end
%Final cost
theta = X(ix(1));
dtheta = X(ix(2));
u = (X(iu)+K*theta+Kd*dtheta)/J;
obj = obj + 1/2*u^2;
if theta > pi/2
obj = obj+0000*(pi/2-theta)^2;
end
ix = ix+params.nvarpernode;
iu = iu+params.nvarpernode;
end
obj = obj/params.NSU/params.NperSU;