I have an error but I dont know where it is referring to "Index exceeds the number of array elements"

1 view (last 30 days)
This is my code for the intepreted MATLAB function on simulink
function [XDOT] = RCAM_model(X,U)
% ---------- State and control vectors ----------
x(1) = X(1); %u
x(2) = X(2); %alpha
x(3) = X(3); %q
x(4) = X(4); %theta
x(5) = X(5); %mass
x(6) = X(6); %vertical
x(7) = X(7); %horizontal
u1 = U(1); %eledefl
u2 = U(2); %thrustvec
u3 = U(3); %power
% ---------- Constants ----------
b = 10.2;
c = 1.74;
S = 17.1;
initial_mass = 1248.5;
Ix = 1421;
Iy = 4067.5;
Iz = 4786;
Ixz = 200;
g = 9.81;
rho = 1.225;
speedsound = 340;
% ---------- Control limits ----------
u1min = 0;
u1max = 25*pi/180;
u2min = 0;
u2max = 90*pi/180;
u3min = 1;
u3max = 4;
if(u1>u1max)
u1 = u1max;
else if (u<u1min)
u1 =u1min;
end
if(u2>u2max)
u2 = u2max;
else if (u<u2min)
u2 =u2min;
end
if(u3>u3max)
u3 = u3max;
else if (u<u3min)
u3 =u3min;
end
% ---------- Aerodynamic force coefficients ----------
CL = 6.44*x(2) + 3.8*c*x(3)/(2*x(1)) + 0.355*u(1); % Lift
L = 0.5*rho*speed^2 * CL;
CD = 0.03 + 0.05*CL^2; % Drag
D = 0.5*rho*speed^2 * CD;
Cm = 0.05 - 0.683*x(2) - 9.96*c*x(3)/(2*x(1)) - 0.923*u(1); % pitching moment
m = 0.5*rho*speed^2*S*c*Cm;
% ---------- Engine force ----------
thrust = u(3) * ( (7+speed/speedsound)*200/3 + x(6)*(2*speed/speedsound -11) )
% ---------- XDOT ----------
XDOT = [(thrust * cos(x(2)+x(2)) - D)/ x(5) - g*sin(x(4)-x(2));
q - (thrust*sin(x(2)+u(2)) + L) / x(5)* x(1) + g/x(1)*cos(x(4) - x(2));
m/Iy;
x(3);
-200/60/60/1000 * thrust;
x(1)*sin(x(4)-x(2));
x(1)*cos(x(4)-x(2))]
Below is my initilaizing constants script and to run the simulink simulation
% Initialize constatns for the rcam simulation
clear clc
close all
%% Define constants
x0 = [63.292; 0.043891; 0; 0.043891; 1248.5; 0; 0]; % u, alpha, q, theta, mass, vertical, horizontal
u = [0.021692; 0; 1]; % eledefl, thrustvec, power
TF = 60;
%% Run the model
sim('RCAMSimulation.slx')
I cant run the simulation and I got the error:
  • Error using InitializeConstants (line 15)
  • Error due to multiple causes.
  • Caused by:
  • Error using InitializeConstants (line 15)
  • Index exceeds the number of array elements (4).
  • Error using InitializeConstants (line 15)
  • Error in 'RCAMSimulation/Interpreted MATLAB Function' while evaluating expression.
Thank you for your help!
  4 Comments
Walter Roberson
Walter Roberson on 3 Jan 2022
If you want u(1) to refer to u = [0.021692; 0; 1]; % eledefl, thrustvec, power then I suspect you might need to use a global memory store and a "global" statement in the MATLAB code. https://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html
Alternately, I think you just might be able to use the workspace variable in the definition for the value of a Constant block, which you would then connect as an input to the MATLAB Function Block.

Sign in to comment.

Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!