-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInfContRule_w_global.m
More file actions
139 lines (121 loc) · 4.91 KB
/
InfContRule_w_global.m
File metadata and controls
139 lines (121 loc) · 4.91 KB
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
136
137
138
139
%%
% Copyright 2013 Jacek B. Krawczyk and Alastair Pharo
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
function ControlValues=InfContRule(FileName,InitialCondition,...
VariableOfInterest,LineSpec) %#ok<INUSD>
% InfContRule produces graphs of the continuous-time, continuous-state
% control rule derived from the solution computed by InfSOCSol. Each
% control rule graph holds all but one state variable constant.
global States ODM1 Minimum Maximum StateStep
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% READ PARAMETER FILE %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open parameter file.
[fid,message]=fopen([FileName,'.DPP'],'r');
% Error in opening?
if fid==-1
fprintf(['Error opening ',FileName,'.DPP\n']);
error(message);
end; % if fid==-1
% Read in program parameters.
DeltaFunction=fscanf(fid,'%s',1); %#ok<NASGU>
StageReturnFunction=fscanf(fid,'%s',1); %#ok<NASGU>
fgets(fid);
Minimum=MatRead(fid);
Maximum=MatRead(fid);
StateStepSize=MatRead(fid);
TimeStep=MatRead(fid); %#ok<NASGU>
DiscountRate=MatRead(fid); %#ok<NASGU>
InfSOCSolOptions=MatRead(fid); %#ok<NASGU>
fclose(fid);
ControlDimension=InfSOCSolOptions(1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% READ SOLUTION FILE %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open solution file.
[fid,message]=fopen([FileName,'.DPS'],'r');
% Error in opening?
if fid==-1
fprintf(['Error opening ',FileName,'.DPS\n']);
error(message);
end; % if fid==-1
% Read Optimal Decision Matrices for each control dimension.
for i=1:ControlDimension
eval(['ODM',int2str(i),'=MatRead(fid);'])
end;
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% DEFINE CONSTANTS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Error check the number of input arguments and give defaults to
% unspecified input arguments.
if nargin<4
LineSpec='r-'; %#ok<NASGU>
if nargin<3
VariableOfInterest=1;
if nargin<2
error('InfContRule must be given at least 3 input arguments.');
end;
end; % if nargin<3
end; % if nargin<4
% Determine the Dimension of the problem. This is used to compute the
% coding vector. (Though strictly its use is mere convenience).
Dimension=size(Minimum,2);
% Compute the discretization constants for the target stage.
States=round((Maximum-Minimum)./StateStepSize+1);
c=cumprod(States);
TotalStates=c(Dimension);
CodingVector=[1,c(1:Dimension-1)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% COMPUTE CONTROL PROFILES %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the State Number for the initial state modified by setting the
% variable of interest to its minimum value. All the states of interest are
% this base state plus some multiple of the variable of interest's coding
% value.
InitialCondition(VariableOfInterest)=Minimum(VariableOfInterest);
fprintf('Initial Condition: %12g\n',InitialCondition(VariableOfInterest));
BaseState=round((InitialCondition-Minimum)./StateStepSize)*...
CodingVector'+1; %#ok<NASGU>
% Compute the actual state variables corresponding to the states of
% interest and compute the control profiles in these states.
fprintf('State Step: %12g\n',StateStepSize(VariableOfInterest));
StateVect=Minimum(VariableOfInterest)+StateStepSize(VariableOfInterest)...
*(0:(States(VariableOfInterest)-1)); %#ok<NASGU>
for i=1:ControlDimension
eval(['C',int2str(i),'=ODM',int2str(i),...
'(BaseState+CodingVector(VariableOfInterest).*',...
'(0:(States(VariableOfInterest)-1)));']);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% PLOT CONTROL PROFILES %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:ControlDimension
subplot(ControlDimension,1,i);
eval(['plot(StateVect,C',int2str(i),',LineSpec);']);
grid;
ylabel(['u_',int2str(i)]);
hold on;
end; % for i=1:ControlDimension
xlabel(['x_',int2str(VariableOfInterest)]);
StateStep=StateStepSize;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% RETURN OUTPUT %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargout==1
ControlValues=zeros(TotalStates,ControlDimension);
for i=1:ControlDimension
ControlValues(:,i) = eval(['C',int2str(i),]);
end;
end; % if nargout==1