1

I am trying to integrate a DE using Euler's method. I can get through the entire method once, but it will not reloop. I end up with the error of "Subscript indices must either be real positive integers or logicals.", and I'm not sure why.

I already found: Subscript indices must either be real positive integers or logicals, generic solution and tried those methods of troubleshooting, to no avail.

Here is my code. My apologies for the sloppiness of it:

clc 
clear

N(1) = 2.5; %1st gear
N(2) = 2.0; %2nd ^
N(3) = 1.0; %3rd ^

Diff = 2.3; %Final drive

u = 0.8;
fr = 0.015;
Rt = .303; %Tire radius in m
W = 13400; %Weight in N
rho = 1.25; %Air density in kg/m^3
A = 2.10; %Frontal alrea in m^2
Cd = 0.38; %Coeff of drag
L = 2.61; %Wheelbase in m
LH = 3.93; %L/H ratio
FR = 51.5; %F/R weight ratio
theta1 = 0; %Road incline
theta2 = 5; %Road incline
Ftmax = 3000;
m = W/9.81;

w = 0.001;
xdot = 0.001;

dbstop if error
% T = -(0.00108*w^2)+0.50143+111.77086 %Engine tq lookup

for i = 1:0.001:200 %Total time of 200 seconds in 0.001 second intervals
%        if i<.001
%        end
    T(i) = -(0.00108*w(i)^2)+0.50143*w(i)+111.77086; %Engine tq lookup

    Ft(i) = T(i)*N(1)*Diff/Rt; %Calculate tractive force

    if Ft>Ftmax   %Check for traction limit
        Ft = Ftmax;
    end

    xddot(i) = (1/m)*(Ft(i)/xdot(i)-W*u*(1+xdot(i)*3.6/160)-0.5*rho*Cd*A*xdot(i)^2-W*sin(theta1));% Using m/s for velocity
    xdot(i) = xddot(i)*0.001+xdot(i); %Velocity calculation
    w(i) = xdot(i)/Rt*N(1)*Diff; %Engine speed calculation
end

I have asked a few friends that code, and they have no idea either. Hopefuly someone here can help. Thanks!

Zak Parker
  • 13
  • 2

1 Answers1

0

Your subscript indices aren't integers. So instead of:

for i = 1:0.001:200 %Total time of 200 seconds in 0.001 second intervals

You could try something like this:

for i = 1:200*1000 %Total time of 200 seconds in 1 millisecond intervals

Also, since you're working with vectors in the loop, you might want to allocate them ahead of time, so you could replace this:

w = 0.001;
xdot = 0.001;

With this:

steps = 200*1000; % You might also want to replace 200*1000 in the for-loop with 'steps'

T = zeros(1,steps);
Ft = zeros(1,steps);
xddot = zeros(1,steps);
xdot = zeros(1,steps);
w = zeros(1,steps);

w(1) = 0.001;
xdot(1) = 0.001;
frslm
  • 2,969
  • 3
  • 13
  • 26