How can I modify this? Error in 'Simu_Kine​matics_Dyn​amics_Mode​l_LV/Dynam​ics' while executing MATLAB S-function 'aircraft_dynamics', flag = 1 (derivatives), at time 0.0.

10 Ansichten (letzte 30 Tage)
Below are my codes, please give me some guidance in modifying this. I could not run this file in simulink after I type the matrices in matlab. Thank you so much!
Error:
Error in 'Simu_Kinematics_Dynamics_Model_LV/Dynamics' while executing MATLAB S-function 'aircraft_dynamics', flag = 1 (derivatives), at time 0.0.
Dimensions of arrays being concatenated are not consistent.
function [sys,x0,str,ts,simStateCompliance] = aircraft_dynamics(t,x,u,flag,P)
switch flag
% Initialization %
case 0
[sys,x0,str,ts,simStateCompliance] = mdlInitializeSizes(P);
% Derivatives %
case 1
sys = mdlDerivatives(t,x,u,P);
% Update %
case 2
sys = mdlUpdate(t,x,u);
% Outputs %
case 3
sys = mdlOutputs(t,x,u);
% GetTimeOfNextVarHit %
case 4
sys = mdlGetTimeOfNextVarHit(t,x,u);
% Terminate %
case 9
sys = mdlTerminate(t,x,u);
% Unexpected flags %
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
% end sfuntmpl
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(P)
% call simsizes for a sizes structure, fill it in and convert it to a sizes array.
sizes = simsizes;
sizes.NumContStates = 12;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 12;
sizes.NumInputs = 6;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
% initialize the initial conditions
x0 = [...
P.pn0;...
P.pe0;...
P.pd0;...
P.u0;...
P.v0;...
P.w0;...
P.phi0;...
P.theta0;...
P.psi0;...
P.p0;...
P.q0;...
P.r0;...
];
% str is always an empty matrix
str = [];
% initialize the array of sample times
ts = [0 0];
% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < The default setting; warn and assume DefaultSimState
% 'DefaultSimState', < Same sim state as a built-in block
% 'HasNoSimState', < No sim state
% 'DisallowSimState' < Error out when saving or restoring the model sim state
simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
function sys = mdlDerivatives(t, x, uu, P)
pn = x(1);
pe = x(2);
pe = x(3);
u = x(4);
v = x(5);
w = x(6);
phi = x(7);
theta = x(8);
psi = x(9);
p = x(10);
q = x(11);
r = x(12);
fx = uu(1);
fy = uu(2);
fz = uu(3);
ell = uu(4);
m = uu(5);
n = uu(6);
% Time derivative of pn, pe, pd
neddot = [cos(theta)*cos(psi),...
sin(phi)*sin(theta)*cos(psi) -cos(phi)*sin(psi),...
cos(phi)*sin(theta)*cos(psi) +sin(phi)*sin(psi);...
cos(theta)*sin(psi),...
sin(phi)*sin(theta)*sin(psi)+cos(phi)*cos(psi),...
cos(phi)*sin(theta)*sin(psi) -sin(phi)*cos(psi);...
-sin(theta),...
sin(phi)*cos(theta),...
cos(phi)*cos(theta)]*[u;v;w]; % To be completed by students
% Time derivative of u, v, w
veldot = [r*v-q*w; p*w-r*u; q*u-p*v]+1/P.mass*[fx;fy;fz]; % To be completed by students
% Time derivative of phi, theta, psi
attdot = [1, sin(phi)*tan(theta), cos(phi)*tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)]*[p;q;r]; % To be completed by students
% Computation for 8 gamma
gamma = P.Jx * P.Jz - P.Jxz^2;
gam1 = P.Jxz * (P.Jx-P.Jy + P.Jz)/gamma;
gam2 = (P.Jz * (P.Jz-P.Jy) + P.Jxz^2)/gamma;
gam3 = P.Jz/gamma;
gam4 = P.Jxz/gamma;
gam5 = (P.Jz - P.Jx)/P.Jy;
gam6 = P.Jxz/P.Jy;
gam7 = ((P.Jx - P.Jy)*P.Jx + P.Jxz^2)/gamma;
gam8 = P.Jx/gamma;
% Time derivative of p, q, r
attratdot = [gam1*pq - gam2*qr; gam5*pr-gam6*(p^2- r^2); gam7*pq-gam1*qr]+[ gam3*ell+ gam4*n; (l/P.Jy)*m; gam4*ell+gam8*n]; % To be completed by students
pndot = neddot(1);
pedot = neddot(2);
pddot = neddot(3);
udot = veldot(1);
vdot = veldot(2);
wdot = veldot(3);
phidot = attdot(1);
thetadot = attdot(2);
psidot = attdot(3);
pdot = attratdot(1);
qdot = attratdot(2);
rdot = attratdot(3);
sys = [pndot; pedot; pddot; udot; vdot; wdot; phidot; thetadot; psidot; pdot; qdot; rdot];
% end mdlDerivatives
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
function sys = mdlUpdate(t,x,u)
sys = [];
% end mdlUpdate
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
function sys = mdlOutputs(t,x,u)
sys = x;
% end mdlOutputs
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block. Note that the result is
% absolute time. Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
function sys = mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; % Example, set the next hit to be one second later.
sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
function sys = mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate

Antworten (1)

Walter Roberson
Walter Roberson am 21 Mär. 2022
neddot = [cos(theta)*cos(psi),...
theta and psi are scalar values so cos()*cos() is a scalar value, so the first element of the calculation cos(theta)*cos(psi) gives a scalar value.
sin(phi)*sin(theta)*cos(psi) -cos(phi)*sin(psi),...
phi and theta and psi are scalar values, so the next element of the calculation, sin(phi)*sin(theta)*cos(psi), gives a scalar value.
phi and psi are scalar values, so the next element of the calculation, -cos(phi)*sin(psi) gives a scalar value.
cos(phi)*sin(theta)*cos(psi) +sin(phi)*sin(psi);...
phi, theta, psi are scalars, so the next element of the calculation, cos(phi)*sin(theta)*cos(psi) gives a scalar value.
phi and psi are scalars, so the next element of the calculation, +sin(phi)*sin(psi) gives a scalar value.
That was one element on the first line, two elements on the second line, two elements on the third line, for a total of 5 elements in the row before you encounter the semi-colon
cos(theta)*sin(psi),...
... scalar value for cos(theta)*sin(psi)
sin(phi)*sin(theta)*sin(psi)+cos(phi)*cos(psi),...
... scalar value for sin(phi)*sin(theta)*sin(psi)+cos(phi)*cos(psi)
cos(phi)*sin(theta)*sin(psi) -sin(phi)*cos(psi);...
... scalar value for cos(phi)*sin(theta)*sin(psi)
... scalar value for -sin(phi)*cos(psi)
That was one element on the first line, one element on the second line, two elements on the third line, for a total of 4 elements in the row before you encounter the semi-colon.
So the first row had five elements on it, the second row has four elements on it.
-sin(theta),...
sin(phi)*cos(theta),...
cos(phi)*cos(theta)]*[u;v;w]; % To be completed by students
... scalar, scalar, scalar, so the third row has three elements on it.
Why did
sin(phi)*sin(theta)*sin(psi)+cos(phi)*cos(psi),...
evaluate to one element but
cos(phi)*sin(theta)*sin(psi) -sin(phi)*cos(psi)
evaluates to two elements?
.... Look at that space. The ones that have the space evaluate to two elements but the one without the space evaluates to one element.
Does this mean that spaces mark the end of elements? Not exactly
Suppose you write [1 -1] as a vector. What length do you expect it to be? What about if you write [1-1] ? What about if you write [1 - 1] ? You probably expect the [1 -1] to be two elements but you probably expect [1-1] to be a single element, so intuitively space can at least sometimes signal the end of an element in a list. But then the meaning of [1 - 1] starts to get a bit confusing.
The key here is that in mathematics, it is common to write one as +1 for emphasis, and it is common to write negative one as -1. The + immediately before a value or the - immediately before a value has the potential to be a "unary plus" or "unary minus" instead of an addition or subtraction between adjacent elements. [1 -1 +1] is treated as [1, -1, +1] and [x -x +x] is treated as [x, -x, +x] . The "unary minus" and "unary plus" are separate operators that are not identical to inserting a 0 . The expression -x is treated as uminus(x) instead of as 0-x and the expression +x is treated as uplus(x) instead of as 0+x . For numeric values it makes no difference, but it can make a difference for other kinds of objects.
If there is one or more space between the + or - and the value then they are not treated as unary operators.
If there is a value before the + or - and there is no space between the value and the following + or -, then they are not treated as unary operators
[5 - 5] ---> horzcat(minus(5,5)) --> horzcat(0) --> 0 (space between - and following value)
[5- 5] ---> horzcat(minus(5,5)) --> horzcat(0) --> 0 (space between - and following value)
[5-5] ---> horzcat(minus(5,5)) --> horzcat(0) --> 0 (no space before -)
[5 -5] ---> horzcat(5, 0-5) (space between value and following -, no space between - and following value)

Kategorien

Mehr zu Response Computation and Visualization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by