I have an error but I dont know where it is referring to "Index exc... (2024)

1 view (last 30 days)

Show older comments

Rachel Ong on 3 Jan 2022

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements

Commented: Rachel Ong on 4 Jan 2022

Open in MATLAB Online

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;

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

Show 2 older commentsHide 2 older comments

Walter Roberson on 3 Jan 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1916210

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1916210

Open in MATLAB Online

CL = 6.44*x(2) + 3.8*c*x(3)/(2*x(1)) + 0.355*u(1); % Lift

Could you point me to where lower-case u is coming from in that function? You have a parameter which is U (upper-case) but I do not see u (lower-case) assigned to or passed in?

Rachel Ong on 3 Jan 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1916400

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1916400

Hello, so should I be using the upper cause U instead of the lower case u? I am a bit confused there as well. The U or u I am referring to are the variables in the system.

Walter Roberson on 3 Jan 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1916465

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1916465

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.

Rachel Ong on 4 Jan 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1917100

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1621445-i-have-an-error-but-i-dont-know-where-it-is-referring-to-index-exceeds-the-number-of-array-elements#comment_1917100

I see okay let me try it out. Thank you.

Sign in to comment.

Sign in to answer this question.

Answers (0)

Sign in to answer this question.

See Also

Categories

SimulinkApplicationsGeneral Applications

Find more on General Applications in Help Center and File Exchange

Tags

  • simulink
  • simulation
  • elements

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


I have an error but I dont know where it is referring to "Index exc... (6)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

I have an error but I dont know where it is referring to "Index exc... (2024)

References

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 5985

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.