📄 actrim.m
字号:
% Call minimization routine FMINS. A scalar cost function is contained
% in the M-file ACCOST, which also calls the constraints for coordinated
% turns and rate-of-climb of the aircraft. FMINS returns the trim values
% of alpha, beta, deltae, deltaa, deltar, and pz in the vector vtrimmed.
% ----------------------------------------------------------------------
[vtrimmed,options] = fmins('accost',vtrim,options,[],...
ctrim,rolltype,turntype,gammatype,modfun);
if options(10) == options(14) % reached maximum number of iterations
% before finding stable solution
disp('Warning: solution hasn''t converged.');
answ=input('Perform more iterations (y/n)? ','s');
if answ == 'y'
iterate = 1; % continue the minimization process
vtrim = vtrimmed;
else
iterate = 0; % no solution found, but stop iteration
end
disp(' ');
else
iterate = 0; % solution found, stop iteration
end
end
clear iterate
clc;
disp('Iteration stopped.');
disp(' ');
disp('<<< Press a key to get results >>>');
pause
% Call subroutine ACCONSTR, which contains the flight-path constraints
% once more to obtain the final values of the inputvector and states.
% --------------------------------------------------------------------
[x,u] = acconstr(vtrimmed,ctrim,rolltype,turntype,gammatype);
% Call a/c model once more, to obtain the time-derivatives of the states
% in trimmed-flight condition. By examining xdot, the user can decide if
% the trimmed-flight condition is accurate enough. If not, either the
% error tolerance of the minimization routine needs to be tightened, or
% the cost function in ACCOST.M needs to be changed.
% -----------------------------------------------------------------------
eval(modfun);
% Release compiled aircraft model now that all results are known
% --------------------------------------------------------------
feval(sysname,[],[],[],'term');
% Get rid of variables from optimization process which are not needed
% anymore.
% -------------------------------------------------------------------
clear ctrim vtrim vtrimmed options modfun
% Display the final results. x, u, and xdot contain the inputvector,
% state vector, and time-derivative of the state vector, respectively.
% --------------------------------------------------------------------
clc
disp('State vector (trimmed): ');
x
disp(' ');
disp(' ');
disp('<<< Press a key >>>');
pause
clc
disp('Input vector (trimmed): ');
u
disp(' ');
disp(' ');
disp('<<< Press a key >>>');
pause
clc
disp('Time derivative of state vector (trimmed): ');
xdot
disp(' ');
disp(' ');
disp('<<< Press a key >>>');
pause
% The integrator block in the aircraft model uses the Matlab variable
% xinco to store the initial value of x. The models from the library
% EXAMPLES use the variables uaero0 and uprop0 to store the initial values
% of the inputs to the aerodynamic and engine models, respectively.
% Usually, trimmed-flight conditions are used as initial conditions.
% Therefore, the trimmed-flight conditions will be stored in these'
% variables, to ensure compatibility with the Simulink systems. The
% trimmed-flight value of xdot = dx/dt will be stored in the variable
% xdot0, which indicates that this value of xdot is obtained for the
% INITIAL flight condition, defined by xinco, uaero0, and uprop0.
%-------------------------------------------------------------------
xinco = x; % Initial value of state vector
xdot0 = xdot; % Initial value of time-derivative of state vector
uaero0 = u(1:4); % Initial value of input vector for aerodynamic model
uprop0 = u(5:6); % Initial value of input vector for engine
% forces and moments model
clear x xdot u
% Now, a string-matrix will be created, which contains a description of
% the trimmed-flight condition. Note: the function NUM2STR2 is just a
% sligtly changed version of NUM2STR. Type HELP NUM2STR2 at the Matlab
% command line for more info.
% ---------------------------------------------------------------------
line1 = ['Trimmed-flight condition for S-function ' sysname ':'];
line1a = '';
if opt == 1
line2 = ['Steady-state wings-level flight'];
elseif opt == 2
line2 = ['Steady-state turning flight'];
elseif opt == 3
line2 = ['Steady pull-up'];
elseif opt == 4
if rolltype == 'b' % Body-axis roll
line2 = ['Steady roll along the body-axis'];
else % Stability-axis roll (default)
line2 = ['Steady roll along the stability-axis'];
end
end
line3 = '';
line4 = ['User-specified definition of aircraft and flight condition:'];
line5 = '';
line6 = ['Flap angle: deltaf = ' num2str2(deltaf,10) ' [deg] '];
line7 = ['Engine speed: n = ' num2str2(n,10) ' [RPM] '];
line8 = ['Airspeed: V = ' num2str2(V,10) ' [m/s] '];
line9 = ['Altitude: H = ' num2str2(H,10) ' [m] '];
line10 = ['Yaw-angle: psi = ' num2str2(psi,10) ' [deg] '];
if gammatype == 'f'
line11 = ['Flightpath angle: gamma = ' num2str2(gamma,10) ' [deg] '];
else
line11 = ['Manifold pressure: pz = ' num2str2(pz,10) ' ["Hg] '];
end
line12 = ['Yaw-rate: psi dot = ' num2str2(psidot,10) ' [deg/s]'];
line13 = ['Pitch-rate: theta dot = ' num2str2(thetadot,10) ' [deg/s]'];
line14 = ['Roll-rate: phi dot = ' num2str2(phidot,10) ' [deg/s]'];
if opt == 2 & turntype == 'u' % uncoordinated turn
line15 = ['Uncoordinated turn,'];
line16 = ['Roll-angle: phi = ' num2str2(phi,10) ' [deg]'];
elseif opt == 2 & turntype == 'c' % coordinated turn
line15 = ['Coordinated turn'];
line16 = '';
else % no turning flight
line15 = '';
line16 = '';
end
% Some general remarks, need to be changed manually by the user!
% --------------------------------------------------------------
line17 = 'Definitions of state and input vectors as in system BEAVER:';
line18 = 'x = [V alpha beta p q r psi theta phi xe ye H]''';
line19 = 'u = [deltae deltaa deltar deltaf n pz uw vw ww uwdot vwdot wwdot]''';
line20 = '';
% Add info about the variables in the Matlab Workspace.
% -----------------------------------------------------
line21 = 'Variables: xdot=initial state, uaero0=initial inputs to aeromodel';
line22 = 'uprop0=initial input to engine forces/moments model, xdot0=dx/dt(0)';
line23 = '';
% Add time and date to text matrix.
% ---------------------------------
t = clock;
t1 = num2str(t(4));
t2 = num2str(t(5));
t3 = num2str(t(6));
line24 = ['date: ', date, ' time: ', t1, ':', t2, ':', t3];
clear t t1 t2 t3
% Now all explanatory lines will be enhanced to 80 characters,
% in order to fit them into one string matrix.
% ------------------------------------------------------------
line1 = [line1 blanks(79-length(line1 ))];
line1a = [line1a blanks(79-length(line1a))]; % Well, nothing is perfect...
line2 = [line2 blanks(79-length(line2 ))];
line3 = [line3 blanks(79-length(line3 ))];
line4 = [line4 blanks(79-length(line4 ))];
line5 = [line5 blanks(79-length(line5 ))];
line6 = [line6 blanks(79-length(line6 ))];
line7 = [line7 blanks(79-length(line7 ))];
line8 = [line8 blanks(79-length(line8 ))];
line9 = [line9 blanks(79-length(line9 ))];
line10 = [line10 blanks(79-length(line10))];
line11 = [line11 blanks(79-length(line11))];
line12 = [line12 blanks(79-length(line12))];
line13 = [line13 blanks(79-length(line13))];
line14 = [line14 blanks(79-length(line14))];
line15 = [line15 blanks(79-length(line15))];
line16 = [line16 blanks(79-length(line16))];
line17 = [line17 blanks(79-length(line17))];
line18 = [line18 blanks(79-length(line18))];
line19 = [line19 blanks(79-length(line19))];
line20 = [line20 blanks(79-length(line20))];
line21 = [line21 blanks(79-length(line21))];
line22 = [line22 blanks(79-length(line22))];
line23 = [line23 blanks(79-length(line23))];
line24 = [line24 blanks(79-length(line24))];
% The explanatory lines are collected in the matrix trimdef.
% -------------------------------------------------------------
trimdef = [line1; line1a; line2; line3; line4; line5; line6;
line7; line8; line9; line10; line11; line12; line13;
line14; line15; line16; line17; line18; line19; line20;
line21; line22; line23; line24];
clear line1 line1a line2 line3 line4 line5 line6 line7 line8 line9
clear line10 line11 line12 line13 line14 line15 line16 line17 line18 line19
clear line20 line21 line22 line23 line24
% Get rid of definitions of flight condition which now have
% become obsolete.
% ---------------------------------------------------------
clear opt V H psi gamma phidot psidot thetadot rolltype turntype gammatype
clear deltaf n pz Hdot G phi sysname
% Results can now be saved to a file, which will be filled with the
% trim condition xinco, uaero0, uprop0, xdot0, and the explanation matrix
% trimdef.
%
% Note: files with steady-state trimmed flight condition have
% extension .TRI!
% -----------------------------------------------------------------
clc
answ = input('Save trimmed condition to file (y/n)? ','s');
if answ == 'y'
% Determine destination-directory
% -------------------------------
defdir = datadir; % Default FDC data-directory,
% see the function DATADIR.M
% Go to default directory if that directory exists (if not, start
% save-routine from the current directory).
% ---------------------------------------------------------------
currentdir = chdir;
eval(['chdir ',defdir,';'],['chdir ',currentdir,';']);
% Obtain filename and path.
% -------------------------
[filename,dirname] = uiputfile('*.tri','Save trimmed flight condition');
% Build string variable which specifies what to save in which file.
% -----------------------------------------------------------------
savecmmnd=['save ',dirname,filename,' xinco uaero0 uprop0 xdot0 trimdef'];
% Execute save command, and display file and directory.
% -----------------------------------------------------
clc;
disp(['Results saved to the file: ',dirname,filename]);
eval(savecmmnd);
disp(' ');
disp('<<< Press a key >>>');
pause
end
clear answ dirname filename savecmmnd defdir currentdir
% Display user-information.
% -------------------------
clc
disp('The results have been stored in the following variables:');
disp(' ');
disp('xinco = [V alpha beta p q r psi theta phi xe ye H]'' = state vector');
disp('xdot0 = dx/dt(0)');
disp('uaero0= [deltae deltaa deltar deltaf]'' = initial input vector for');
disp(' aerodynamic model');
disp('uprop0= [n pz]'' = initial input vector for engine model');
disp(' ');
disp('The text-matrix ''trimdef'' contains more info about the trimmed');
disp('flightcondition.');
disp(' ');
end % END OF TRIM-LOOP (SKIPPED IF OPTION 5 = QUIT IS SELECTED)
clear skip
disp(' ');
disp('Ready.');
disp(' ');
%------------------------------------------------------------------------------
% The FDC toolbox. Copyright Marc Rauw, 1994-2000.
% Last revision of this program: June 12, 2000 (SR2 fix).
%
% History of this file since September 8, 1997:
% =============================================
% September 8, 1997
% - replaced cd commands with chdir to enhance compatibility
% October 5, 1997
% - editorial changes
% - added initialization of some variables for Matlab 5 compatibility
% - changed code for defining gammatype and turntype in order to prevent
% Matlab 5 warnings
% October 21, 1997
% - added aircraft model compilation code for Matlab 5 compatibility
% - deleted confusing messages about model-initialization by Simulink
% - changed call to aircraft model for Matlab 5 compatibility
% - added code to release compiled aircraft model after finishing the
% iterations
% - added warning dialog in case model parameters are to be retrieved from
% a file by calling the function LOADER
% February 7, 1998
% - changed options definition (previously, variable opts was defined, but
% not used in practice)
% June 12, 2000
% - exchanged 'options' and '[ ]' input arguments in FMINS call (bugfix)
% - corrected line 646 (vtrim = vtrimmed i.s.o. vtrimmed = vtrim)
% - corrected gamma definition in line 323 (included pi/180 multiplication)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -