-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquest2.m
68 lines (54 loc) · 2.14 KB
/
quest2.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
clear,clc;
load('tankData.mat');
load('quest2.mat');
iActTank = logical([1 1 0 1 0 0]);
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,:),0,tankPosi,tankSize,aircraftMass,oilDensity)';
tankSwitchPoint = [3000 3500 4500 5200];
tic
for i = 1:numel(t)-1
problem.objective = @(x)norm(getTotalCgFromFlow(...
tankQuantity(i,:),...
iActTank,...
x,...
0,...
tankPosi,...
tankSize,...
aircraftMass,...
oilDensity)' - aircraftIdealCg(min(i+1,7200),:));
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 = quest2changeTank(tankQuantity(i+1,:),tankInitQuantity);
end
actTank(i+1,:) = iActTank;
disp(i);
end
toc
for i = 1:numel(t)
totalCg(i,:) = getTotalCg(tankQuantity(i,:),0,tankPosi,tankSize,aircraftMass,oilDensity)';
end
totalCgError = vecnorm(totalCg - aircraftIdealCg,2,2);
maxTotalCgError = max(totalCgError);
save quest2result.mat totalCg tankFlow tankQuantity actTank totalCgError maxTotalCgError
quest2plot