-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquest4.m
89 lines (73 loc) · 2.64 KB
/
quest4.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
clear,clc;
load('tankData.mat');
load('quest4.mat');
% problem.objective = @(x)norm(getTotalCg(...
% x,...
% 0,...
% tankPosi,...
% tankSize,...
% aircraftMass,...
% oilDensity));
% problem.x0 = tankInitQuantity;
% problem.lb = zeros(6,1);
% problem.ub = tankMaxQuantity';
% problem.Aineq = zeros(1,6) - 1;
% problem.bineq = -sum(aircraftFlow)/oilDensity;
% problem.Aeq = [];
% problem.Beq = [];
% problem.nonlcon = [];
% problem.solver = 'fmincon';
% problem.options = optimoptions('fmincon','Display','none');
%
% [tankInitQuantity,fval] = fmincon(problem);
iActTank = logical([0 0 0 1 1 1]);
engineTank = logical([0 1 1 1 1 0]);
[tankFlow,tankQuantity] = deal(zeros(size(t,1),6));
actTank = false(size(t,1),6);
tankQuantity(1,:) = tankInitQuantity;
actTank(1,:) = iActTank;
tankQuantityBound = [1 -1 0 0 0 0;0 0 0 0 -1 1];
% actTankStateflow = quest2chart('actTank',iActTank,'tankQuantity',tankInitQuantity);
totalCg = zeros(size(t,1),3);
totalCg(1,:) = getTotalCg(tankQuantity(1,:),aircraftPitch(1),tankPosi,tankSize,aircraftMass,oilDensity)';
tankSwitchPoint = [2340 3000 4423 5000];
tic
for i = 1:numel(t)-1
problem.objective = @(x)norm(getTotalCgFromFlow(...
tankQuantity(i,:),...
iActTank,...
x,...
aircraftPitch(i+1),...
tankPosi,...
tankSize,...
aircraftMass,...
oilDensity)');
problem.x0 = zeros(sum(iActTank),1) + 1/6*aircraftFlow(i);
problem.lb = zeros(sum(iActTank),1) + 0.0001;
problem.ub = tankMaxFlow(iActTank);
problem.Aineq = tankQuantityBound(:,iActTank);
problem.bineq = [tankMaxQuantity(2) - tankQuantity(i,2);tankMaxQuantity(5) - tankQuantity(i,5)];
actEngineTank = iActTank & engineTank;
problem.Aeq = double(actEngineTank(iActTank));
problem.Beq = aircraftFlow(i);
problem.nonlcon = [];
problem.solver = 'fmincon';
problem.options = optimoptions('fmincon','Display','none','Algorithm','sqp');
tankFlow(i,iActTank) = fmincon(problem)';
iTankQuantity = tankQuantity(i,:) - tankFlow(i,:)/oilDensity;
iTankQuantity([2 5]) = iTankQuantity([2 5]) + tankFlow(i,[1 6])/oilDensity;
tankQuantity(i+1,:) = iTankQuantity;
if ismember(i,tankSwitchPoint) || any(iTankQuantity(iActTank) < 0.01)
iActTank = quest4changeTank(tankQuantity(i+1,:));
end
actTank(i+1,:) = iActTank;
disp(i);
end
toc
for i = 1:numel(t)
totalCg(i,:) = getTotalCg(tankQuantity(i,:),aircraftPitch(i),tankPosi,tankSize,aircraftMass,oilDensity)';
end
totalCgError = vecnorm(totalCg,2,2);
maxTotalCgError = max(totalCgError);
save quest4result.mat totalCg tankFlow tankQuantity actTank totalCgError maxTotalCgError
quest4plot