how to code transfer function

Hi, I would like to know how to code and plot this tf:
This in my attempt;
d = (e^-(t*s)/t1*s + 1)*dc;
not sure about it, any help will be greatly appreciated. Thanks much.

10 Comments

Thanks much for your reply, basically I need to include and plot this tf to this function and script flle and run this simulation. Do you know how I could include it based on your answer? Thanks a lot
function xdot = STOL_EOM(t,x)
% STOL_EOM contains the nonlinear equations of motion for a rigid airplane.
% (NOTE: The aerodynamic model is linear.)
global e1 e2 e3 rho m g S b c AR WE Power ...
GeneralizedInertia GeneralizedInertia_Inv ...
CD0 e CL_Trim CLalpha CLq CLde Cmde ...
CYbeta CYp CYr Cmalpha Cmq Clbeta Clp Clr Cnbeta Cnp Cnr ...
CYda CYdr Clda Cldr Cnda Cndr ...
de0 da0 dr0
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
alpha = atan(w/u);
beta = asin(v/norm(V));
P_dynamic = (1/2)*rho*norm(V)^2;
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
VE = V + RIB'*WE;
% Kinematic equations
XDot = RIB*VE;
ThetaDot = LIB*omega;
% Weight
W = RIB'*(m*g*e3);
% Control moments
de = de0;
da = da0;
dr = dr0;
% Components of aerodynamic force (modulo "unsteady" terms)
CL = CL_Trim + CLalpha*alpha + CLq*((q*c)/(2*norm(V))) + CLde*de;
CD = CD0 + (CL^2)/(e*pi*AR);
temp = expm(-alpha*hat(e2))*expm(beta*hat(e3))*[-CD; 0; -CL];
CX = temp(1);
CZ = temp(3);
CY = CYbeta*beta + CYp*((b*p)/(2*norm(V))) + CYr*((b*r)/(2*norm(V))) + ...
CYda*da + CYdr*dr;
%X = P_dynamic*S*CX + T0; %Constant Thrust?
Thrust = Power/norm(V);
X = P_dynamic*S*CX + Thrust; %Constant Thrust?
Y = P_dynamic*S*CY;
Z = P_dynamic*S*CZ;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Clbeta*beta + Clp*((b*p)/(2*norm(V))) + Clr*((b*r)/(2*norm(V))) + ...
Clda*da + Cldr*dr;
Cm = Cmalpha*alpha + Cmq*((c*q)/(2*norm(V))) + Cmde*de;
Cn = Cnbeta*beta + Cnp*((b*p)/(2*norm(V))) + Cnr*((b*r)/(2*norm(V))) + ...
Cnda*da + Cndr*dr;
L = P_dynamic*S*b*Cl;
M = P_dynamic*S*c*Cm;
N = P_dynamic*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = W + Force_Aero;
Moment = Moment_Aero;
% NOTE: GeneralizedInertia includes "unsteady" aerodynamic terms
% (i.e., added mass/inertia).
temp = GeneralizedInertia*[VE; omega];
LinearMomentum = temp(1:3);
AngularMomentum = temp(4:6);
clear temp
RHS = [cross(LinearMomentum,omega) + Force; ...
cross(AngularMomentum,omega) + Moment];
temp = GeneralizedInertia_Inv*RHS;
VEDot = temp(1:3);
VDot = VEDot + cross(omega,RIB'*WE);
omegaDot = temp(4:6);
clear temp
xdot = [XDot; ThetaDot; VDot; omegaDot];
clear
close all
% GenericFixedWingScript.m solves the nonlinear equations of motion for a
% fixed-wing aircraft flying in ambient wind with turbulence.
global e1 e2 e3 rho m g S b c AR Inertia
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
%a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
%u0 = Ma*a;
u0 = 13.7;
P_dynamic = (1/2)*rho*u0^2;
% Aircraft parameters (Bix3)
m = 1.202; % Mass (slugs)
W = m*g; % Weight (Newtons)
Ix = 0.2163; % Roll inertia (kg-m^2)
Iy = 0.1823; % Pitch inertia (kg-m^2)
Iz = 0.3396; % Yaw inertia (kg-m^2)
Ixz = 0.0364; % Roll/Yaw product of inertia (kg-m^2)
%Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 0.0285; % Wing area (m^2)
b = 1.54; % Wing span (m)
c = 0.188; % Wing chord (m)
AR = (b^2)/S; % Aspect ratio
CL_Trim = W/(P_dynamic*S);
CD_Trim = 0.036;
e = 0.6; %Contrived
CD0 = CD_Trim - (CL_Trim^2)/(e*pi*AR);
% Equilibrium power (constant)
Thrust_Trim = P_dynamic*S*CD_Trim;
Power = Thrust_Trim*u0;
% Longitudinal nondimensional stability and control derivatives
%Cx0 = 0.197;
%Cxu = -0.156;
%Cxw = 0.297;
%Cxw2 = 0.960;
%Cz0 = -0.179;
%Czw = -5.32;
%Czw2 = 7.02;
%Czq = -8.20;
%Czde = -0.308;
%Cm0 = 0.0134;
%Cmw = -0.240;
%Cmq = -4.49;
%Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
X0 = ones(3,1);
Theta0 = ones(3,1);
V0 = u0*e1;
omega0 = ones(3,1);
y0 = [X0; Theta0; V0; omega0];
t_final = 10;
[t,y] = ode45('STOL_EOM',[0:0.1:t_final]',y0);
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
ylabel('Position')
subplot(2,1,2)
plot(t,y(:,4:6))
ylabel('Attitude')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
ylabel('Velocity')
subplot(2,1,2)
plot(t,y(:,10:12))
ylabel('Angular Velocity')
Hi Cesar,
I assume that deltac represents the control actuator commands: de0, da0, and dr0. In the code, these are treated as global constants. In reality they are functions of time. How are de0, da0, and dr0 to be devloped as functions of time as the simulation model runs?
@Paul yes, those are actuator commands as global constants, not sure how they develop, this is another example:
any help will be greatly appreciated. thanks
function:
function xdot = GenericFixedWingEOM(t,x)
% GenericFixedWingEOM.m
%
% Nonlinear equations of motion for a rigid, fixed-wing airplane.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2
% Parse out state vector components
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
% if VonKarmanFlag == 1
%
% %%% WIND VELOCITY (1D VON KARMAN) %%%
% WX = Amp(:,1)'*cos(Omega*X(1)+Phase(:,1));
% WY = Amp(:,2)'*cos(Omega*X(1)+Phase(:,2));
% WZ = Amp(:,3)'*cos(Omega*X(1)+Phase(:,3));
% W = [WX; WY; WZ];
%
% %%% WIND GRADIENT (1D VON KARMAN) %%%
% WXX = -(Amp(:,1).*Omega)'*sin(Omega*X(1)+Phase(:,1));
% WYX = -(Amp(:,2).*Omega)'*sin(Omega*X(1)+Phase(:,2));
% WZX = -(Amp(:,3).*Omega)'*sin(Omega*X(1)+Phase(:,3));
% GradW = [WXX, 0, 0; WYX, 0, 0; WZX, 0, 0];
%
% elseif VonKarmanFlag == 2
%
% %%% WIND VELOCITY (2D VON KARMAN) %%%
% WX = Amp(:,1)'*(cos(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WY = Amp(:,2)'*(cos(Omega*X(1)+Phase(:,2)).*cos(Omega2*X(2)+Phase2(:,2)));
% WZ = Amp(:,3)'*(cos(Omega*X(1)+Phase(:,3)).*cos(Omega2*X(2)+Phase2(:,3)));;
% W = [WX; WY; WZ];
%
% %%% WIND GRADIENT (2D VON KARMAN) %%%
% WXX = -(Amp(:,1).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WYX = -(Amp(:,2).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WZX = -(Amp(:,3).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WXY = -(Amp(:,1).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% WYY = -(Amp(:,2).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% WZY = -(Amp(:,3).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% GradW = [WXX, WXY, 0; WYX, WYY, 0; WZX, WZY, 0];
%
% end
% Wind velocity in body frame, air-relative velocity, and dynamic pressure
% W = RIB'*W; % Convert wind velocity to body frame
W = [0;0;0];
Vr = V - W;
ur = Vr(1);
vr = Vr(2);
wr = Vr(3);
PDyn = (1/2)*rho*norm(Vr)^2;
% Wind gradient in body frame
%Phi = RIB'*GradW'*RIB;
% Effective angular velocity of the fluid (Note: Etkin argues for
% a different formulation of the "angular velocity of the wind")
%temp = -(Phi-Phi');
%omegaf = [-temp(2,3); temp(1,3); -temp(1,2)];
omegaf =[0;0;0];
omegar = omega - omegaf;
pr = omegar(1);
qr = omegar(2);
rr = omegar(3);
%%% AERODYNAMIC FORCES AND MOMENTS %%%
% Aerodynamic angles
%beta = asin(Vr(2)/norm(Vr));
%alpha = atan2(Vr(3),Vr(1));
% Control deflections
de = deEq;
da = 0;
%da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
dr = 0;
dT = dTEq;
% Aerodynamic force coefficients
Cx = Cx0 + Cxu*(ur/norm(Vr)) + Cxw*(wr/norm(Vr)) + Cxw2*(wr/norm(Vr))^2;
Cy = Cy0 + Cyv*(vr/norm(Vr)) + Cyp*((b*pr)/(2*norm(V))) + Cyr*((b*rr)/(2*norm(Vr))) ...
+ Cyda*da + Cydr*dr;
Cz = Cz0 + Czw*(wr/norm(Vr)) + Czw2*(wr/norm(Vr))^2 + Czq*((c*qr)/(2*norm(Vr))) ...
+ Czde*de;
Thrust = dT*Power/norm(Vr); % Constant power
X = PDyn*S*Cx + Thrust;
Y = PDyn*S*Cy;
Z = PDyn*S*Cz;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Cl0 + Clv*(vr/norm(Vr)) + Clp*((b*pr)/(2*norm(Vr))) + Clr*((b*rr)/(2*norm(Vr))) + ...
Clda*da + Cldr*dr;
Cm = Cm0 + Cmw*(wr/norm(Vr)) + Cmq*((c*qr)/(2*norm(Vr))) + Cmde*de;
Cn = Cn0 + Cnv*(vr/norm(Vr)) + Cnv2*(vr/norm(Vr))^2 + Cnp*((b*pr)/(2*norm(Vr))) + Cnr*((b*rr)/(2*norm(Vr))) + ...
Cnda*da + Cndr*dr;
L = PDyn*S*b*Cl;
M = PDyn*S*c*Cm;
N = PDyn*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = RIB'*(m*g*e3) + Force_Aero;
Moment = Moment_Aero;
%%% EQUATIONS OF MOTION %%%
%%% KINEMATIC EQUATIONS %%%
XDot = RIB*V;
ThetaDot = LIB*omega;
%%% DYNAMIC EQUATIONS %%%
VDot = (1/m)*(cross(m*V,omega) + Force);
omegaDot = inv(Inertia)*(cross(Inertia*omega,omega) + Moment);
xdot = [XDot; ThetaDot; VDot; omegaDot];
and this is the script
clear
close all
% GenericFixedWingScript.m solves the nonlinear equations of motion for a
% fixed-wing aircraft flying in ambient wind with turbulence.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
% Aircraft parameters (Bix3)
m = 1.202; % Mass (kg)
W = m*g; % Weight (Newtons)
Ix = 0.095; % Roll inertia (kg-m^2)
Iy = 215e3; % Pitch inertia (kg-m^2)
Iz = 447e3; % Yaw inertia (kg-m^2)
Ixz = 0; % Roll/Yaw product of inertia (kg-m^2)
Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 0.285; % Wing area (m^2)
b = 1.54; % Wing span (m)
c = 0.188; % Wing chord (m)
AR = (b^2)/S; % Aspect ratio
% Longitudinal nondimensional stability and control derivatives
Cx0 = 0; % Define CT0 = 0.197 below. (Cx0 = 0.197 in [Simmons et al, 2019])
Cxu = -0.156;
Cxw = 0.297;
Cxw2 = 0.960;
Cz0 = -0.179;
Czw = -5.32;
Czw2 = 7.02;
Czq = -8.20;
Czde = -0.308;
Cm0 = 0.0134;
Cmw = -0.240;
Cmq = -4.49;
Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
Cy0 = 0;
Cyv = -0.251;
Cyp = 0.170;
Cyr = 0.350;
Cyda = 0.103;
Cydr = 0.0157;
Cl0 = 0;
Clv = -0.0756;
Clp = -0.319;
Clr = 0.183;
Clda = 0.170;
Cldr = -0.0117;
Cn0 = 0;
Cnv = 0.0408;
Cnv2 = 0.0126;
Cnp = -0.242;
Cnr = -0.166;
Cnda = 0.0416;
Cndr = -0.0618;
% Solve for wings-level, constant-altitude equilibrium flight condition
VEq = 12; % Initial guess for speed (m/s)
thetaEq = 2*(pi/180); % Initial guess for pitch (rad)
deEq = -(Cm0 + Cmw*sin(thetaEq))/Cmde; % Initial guess for elevator (rad)
Power = 200; % Maximum Power (W)
CT0 = 0.197; % CT0 = Cx0 in [Simmons et al, 2019]
dTEq = (CT0*rho*S*(VEq^3))/(2*Power); % Nominal throttle setting
% Define initial state
X0 = zeros(3,1); % (m)
Theta0 = [0;thetaEq;0]; % (rad)
V0 = [VEq*cos(thetaEq);0;VEq*sin(thetaEq)]; % (m/s)
omega0 = zeros(3,1); % (rad/s)
%deEq = zeros(3,1);
y0 = [X0; Theta0; V0; omega0];
t_final = 10;
[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0);
%%%% CODE TO COMPUTE FLOW RELATIVE VELOCITY, ETC %%%%
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
legend('X','Y','Z')
ylabel('Position (m)')
subplot(2,1,2)
plot(t,y(:,4:6)*(180/pi))
legend('\phi','\theta','\psi')
ylabel('Attitude (deg)')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
legend('u','v','w')
ylabel('Velocity (m/s)')
subplot(2,1,2)
plot(t,y(:,10:12)*(180/pi))
ylabel('Angular Velocity (deg/s)')
legend('p','q','r')
those are actuator commands as global constants, not sure how they develop,
Do you want a solution that works only if the actuator commands are constant? Or do you need a more general solution? If the latter, we'll need model equations for how the actuator commands evolve as a function of time.
yes, only if they are constant. I do not really have more equations. I just would like a basic solution. Thanks
@Paul yes, only if they are constant. I do not really have more equations. I just would like a basic solution. Thanks
@Star Strider thanks much for your explanation. I'm trying plot based on your code but I get errors. "syms requires Symbolic Math Toolbox", how exactly do I have to type it? sorry, I'm not a matlab expert. Thanks much.
@Cesar Cardenas — You have to have the Symbolic Math Toolbox licensed and installed.
@Star Strider it is running and plotting now. thanks much.
My pleasure!

Sign in to comment.

 Accepted Answer

Paul
Paul on 22 Sep 2022
Edited: Paul on 22 Sep 2022
There seems to be two questions in this Question.
The first question is: How to plot the transfer function. For values of tau0 and tau1, the transfer function from delta_c to delta is
H = tf(1,[tau1 1],'InputDelay',tau0);
The concept of "plot the transfer function" is ambiguous. Typical plots of a transfer function might be
bode(H)
impulse(H)
What kind of plot is desired?
Or, if you really want to plot the actuator position in response to an actuator command (which isn't really "plot the transfer function"), then we need to define the actuator command of interest as a function of time and then use functions like step or lsim as apprropriate for the specific actuator command input of interest.
The second question is how to add those actuator dynamics to the simulation. For constant actuator command inputs, and assuming that the actuator commands are zero for t < 0 (i.e., we are injecting a step command into the actuator) the approach would be as follows. Referring to the code in this comment ...
Add three additional elements to the state vector, y. These will be the values of de, da, and dr. Make sure to initialize these values in y0
In function STOL_Eom:
Extract de, da, and dr from the state vector.
Compute:
dec = de0*(t >tau0). Note that this approach is a little loose. Technically, we'd have to integrate from t = 0 to t = tau0, stop the simulation, and then restart it. But we can try to make this a little simpler, assuming that tau0 is reasonably small as would normally be the case for an actuator.
dedot = (dec - de) / tau1
Repeat for da and dr.
Add dedot, dadot, and drdot to the end of the xdot vector
In the main code, call ode45 with with additional option of InitialStep = tau0. See ode45 to see how to use the odeset function to specify this option.
The idea here is that ode45 will hopefully take a small first step from t = 0 to t = tau0 to catch the step change in the actuator commands. This can be checked in the t vector after the simulation completes.This approach is a bit loose, but may be sufficient for your needs. If it doesn't work as well as you'd like, come back and we can address this issue a bit better.
Based on the structure of the code, I guess tau0 and tau1 would be added to the global variable list with values assigned in the main script.

18 Comments

@Paul Right thanks much for your explanation, to avoid errors, would you mind helping me add the code lines to the files? Thanks much.
Hi Cesar,
I suggest you try it yourself and then come back if having a problem. Is there something that's unclear about the suggested approachj? If so, ask away and we'll try to get it sorted out.
Hi Paul, I'll try it and get back to you if I have any problem. Thanks much.
Hi @Paul based on your explanation, I added this:
Also, I added tau_0, tau_1 to global
not sure how to do or setup the odeset function.? thanks much.
% Control deflections
de = deEq;
da = 0;
%da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
dr = 0;
dT = dTEq;
tau_1 = 1;
tau_0 = 2;
dec = de0*tau_0;
dedot = (dec - de)/tau_1;
dadot = (dec - de)/tau_1;
drdot = (dec - de)/tau_1;
xdot = [XDot; ThetaDot; VDot; omegaDo; dedot; dadot; drdot];
%in the main script I added this:
y0 = [X0; Theta0; V0; omega0; dedot; dadot; drdot];
In STOL_EOM:
at the top:
de = x(13);
da = x(14);
dr = x(15);
Comment out this, at least for now
% Control deflections
%de = deEq;
%da = 0;
%da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
%dr = 0;
%dT = dTEq;
In the middle:
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dedot = (dec - de)/tau_1;
dadot = (dec - de)/tau_1;
drdot = (dec - de)/tau_1;
At the bottom:
xdot(13) = dedot;
xdot(14) = dadot;
xdot(15) = drdot;
In the main script:
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
where *_initial are the initial values of the actuator deflections.
[t,y] = ode45('STOL_EOM',[0:0.1:t_final]',y0,odeset('InitialStep'),tau0);
Paul, thanks much. I'm trying in this file that has the same structure as the STOL_EOM and I'm getting this:
Error using odeset
Arguments must occur in name-value pairs.
Error in GenericFixedWingScript (line 110)
[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0,odeset('InitialStep'),tau0);
Thanks for your help
function xdot = GenericFixedWingEOM(t,x)
% GenericFixedWingEOM.m
%
% Nonlinear equations of motion for a rigid, fixed-wing airplane.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2
% Parse out state vector components
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
% if VonKarmanFlag == 1
%
% %%% WIND VELOCITY (1D VON KARMAN) %%%
% WX = Amp(:,1)'*cos(Omega*X(1)+Phase(:,1));
% WY = Amp(:,2)'*cos(Omega*X(1)+Phase(:,2));
% WZ = Amp(:,3)'*cos(Omega*X(1)+Phase(:,3));
% W = [WX; WY; WZ];
%
% %%% WIND GRADIENT (1D VON KARMAN) %%%
% WXX = -(Amp(:,1).*Omega)'*sin(Omega*X(1)+Phase(:,1));
% WYX = -(Amp(:,2).*Omega)'*sin(Omega*X(1)+Phase(:,2));
% WZX = -(Amp(:,3).*Omega)'*sin(Omega*X(1)+Phase(:,3));
% GradW = [WXX, 0, 0; WYX, 0, 0; WZX, 0, 0];
%
% elseif VonKarmanFlag == 2
%
% %%% WIND VELOCITY (2D VON KARMAN) %%%
% WX = Amp(:,1)'*(cos(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WY = Amp(:,2)'*(cos(Omega*X(1)+Phase(:,2)).*cos(Omega2*X(2)+Phase2(:,2)));
% WZ = Amp(:,3)'*(cos(Omega*X(1)+Phase(:,3)).*cos(Omega2*X(2)+Phase2(:,3)));;
% W = [WX; WY; WZ];
%
% %%% WIND GRADIENT (2D VON KARMAN) %%%
% WXX = -(Amp(:,1).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WYX = -(Amp(:,2).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WZX = -(Amp(:,3).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WXY = -(Amp(:,1).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% WYY = -(Amp(:,2).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% WZY = -(Amp(:,3).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% GradW = [WXX, WXY, 0; WYX, WYY, 0; WZX, WZY, 0];
%
% end
% Wind velocity in body frame, air-relative velocity, and dynamic pressure
% W = RIB'*W; % Convert wind velocity to body frame
W = [0;0;0];
Vr = V - W;
ur = Vr(1);
vr = Vr(2);
wr = Vr(3);
PDyn = (1/2)*rho*norm(Vr)^2;
% Wind gradient in body frame
%Phi = RIB'*GradW'*RIB;
% Effective angular velocity of the fluid (Note: Etkin argues for
% a different formulation of the "angular velocity of the wind")
%temp = -(Phi-Phi');
%omegaf = [-temp(2,3); temp(1,3); -temp(1,2)];
omegaf =[0;0;0];
omegar = omega - omegaf;
pr = omegar(1);
qr = omegar(2);
rr = omegar(3);
%%% AERODYNAMIC FORCES AND MOMENTS %%%
% Aerodynamic angles
%beta = asin(Vr(2)/norm(Vr));
%alpha = atan2(Vr(3),Vr(1));
% Control deflections
% %de = deEq;
% da = 0;
% %da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
% dr = 0;
% dT = dTEq;
de = x(13);
da = x(14);
dr = x(15);
tau0 = 1;
tau_1 = 2;
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dedot = (dec - de)/tau_1;
dadot = (dec - de)/tau_1;
drdot = (dec - de)/tau_1;
xdot(13) = dedot;
xdot(14) = dadot;
xdot(15) = drdot;
% Aerodynamic force coefficients
Cx = Cx0 + Cxu*(ur/norm(Vr)) + Cxw*(wr/norm(Vr)) + Cxw2*(wr/norm(Vr))^2;
Cy = Cy0 + Cyv*(vr/norm(Vr)) + Cyp*((b*pr)/(2*norm(V))) + Cyr*((b*rr)/(2*norm(Vr))) ...
+ Cyda*da + Cydr*dr;
Cz = Cz0 + Czw*(wr/norm(Vr)) + Czw2*(wr/norm(Vr))^2 + Czq*((c*qr)/(2*norm(Vr))) ...
+ Czde*de;
Thrust = dT*Power/norm(Vr); % Constant power
X = PDyn*S*Cx + Thrust;
Y = PDyn*S*Cy;
Z = PDyn*S*Cz;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Cl0 + Clv*(vr/norm(Vr)) + Clp*((b*pr)/(2*norm(Vr))) + Clr*((b*rr)/(2*norm(Vr))) + ...
Clda*da + Cldr*dr;
Cm = Cm0 + Cmw*(wr/norm(Vr)) + Cmq*((c*qr)/(2*norm(Vr))) + Cmde*de;
Cn = Cn0 + Cnv*(vr/norm(Vr)) + Cnv2*(vr/norm(Vr))^2 + Cnp*((b*pr)/(2*norm(Vr))) + Cnr*((b*rr)/(2*norm(Vr))) + ...
Cnda*da + Cndr*dr;
L = PDyn*S*b*Cl;
M = PDyn*S*c*Cm;
N = PDyn*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = RIB'*(m*g*e3) + Force_Aero;
Moment = Moment_Aero;
%%% EQUATIONS OF MOTION %%%
%%% KINEMATIC EQUATIONS %%%
XDot = RIB*V;
ThetaDot = LIB*omega;
%%% DYNAMIC EQUATIONS %%%
VDot = (1/m)*(cross(m*V,omega) + Force);
omegaDot = inv(Inertia)*(cross(Inertia*omega,omega) + Moment);
xdot = [XDot; ThetaDot; VDot; omegaDot];
clear
close all
% GenericFixedWingScript.m solves the nonlinear equations of motion for a
% fixed-wing aircraft flying in ambient wind with turbulence.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
% Aircraft parameters (Bix3)
m = 1.202; % Mass (kg)
W = m*g; % Weight (Newtons)
Ix = 0.095; % Roll inertia (kg-m^2)
Iy = 215e3; % Pitch inertia (kg-m^2)
Iz = 447e3; % Yaw inertia (kg-m^2)
Ixz = 0; % Roll/Yaw product of inertia (kg-m^2)
Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 0.285; % Wing area (m^2)
b = 1.54; % Wing span (m)
c = 0.188; % Wing chord (m)
AR = (b^2)/S; % Aspect ratio
% Longitudinal nondimensional stability and control derivatives
Cx0 = 0; % Define CT0 = 0.197 below. (Cx0 = 0.197 in [Simmons et al, 2019])
Cxu = -0.156;
Cxw = 0.297;
Cxw2 = 0.960;
Cz0 = -0.179;
Czw = -5.32;
Czw2 = 7.02;
Czq = -8.20;
Czde = -0.308;
Cm0 = 0.0134;
Cmw = -0.240;
Cmq = -4.49;
Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
Cy0 = 0;
Cyv = -0.251;
Cyp = 0.170;
Cyr = 0.350;
Cyda = 0.103;
Cydr = 0.0157;
Cl0 = 0;
Clv = -0.0756;
Clp = -0.319;
Clr = 0.183;
Clda = 0.170;
Cldr = -0.0117;
Cn0 = 0;
Cnv = 0.0408;
Cnv2 = 0.0126;
Cnp = -0.242;
Cnr = -0.166;
Cnda = 0.0416;
Cndr = -0.0618;
% Solve for wings-level, constant-altitude equilibrium flight condition
VEq = 12; % Initial guess for speed (m/s)
thetaEq = 2*(pi/180); % Initial guess for pitch (rad)
deEq = -(Cm0 + Cmw*sin(thetaEq))/Cmde; % Initial guess for elevator (rad)
Power = 200; % Maximum Power (W)
CT0 = 0.197; % CT0 = Cx0 in [Simmons et al, 2019]
dTEq = (CT0*rho*S*(VEq^3))/(2*Power); % Nominal throttle setting
% Define initial state
X0 = zeros(3,1); % (m)
Theta0 = [0;thetaEq;0]; % (rad)
V0 = [VEq*cos(thetaEq);0;VEq*sin(thetaEq)]; % (m/s)
omega0 = zeros(3,1); % (rad/s)
de_initial = zeros(3,1);
da_initial = zeros(3,1);
dr_initial = zeros(3,1);
% y0 = [X0; Theta0; V0; omega0];
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
t_final = 10;
% [t,y] = ode45('GenericFixedWingEOM',(0:0.1:t_final)',y0);
[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0,odeset('InitialStep'),tau0);
%%%% CODE TO COMPUTE FLOW RELATIVE VELOCITY, ETC %%%%
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
legend('X','Y','Z')
ylabel('Position (m)')
subplot(2,1,2)
plot(t,y(:,4:6)*(180/pi))
legend('\phi','\theta','\psi')
ylabel('Attitude (deg)')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
legend('u','v','w')
ylabel('Velocity (m/s)')
subplot(2,1,2)
plot(t,y(:,10:12)*(180/pi))
ylabel('Angular Velocity (deg/s)')
legend('p','q','r')
I had typos in my response. Corrected below. I also noticed some other issues, corrected below. Let's see if it at least runs now.
Nope, still an error. What is hat(e3), etc. in the computation of RIB? Is that just supposed to be e3?
Also, I don't think de0, da0, or dr0 are defined anywhere.
% GenericFixedWingScript.m solves the nonlinear equations of motion for a
% fixed-wing aircraft flying in ambient wind with turbulence.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 ...
tau0 tau_1
tau0 and tau_1 added to the global list
tau0 = 1; % actuator delay
tau_1 = 2; % time constant
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
% Aircraft parameters (Bix3)
m = 1.202; % Mass (kg)
W = m*g; % Weight (Newtons)
Ix = 0.095; % Roll inertia (kg-m^2)
Iy = 215e3; % Pitch inertia (kg-m^2)
Iz = 447e3; % Yaw inertia (kg-m^2)
Ixz = 0; % Roll/Yaw product of inertia (kg-m^2)
Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 0.285; % Wing area (m^2)
b = 1.54; % Wing span (m)
c = 0.188; % Wing chord (m)
AR = (b^2)/S; % Aspect ratio
% Longitudinal nondimensional stability and control derivatives
Cx0 = 0; % Define CT0 = 0.197 below. (Cx0 = 0.197 in [Simmons et al, 2019])
Cxu = -0.156;
Cxw = 0.297;
Cxw2 = 0.960;
Cz0 = -0.179;
Czw = -5.32;
Czw2 = 7.02;
Czq = -8.20;
Czde = -0.308;
Cm0 = 0.0134;
Cmw = -0.240;
Cmq = -4.49;
Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
Cy0 = 0;
Cyv = -0.251;
Cyp = 0.170;
Cyr = 0.350;
Cyda = 0.103;
Cydr = 0.0157;
Cl0 = 0;
Clv = -0.0756;
Clp = -0.319;
Clr = 0.183;
Clda = 0.170;
Cldr = -0.0117;
Cn0 = 0;
Cnv = 0.0408;
Cnv2 = 0.0126;
Cnp = -0.242;
Cnr = -0.166;
Cnda = 0.0416;
Cndr = -0.0618;
% Solve for wings-level, constant-altitude equilibrium flight condition
VEq = 12; % Initial guess for speed (m/s)
thetaEq = 2*(pi/180); % Initial guess for pitch (rad)
deEq = -(Cm0 + Cmw*sin(thetaEq))/Cmde; % Initial guess for elevator (rad)
Power = 200; % Maximum Power (W)
CT0 = 0.197; % CT0 = Cx0 in [Simmons et al, 2019]
dTEq = (CT0*rho*S*(VEq^3))/(2*Power); % Nominal throttle setting
% Define initial state
X0 = zeros(3,1); % (m)
Theta0 = [0;thetaEq;0]; % (rad)
V0 = [VEq*cos(thetaEq);0;VEq*sin(thetaEq)]; % (m/s)
omega0 = zeros(3,1); % (rad/s)
de, da, and dr are scalars and should be initialized as such
%de_initial = zeros(3,1);
%da_initial = zeros(3,1);
%dr_initial = zeros(3,1);
de_initial = 0;
da_initial = 0;
dr_initial = 0;
% y0 = [X0; Theta0; V0; omega0];
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
t_final = 10;
% [t,y] = ode45('GenericFixedWingEOM',(0:0.1:t_final)',y0);
Fix the typo in the call to odeset. The odefun argument should be a function handle.
%[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0,odeset('InitialStep'),tau0);
[t,y] = ode45(@GenericFixedWingEOM,[0:0.1:t_final]',y0,odeset('InitialStep',tau0));
Unrecognized function or variable 'hat'.

Error in solution>GenericFixedWingEOM (line 147)
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
%%%% CODE TO COMPUTE FLOW RELATIVE VELOCITY, ETC %%%%
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
legend('X','Y','Z')
ylabel('Position (m)')
subplot(2,1,2)
plot(t,y(:,4:6)*(180/pi))
legend('\phi','\theta','\psi')
ylabel('Attitude (deg)')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
legend('u','v','w')
ylabel('Velocity (m/s)')
subplot(2,1,2)
plot(t,y(:,10:12)*(180/pi))
ylabel('Angular Velocity (deg/s)')
legend('p','q','r')
function xdot = GenericFixedWingEOM(t,x)
% GenericFixedWingEOM.m
%
% Nonlinear equations of motion for a rigid, fixed-wing airplane.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 ...
tau0 tau_1 % added
% Parse out state vector components
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
% if VonKarmanFlag == 1
%
% %%% WIND VELOCITY (1D VON KARMAN) %%%
% WX = Amp(:,1)'*cos(Omega*X(1)+Phase(:,1));
% WY = Amp(:,2)'*cos(Omega*X(1)+Phase(:,2));
% WZ = Amp(:,3)'*cos(Omega*X(1)+Phase(:,3));
% W = [WX; WY; WZ];
%
% %%% WIND GRADIENT (1D VON KARMAN) %%%
% WXX = -(Amp(:,1).*Omega)'*sin(Omega*X(1)+Phase(:,1));
% WYX = -(Amp(:,2).*Omega)'*sin(Omega*X(1)+Phase(:,2));
% WZX = -(Amp(:,3).*Omega)'*sin(Omega*X(1)+Phase(:,3));
% GradW = [WXX, 0, 0; WYX, 0, 0; WZX, 0, 0];
%
% elseif VonKarmanFlag == 2
%
% %%% WIND VELOCITY (2D VON KARMAN) %%%
% WX = Amp(:,1)'*(cos(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WY = Amp(:,2)'*(cos(Omega*X(1)+Phase(:,2)).*cos(Omega2*X(2)+Phase2(:,2)));
% WZ = Amp(:,3)'*(cos(Omega*X(1)+Phase(:,3)).*cos(Omega2*X(2)+Phase2(:,3)));;
% W = [WX; WY; WZ];
%
% %%% WIND GRADIENT (2D VON KARMAN) %%%
% WXX = -(Amp(:,1).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WYX = -(Amp(:,2).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WZX = -(Amp(:,3).*Omega)'*(sin(Omega*X(1)+Phase(:,1)).*cos(Omega2*X(2)+Phase2(:,1)));
% WXY = -(Amp(:,1).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% WYY = -(Amp(:,2).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% WZY = -(Amp(:,3).*Omega2)'*(cos(Omega*X(1)+Phase(:,1)).*sin(Omega2*X(2)+Phase2(:,1)));
% GradW = [WXX, WXY, 0; WYX, WYY, 0; WZX, WZY, 0];
%
% end
% Wind velocity in body frame, air-relative velocity, and dynamic pressure
% W = RIB'*W; % Convert wind velocity to body frame
W = [0;0;0];
Vr = V - W;
ur = Vr(1);
vr = Vr(2);
wr = Vr(3);
PDyn = (1/2)*rho*norm(Vr)^2;
% Wind gradient in body frame
%Phi = RIB'*GradW'*RIB;
% Effective angular velocity of the fluid (Note: Etkin argues for
% a different formulation of the "angular velocity of the wind")
%temp = -(Phi-Phi');
%omegaf = [-temp(2,3); temp(1,3); -temp(1,2)];
omegaf =[0;0;0];
omegar = omega - omegaf;
pr = omegar(1);
qr = omegar(2);
rr = omegar(3);
%%% AERODYNAMIC FORCES AND MOMENTS %%%
% Aerodynamic angles
%beta = asin(Vr(2)/norm(Vr));
%alpha = atan2(Vr(3),Vr(1));
% Control deflections
% %de = deEq;
% da = 0;
% %da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
% dr = 0;
% dT = dTEq;
Why not pull these out of x at the top of the function with the other state variables?
de = x(13);
da = x(14);
dr = x(15);
Defined in the global script
%tau0 = 1;
%tau_1 = 2;
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dadot and drdot equations are incorrect. Looks like I had these incorrect in my comment above (copy/paste error, obviously).
dedot = (dec - de)/tau_1;
%dadot = (dec - de)/tau_1;
%drdot = (dec - de)/tau_1;
dadot = (dac - da)/tau_1;
drdot = (drc - dr)/tau_1;
The xdot vector has to be constructed below.
%xdot(13) = dedot;
%xdot(14) = dadot;
%xdot(15) = drdot;
% Aerodynamic force coefficients
Cx = Cx0 + Cxu*(ur/norm(Vr)) + Cxw*(wr/norm(Vr)) + Cxw2*(wr/norm(Vr))^2;
Cy = Cy0 + Cyv*(vr/norm(Vr)) + Cyp*((b*pr)/(2*norm(V))) + Cyr*((b*rr)/(2*norm(Vr))) ...
+ Cyda*da + Cydr*dr;
Cz = Cz0 + Czw*(wr/norm(Vr)) + Czw2*(wr/norm(Vr))^2 + Czq*((c*qr)/(2*norm(Vr))) ...
+ Czde*de;
Thrust = dT*Power/norm(Vr); % Constant power
X = PDyn*S*Cx + Thrust;
Y = PDyn*S*Cy;
Z = PDyn*S*Cz;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Cl0 + Clv*(vr/norm(Vr)) + Clp*((b*pr)/(2*norm(Vr))) + Clr*((b*rr)/(2*norm(Vr))) + ...
Clda*da + Cldr*dr;
Cm = Cm0 + Cmw*(wr/norm(Vr)) + Cmq*((c*qr)/(2*norm(Vr))) + Cmde*de;
Cn = Cn0 + Cnv*(vr/norm(Vr)) + Cnv2*(vr/norm(Vr))^2 + Cnp*((b*pr)/(2*norm(Vr))) + Cnr*((b*rr)/(2*norm(Vr))) + ...
Cnda*da + Cndr*dr;
L = PDyn*S*b*Cl;
M = PDyn*S*c*Cm;
N = PDyn*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = RIB'*(m*g*e3) + Force_Aero;
Moment = Moment_Aero;
%%% EQUATIONS OF MOTION %%%
%%% KINEMATIC EQUATIONS %%%
XDot = RIB*V;
ThetaDot = LIB*omega;
%%% DYNAMIC EQUATIONS %%%
VDot = (1/m)*(cross(m*V,omega) + Force);
omegaDot = inv(Inertia)*(cross(Inertia*omega,omega) + Moment);
Add the actuator rates to the xdot vector
xdot = [XDot; ThetaDot; VDot; omegaDot; dedot; dadot; drdot];
end
Hi, I have the hat file, I'll try and let you know. Thanks much.
Hi Paul, would you mind clarifying the right order to type the code lines? I'm getting errors. Thanks much.
Hi Paul, I elimiated some things that I do not need to symplify it and better visualization. I'm missing some lines based on your previous comments which not sure exactly where to add. Thanks much.
this is the hat file:
function xhat = hat(x)
xhat = [0,-x(3),x(2);x(3),0,-x(1);-x(2),x(1),0];
function xdot = GenericFixedWingEOM(t,x)
% GenericFixedWingEOM.m
%
% Nonlinear equations of motion for a rigid, fixed-wing airplane.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2
tau0 tau_1 % added
% Parse out state vector components
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
W = [0;0;0];
Vr = V - W;
ur = Vr(1);
vr = Vr(2);
wr = Vr(3);
PDyn = (1/2)*rho*norm(Vr)^2;
omegaf =[0;0;0];
omegar = omega - omegaf;
pr = omegar(1);
qr = omegar(2);
rr = omegar(3);
%%% AERODYNAMIC FORCES AND MOMENTS %%%
% Aerodynamic angles
%beta = asin(Vr(2)/norm(Vr));
%alpha = atan2(Vr(3),Vr(1));
% Control deflections
%de = deEq;
%da = 0;
%da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
%dr = 0;
%dT = dTEq;
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dedot = (dec - de)/tau_1;
%dadot = (dec - de)/tau_1;
%drdot = (dec - de)/tau_1;
dadot = (dac - da)/tau_1;
drdot = (drc - dr)/tau_1;
% Aerodynamic force coefficients
Cx = Cx0 + Cxu*(ur/norm(Vr)) + Cxw*(wr/norm(Vr)) + Cxw2*(wr/norm(Vr))^2;
Cy = Cy0 + Cyv*(vr/norm(Vr)) + Cyp*((b*pr)/(2*norm(V))) + Cyr*((b*rr)/(2*norm(Vr))) ...
+ Cyda*da + Cydr*dr;
Cz = Cz0 + Czw*(wr/norm(Vr)) + Czw2*(wr/norm(Vr))^2 + Czq*((c*qr)/(2*norm(Vr))) ...
+ Czde*de;
Thrust = dT*Power/norm(Vr); % Constant power
X = PDyn*S*Cx + Thrust;
Y = PDyn*S*Cy;
Z = PDyn*S*Cz;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Cl0 + Clv*(vr/norm(Vr)) + Clp*((b*pr)/(2*norm(Vr))) + Clr*((b*rr)/(2*norm(Vr))) + ...
Clda*da + Cldr*dr;
Cm = Cm0 + Cmw*(wr/norm(Vr)) + Cmq*((c*qr)/(2*norm(Vr))) + Cmde*de;
Cn = Cn0 + Cnv*(vr/norm(Vr)) + Cnv2*(vr/norm(Vr))^2 + Cnp*((b*pr)/(2*norm(Vr))) + Cnr*((b*rr)/(2*norm(Vr))) + ...
Cnda*da + Cndr*dr;
L = PDyn*S*b*Cl;
M = PDyn*S*c*Cm;
N = PDyn*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = RIB'*(m*g*e3) + Force_Aero;
Moment = Moment_Aero;
%%% EQUATIONS OF MOTION %%%
%%% KINEMATIC EQUATIONS %%%
XDot = RIB*V;
ThetaDot = LIB*omega;
%%% DYNAMIC EQUATIONS %%%
VDot = (1/m)*(cross(m*V,omega) + Force);
omegaDot = inv(Inertia)*(cross(Inertia*omega,omega) + Moment);
xdot = [XDot; ThetaDot; VDot; omegaDot];
%xdot = [XDot; ThetaDot; VDot; omegaDot; dedot; dadot; drdot];
clear
close all
% GenericFixedWingScript.m solves the nonlinear equations of motion for a
% fixed-wing aircraft flying in ambient wind with turbulence.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 tau0 tau_1
tau0 = 1; % actuator delay
tau_1 = 2; % time constant
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
% Aircraft parameters (Bix3)
m = 1.202; % Mass (kg)
W = m*g; % Weight (Newtons)
Ix = 0.095; % Roll inertia (kg-m^2)
Iy = 215e3; % Pitch inertia (kg-m^2)
Iz = 447e3; % Yaw inertia (kg-m^2)
Ixz = 0; % Roll/Yaw product of inertia (kg-m^2)
Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 0.285; % Wing area (m^2)
b = 1.54; % Wing span (m)
c = 0.188; % Wing chord (m)
AR = (b^2)/S; % Aspect ratio
% Longitudinal nondimensional stability and control derivatives
Cx0 = 0; % Define CT0 = 0.197 below. (Cx0 = 0.197 in [Simmons et al, 2019])
Cxu = -0.156;
Cxw = 0.297;
Cxw2 = 0.960;
Cz0 = -0.179;
Czw = -5.32;
Czw2 = 7.02;
Czq = -8.20;
Czde = -0.308;
Cm0 = 0.0134;
Cmw = -0.240;
Cmq = -4.49;
Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
Cy0 = 0;
Cyv = -0.251;
Cyp = 0.170;
Cyr = 0.350;
Cyda = 0.103;
Cydr = 0.0157;
Cl0 = 0;
Clv = -0.0756;
Clp = -0.319;
Clr = 0.183;
Clda = 0.170;
Cldr = -0.0117;
Cn0 = 0;
Cnv = 0.0408;
Cnv2 = 0.0126;
Cnp = -0.242;
Cnr = -0.166;
Cnda = 0.0416;
Cndr = -0.0618;
% Solve for wings-level, constant-altitude equilibrium flight condition
VEq = 13.7; % Initial guess for speed (m/s)
thetaEq = 2*(pi/180); % Initial guess for pitch (rad)
deEq = -(Cm0 + Cmw*sin(thetaEq))/Cmde; % Initial guess for elevator (rad)
Power = 200; % Maximum Power (W)
CT0 = 0.197; % CT0 = Cx0 in [Simmons et al, 2019]
dTEq = (CT0*rho*S*(VEq^3))/(2*Power); % Nominal throttle setting
% Define initial state
X0 = zeros(3,1); % (m)
Theta0 = [0;thetaEq;0]; % (rad)
V0 = [VEq*cos(thetaEq);0;VEq*sin(thetaEq)]; % (m/s)
omega0 = zeros(3,1); % (rad/s)
de_initial = 0;
da_initial = 0;
dr_initial = 0;
%y0 = [X0; Theta0; V0; omega0];
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
t_final = 10;
[t,y] = ode45(@GenericFixedWingEOM,[0:0.1:t_final]',y0,odeset('InitialStep',tau0));
%t_final = 30;
%[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0);
%%%% CODE TO COMPUTE FLOW RELATIVE VELOCITY, ETC %%%%
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
legend('X','Y','Z')
ylabel('Position (m)')
subplot(2,1,2)
plot(t,y(:,4:6)*(180/pi))
legend('\phi','\theta','\psi')
ylabel('Attitude (deg)')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
legend('u','v','w')
ylabel('Velocity (m/s)')
subplot(2,1,2)
plot(t,y(:,10:12)*(180/pi))
ylabel('Angular Velocity (deg/s)')
legend('p','q','r')
Hi Paul, just a final question, wondering if you know how to include a doublet in this code? Thanks much for all your help
It looks like you didn't actually use all of code changes I provided above. They have been reimplemented below, along with adding the actuation commands to the script. The code below runs, as demonstrated, for constant actuator commands.
You can try to implement a doublet actuator command as follows:
a. Remove de0, da0, dr0 from the script and global variable list
b. Create a function with the signature
dc = doublet(t, ...) that computes the acutator command as a function of time.
c. In GenericFixedWingEOM, prior the computation of the actuator derviatives:
de0 = doublet(t-tau0, ....). Do the same for da0 and dr0.
This approach might work ok for your needs if the width of the doublet steps are fairly wide. Again, plot the achieved actuator deflections to see if they are what they should be.
Good luck with your project.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 tau0 tau_1 ...
de0 da0 dr0 % added
Define small constant actuator deflection commands for testing
de0 = 0.001;
da0 = 0.0005;
dr0 = 0.0002;
tau0 = 1; % actuator delay
tau_1 = 2; % time constant
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
% Aircraft parameters (Bix3)
m = 1.202; % Mass (kg)
W = m*g; % Weight (Newtons)
Ix = 0.095; % Roll inertia (kg-m^2)
Iy = 215e3; % Pitch inertia (kg-m^2)
Iz = 447e3; % Yaw inertia (kg-m^2)
Ixz = 0; % Roll/Yaw product of inertia (kg-m^2)
Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 0.285; % Wing area (m^2)
b = 1.54; % Wing span (m)
c = 0.188; % Wing chord (m)
AR = (b^2)/S; % Aspect ratio
% Longitudinal nondimensional stability and control derivatives
Cx0 = 0; % Define CT0 = 0.197 below. (Cx0 = 0.197 in [Simmons et al, 2019])
Cxu = -0.156;
Cxw = 0.297;
Cxw2 = 0.960;
Cz0 = -0.179;
Czw = -5.32;
Czw2 = 7.02;
Czq = -8.20;
Czde = -0.308;
Cm0 = 0.0134;
Cmw = -0.240;
Cmq = -4.49;
Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
Cy0 = 0;
Cyv = -0.251;
Cyp = 0.170;
Cyr = 0.350;
Cyda = 0.103;
Cydr = 0.0157;
Cl0 = 0;
Clv = -0.0756;
Clp = -0.319;
Clr = 0.183;
Clda = 0.170;
Cldr = -0.0117;
Cn0 = 0;
Cnv = 0.0408;
Cnv2 = 0.0126;
Cnp = -0.242;
Cnr = -0.166;
Cnda = 0.0416;
Cndr = -0.0618;
% Solve for wings-level, constant-altitude equilibrium flight condition
VEq = 13.7; % Initial guess for speed (m/s)
thetaEq = 2*(pi/180); % Initial guess for pitch (rad)
deEq = -(Cm0 + Cmw*sin(thetaEq))/Cmde; % Initial guess for elevator (rad)
Power = 200; % Maximum Power (W)
CT0 = 0.197; % CT0 = Cx0 in [Simmons et al, 2019]
dTEq = (CT0*rho*S*(VEq^3))/(2*Power); % Nominal throttle setting
% Define initial state
X0 = zeros(3,1); % (m)
Theta0 = [0;thetaEq;0]; % (rad)
V0 = [VEq*cos(thetaEq);0;VEq*sin(thetaEq)]; % (m/s)
omega0 = zeros(3,1); % (rad/s)
de_initial = 0;
da_initial = 0;
dr_initial = 0;
%y0 = [X0; Theta0; V0; omega0];
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
t_final = 10;
[t,y] = ode45(@GenericFixedWingEOM,[0:0.1:t_final]',y0,odeset('InitialStep',tau0));
%t_final = 30;
%[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0);
%%%% CODE TO COMPUTE FLOW RELATIVE VELOCITY, ETC %%%%
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
legend('X','Y','Z')
ylabel('Position (m)')
subplot(2,1,2)
plot(t,y(:,4:6)*(180/pi))
legend('\phi','\theta','\psi')
ylabel('Attitude (deg)')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
legend('u','v','w')
ylabel('Velocity (m/s)')
subplot(2,1,2)
plot(t,y(:,10:12)*(180/pi))
ylabel('Angular Velocity (deg/s)')
legend('p','q','r')
See if actuator reponse to constant input is delaybe by tau0 = 1. It is.
figure
plot(t,y(:,13:15))
legend('de','da','dr')
function xdot = GenericFixedWingEOM(t,x)
% GenericFixedWingEOM.m
%
% Nonlinear equations of motion for a rigid, fixed-wing airplane.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 ...
tau0 tau_1 ...
de0 da0 dr0 % added
% Parse out state vector components
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
Extract actuator deflections from the state vector.
de = x(13);
da = x(14);
dr = x(15);
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
W = [0;0;0];
Vr = V - W;
ur = Vr(1);
vr = Vr(2);
wr = Vr(3);
PDyn = (1/2)*rho*norm(Vr)^2;
omegaf =[0;0;0];
omegar = omega - omegaf;
pr = omegar(1);
qr = omegar(2);
rr = omegar(3);
%%% AERODYNAMIC FORCES AND MOMENTS %%%
% Aerodynamic angles
%beta = asin(Vr(2)/norm(Vr));
%alpha = atan2(Vr(3),Vr(1));
% Control deflections
%de = deEq;
%da = 0;
%da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
%dr = 0;
Need to assign to dT
dT = dTEq;
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dedot = (dec - de)/tau_1;
%dadot = (dec - de)/tau_1;
%drdot = (dec - de)/tau_1;
dadot = (dac - da)/tau_1;
drdot = (drc - dr)/tau_1;
% Aerodynamic force coefficients
Cx = Cx0 + Cxu*(ur/norm(Vr)) + Cxw*(wr/norm(Vr)) + Cxw2*(wr/norm(Vr))^2;
Cy = Cy0 + Cyv*(vr/norm(Vr)) + Cyp*((b*pr)/(2*norm(V))) + Cyr*((b*rr)/(2*norm(Vr))) ...
+ Cyda*da + Cydr*dr;
Cz = Cz0 + Czw*(wr/norm(Vr)) + Czw2*(wr/norm(Vr))^2 + Czq*((c*qr)/(2*norm(Vr))) ...
+ Czde*de;
Thrust = dT*Power/norm(Vr); % Constant power
X = PDyn*S*Cx + Thrust;
Y = PDyn*S*Cy;
Z = PDyn*S*Cz;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Cl0 + Clv*(vr/norm(Vr)) + Clp*((b*pr)/(2*norm(Vr))) + Clr*((b*rr)/(2*norm(Vr))) + ...
Clda*da + Cldr*dr;
Cm = Cm0 + Cmw*(wr/norm(Vr)) + Cmq*((c*qr)/(2*norm(Vr))) + Cmde*de;
Cn = Cn0 + Cnv*(vr/norm(Vr)) + Cnv2*(vr/norm(Vr))^2 + Cnp*((b*pr)/(2*norm(Vr))) + Cnr*((b*rr)/(2*norm(Vr))) + ...
Cnda*da + Cndr*dr;
L = PDyn*S*b*Cl;
M = PDyn*S*c*Cm;
N = PDyn*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = RIB'*(m*g*e3) + Force_Aero;
Moment = Moment_Aero;
%%% EQUATIONS OF MOTION %%%
%%% KINEMATIC EQUATIONS %%%
XDot = RIB*V;
ThetaDot = LIB*omega;
%%% DYNAMIC EQUATIONS %%%
VDot = (1/m)*(cross(m*V,omega) + Force);
omegaDot = inv(Inertia)*(cross(Inertia*omega,omega) + Moment);
%xdot = [XDot; ThetaDot; VDot; omegaDot];
Include actuator rates in state derivative vector
xdot = [XDot; ThetaDot; VDot; omegaDot; dedot; dadot; drdot];
end
function xhat = hat(x)
xhat = [0,-x(3),x(2);x(3),0,-x(1);-x(2),x(1),0];
end
Hi Paul, thank you so much for all your help. It looks nice. I've been carefully checking every line and trying to run it, unfortunately I'm still getting errors.
Addtionally, when you define:
de = x(13);
da = x(14);
dr = x(15);
what command or instruction are you giving? Thanks much.
Error using odearguments
GENERICFIXEDWINGEOM returns a vector of length 12, but the length of initial conditions vector is 15. The vector returned
by GENERICFIXEDWINGEOM and the initial conditions vector must have the same number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in GenericFixedWingScript1 (line 113)
[t,y] = ode45(@GenericFixedWingEOM,[0:0.1:t_final]',y0,odeset('InitialStep',tau0));
Why not just copy and paste the code I posted? It runs without error.
I don't understand your questiion about de, da, and dr. They are now state variables in the state vector x and are pulled out of x to use inc ompuations in the exact same way you were already extracting omega, for example.
The error message sounds like it results because you didn't add the variables dedot, dadot, and drdot to the xdot vector at the bottom of GenericFixedWingEOM, as I've now shown twice, and for some reason you commented out in the code you posted in this comment.
Yes, I just copied and pasted it. I think the problem is the initial conditions. (please ignore my previous message, it is bit late and I was mistaken). What I have is this:
in the GenericFixedWingEOM:
xdot = [XDot; ThetaDot; VDot; omegaDot; dedot; dadot; drdot];
and in the script:
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
d'_initials are all zero, which is right, but I get an error regarding vector lengths and can not find it. sorry about that,
Hi Paul, now I understand the varaibles in the state vector. Finally, why these multiplications and substractions here? Sorry, I'm not a matlab expert and trying to undsertand a bit better these modifications. Thank you so much.
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dedot = (dec - de)/tau_1;
%dadot = (dec - de)/tau_1;
%drdot = (dec - de)/tau_1;
dadot = (dac - da)/tau_1;
drdot = (drc - dr)/tau_1;
Hi Paul, sorry for bothering so much, This is what I'm trying to do for the doublet based on your comment
Not really sure if it is the right approach. Thanks for your feeddback in advance.
function xdot = GenericFixedWingEOM(t,x)
% GenericFixedWingEOM.m
%
% Nonlinear equations of motion for a rigid, fixed-wing airplane.
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 ...
tau0 tau_1 ...
%removed de0 da0 dr0
% Parse out state vector components
X = x(1:3);
Theta = x(4:6);
phi = Theta(1);
theta = Theta(2);
psi = Theta(3);
V = x(7:9);
u = V(1);
v = V(2);
w = V(3);
omega = x(10:12);
p = omega(1);
q = omega(2);
r = omega(3);
%Actuator deflections taken from state vector
de = x(13);
da = x(14);
dr = x(15);
RIB = expm(psi*hat(e3))*expm(theta*hat(e2))*expm(phi*hat(e1));
LIB = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)];
W = [0;0;0];
Vr = V - W;
ur = Vr(1);
vr = Vr(2);
wr = Vr(3);
PDyn = (1/2)*rho*norm(Vr)^2;
omegaf =[0;0;0];
omegar = omega - omegaf;
pr = omegar(1);
qr = omegar(2);
rr = omegar(3);
%%% AERODYNAMIC FORCES AND MOMENTS %%%
% Aerodynamic angles
%beta = asin(Vr(2)/norm(Vr));
%alpha = atan2(Vr(3),Vr(1));
% Control deflections
%de = deEq;
%da = 0;
%da = - 0.5*phi - 2*pr; % Add some roll stiffness and damping
%dr = 0;
%added
de0 = double(t:tau0:tau_1);
da0 = double(t:tau0:tau_1);
dr0 = double(t:tau0:tau_1);
dT = dTEq;
dec = de0*(t >= tau0);
dac = da0*(t >= tau0);
drc = dr0*(t >= tau0);
dedot = (dec - de)/tau_1;
%dadot = (dec - de)/tau_1;
%drdot = (dec - de)/tau_1;
dadot = (dac - da)/tau_1;
drdot = (drc - dr)/tau_1;
% Aerodynamic force coefficients
Cx = Cx0 + Cxu*(ur/norm(Vr)) + Cxw*(wr/norm(Vr)) + Cxw2*(wr/norm(Vr))^2;
Cy = Cy0 + Cyv*(vr/norm(Vr)) + Cyp*((b*pr)/(2*norm(V))) + Cyr*((b*rr)/(2*norm(Vr))) ...
+ Cyda*da + Cydr*dr;
Cz = Cz0 + Czw*(wr/norm(Vr)) + Czw2*(wr/norm(Vr))^2 + Czq*((c*qr)/(2*norm(Vr))) ...
+ Czde*de;
Thrust = dT*Power/norm(Vr); % Constant power
X = PDyn*S*Cx + Thrust;
Y = PDyn*S*Cy;
Z = PDyn*S*Cz;
Force_Aero = [X; Y; Z];
% Components of aerodynamic moment (modulo "unsteady" terms)
Cl = Cl0 + Clv*(vr/norm(Vr)) + Clp*((b*pr)/(2*norm(Vr))) + Clr*((b*rr)/(2*norm(Vr))) + ...
Clda*da + Cldr*dr;
Cm = Cm0 + Cmw*(wr/norm(Vr)) + Cmq*((c*qr)/(2*norm(Vr))) + Cmde*de;
Cn = Cn0 + Cnv*(vr/norm(Vr)) + Cnv2*(vr/norm(Vr))^2 + Cnp*((b*pr)/(2*norm(Vr))) + Cnr*((b*rr)/(2*norm(Vr))) + ...
Cnda*da + Cndr*dr;
L = PDyn*S*b*Cl;
M = PDyn*S*c*Cm;
N = PDyn*S*b*Cn;
Moment_Aero = [L; M; N];
% Sum of forces and moments
Force = RIB'*(m*g*e3) + Force_Aero;
Moment = Moment_Aero;
%%% EQUATIONS OF MOTION %%%
%%% KINEMATIC EQUATIONS %%%
XDot = RIB*V;
ThetaDot = LIB*omega;
%%% DYNAMIC EQUATIONS %%%
VDot = (1/m)*(cross(m*V,omega) + Force);
omegaDot = inv(Inertia)*(cross(Inertia*omega,omega) + Moment);
%xdot = [XDot; ThetaDot; VDot; omegaDot];
xdot = [XDot; ThetaDot; VDot; omegaDot; dedot; dadot; drdot];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dc = doublet(t,u)
global e1 e2 e3 rho m g S b c Inertia Power ...
Cx0 Cxu Cxw Cxw2 ...
Cz0 Czw Czw2 Czq Czde ...
Cm0 Cmw Cmq Cmde ...
Cy0 Cyv Cyp Cyr Cyda Cydr ...
Cl0 Clv Clp Clr Clda Cldr ...
Cn0 Cnv Cnv2 Cnp Cnr Cnda Cndr ...
dTEq deEq ...
VonKarmanFlag Amp Phase Omega Phase2 Omega2 tau0 tau_1 ...
%removed de0 da0 dr0
%Actuator deflections are defined
de0 = 0.001;
da0 = 0.0005;
dr0 = 0.0002;
tau0 = 1; % actuator delay
tau_1 = 2; % time constant
% Basis vectors
e1 = [1;0;0];
e2 = [0;1;0];
e3 = [0;0;1];
% Atmospheric and gravity parameters (Constant altitude: Sea level)
a = 340.3; % Speed of sound (m/s)
rho = 1.225; % Density (kg/m^3)
g = 9.80665; % Gravitational acceleration (m/s^2)
% Aircraft parameters (Bix3)
m = 0.211; % Mass (slug)
W = m*g; % Weight (Newtons)
Ix = 0.2163; % Roll inertia (slug-ft^2)
Iy = 0.1823; % Pitch inertia (slug-ft^2)
Iz = 0.3396; % Yaw inertia (slug-ft^2)
Ixz = 0.0364; % Roll/Yaw product of inertia (slug-ft^2)
Inertia = [Ix, 0, -Ixz; 0, Iy, 0; -Ixz, 0, Iz];
S = 4.92; % Wing area (ft^2)
b = 5.91; % Wing span (ft)
c = 0.833; % Wing chord (ft)
AR = (b^2)/S; % Aspect ratio
% Longitudinal nondimensional stability and control derivatives
Cx0 = 0;
Cxu = -0.156;
Cxw = 0.297;
Cxw2 = 0.960;
Cz0 = -0.179;
Czw = -5.32;
Czw2 = 7.02;
Czq = -8.20;
Czde = -0.308;
Cm0 = 0.0134;
Cmw = -0.240;
Cmq = -4.49;
Cmde = -0.364;
% Lateral-directional nondimensional stability and control derivatives
Cy0 = 0;
Cyv = -0.251;
Cyp = 0.170;
Cyr = 0.350;
Cyda = 0.103;
Cydr = 0.0157;
Cl0 = 0;
Clv = -0.0756;
Clp = -0.319;
Clr = 0.183;
Clda = 0.170;
Cldr = -0.0117;
Cn0 = 0;
Cnv = 0.0408;
Cnv2 = 0.0126;
Cnp = -0.242;
Cnr = -0.166;
Cnda = 0.0416;
Cndr = -0.0618;
% Solve for wings-level, constant-altitude equilibrium flight condition
VEq = 13.7; % Initial guess for speed (m/s) 45ft/s converted to (m/s)
thetaEq = 2*(pi/180); % Initial guess for pitch (rad)
deEq = -(Cm0 + Cmw*sin(thetaEq))/Cmde; % Initial guess for elevator (rad)
Power = 200; % Maximum Power (W)
CT0 = 0.197;
dTEq = (CT0*rho*S*(VEq^3))/(2*Power); % Nominal throttle setting
% Define initial state
X0 = zeros(3,1); % (m)
Theta0 = [0;thetaEq;0]; % (rad)
V0 = [VEq*cos(thetaEq);0;VEq*sin(thetaEq)]; % (m/s)
omega0 = zeros(3,1); % (rad/s)
%de_initial = zeros(3,1);
%da_initial = zeros(3,1);
%dr_initial = zeros(3,1);
de_initial = 0;
da_initial = 0;
dr_initial = 0;
%y0 = [X0; Theta0; V0; omega0];
y0 = [X0; Theta0; V0; omega0; de_initial; da_initial; dr_initial];
t_final = 10;
[t,y] = ode45(@GenericFixedWingEOM,[0:0.1:t_final]',y0,odeset('InitialStep',tau0));
%t_final = 30;
%[t,y] = ode45('GenericFixedWingEOM',[0:0.1:t_final]',y0);}
%%%% CODE TO COMPUTE FLOW RELATIVE VELOCITY, ETC %%%%
figure(1)
subplot(2,1,1)
plot(t,y(:,1:3))
legend('X','Y','Z')
ylabel('Position (m)')
subplot(2,1,2)
plot(t,y(:,4:6)*(180/pi))
legend('\phi','\theta','\psi')
ylabel('Attitude (deg)')
figure(2)
subplot(2,1,1)
plot(t,y(:,7:9))
legend('u','v','w')
ylabel('Velocity (m/s)')
subplot(2,1,2)
plot(t,y(:,10:12)*(180/pi))
ylabel('Angular Velocity (deg/s)')
legend('p','q','r')
figure
plot(t,y(:,13:15))
legend('de','da','dr')
The call in GenericFixedWingEOM should be to "doublet", not "double." I realize you copied exactly what I typed in that comment (since corrected).
What is your definition of a doublet?
I assumed that it's some sort of actuator command defined as a function of time. For example, step the actuator command to 0.001 for 1 second, down to -0.001 for 1 second, and back to zero.
t = 0:.001:3;
figure
plot(t,doublet(t))
ylim([-1.1 1.1]*1e-3)
xlabel('Time (sec)');
ylabel('Actuator Command')
Perhaps my assumption was incorrect.
function dc = doublet(t)
dc = 0.001*(t < 1) - 0.001*(t >=1 & t < 2) + 0*(t >= 2);
end

Sign in to comment.

More Answers (1)

Try something like this:
tau0 = 2;
tau1 = 3;
Gp = tf(1, [tau1 1],'InputDelay', tau0)
Gp = 1 exp(-2*s) * ------- 3 s + 1 Continuous-time transfer function.
For more info, please check out:

Asked:

on 19 Sep 2022

Commented:

on 26 Sep 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!