error: index exceeds matrix dimensions
1 view (last 30 days)
Show older comments
I have the following code that I am trying to run but keep getting the "index exceeds matrix dimensions" error for the rho_a line. Does anyone know how to fix this error?
h1=0;r=0;go=9.81;Mi_1=298000;
for t=0:100;
Pc2_RD_1(t+1) = Pc_RD*(1+(.015*dt_1)^2);
Pe_RD_1(t+1) = Pc2_RD_1(t+1)*(1+((gamma_RD-1)/2)*Me_RD^2)^(gamma_RD/(1-gamma_RD));
Isp_RD_1(t+1) = (e_RD/Pc2_RD_1(t+1))*(Pe_RD_1(t+1)-Pa));
mprop_RD1(t+1) = At_RD*Pc2_RD_1(t+1)/c_RD;
thrust_RD1(t+1) = Isp_RD_1(t+1)*g0*mprop_RD1(t+1)/1000;
dM_RD1(t+1) = mprop_RD1(t+1)*dt_1
dU1(t+1) = Isp_RD_1(t+1)*g0*dM_RD1(t+1)/Mi_1
rho_a(t+1) = 1.2*exp(((-2.9e-5)*h1(t+1)^1.15))
D(t+1) = .5*C_D*Af*rho_a(t+1)*dU1(t+1)^2
g(t+1) = g0*(r_E/(r_E+h1))^2
Uy_dt(t+1) = dU1(t+1)*cos(theta)-dt_1(t+1)*(D(t+1)/(Mi_1-dM_RD1(t+1))+g(t+1))*cos(theta)
Ux_dt(t+1) = dU1(t+1)*sin(theta(t+1))-dt_1(t+1)*(D(t+1)/(Mi_1-dM_RD1(t+1)))*sin(theta(t+1))
U(t+1) = (Ux_dt(t+1))^2+(Uy_dt(t+1))^2
h1(t+1) = h1(t+1)+Uy_dt(t+1)*dt_1(t+1)
r(t+1) = r(t+1)+Ux_dt(t+1)*dt_1(t+1)
if Ux_dt(t+1)>0
theta(t+1) = atan(Ux_dt(t+1)/Uy_dt(t+1))
else
theta(t+1) = atan(Ux_dt(t+1)/Uy_dt(t+1))+pi()
end
dt_1(t+1)=dt_1+.2;
time(t+1)=t+.2;
end
4 Comments
Image Analyst
on 14 Mar 2016
This uncommented code is impenetrable. It looks like alphabet soup to me. I suggest you step through it a line at a time in the debugger to figure out what it's doing.
Answers (2)
Image Analyst
on 13 Mar 2016
You set h1 to be zero, a scalar. So why do you think there should be a (t+1)'th element as if it were an array? It's not an array.
Guillaume
on 13 Mar 2016
What's puzzling is why you can't see the problem.
rho(t+1) depends on h1(t+1), which does not exists yet, hence why you get the error. h1(t+1) is calculated later but since it depends on rho(t+1), you've got a circular reference. Assuming the circular reference is a mistake, only you can tell which of the equations is wrong.
2 Comments
Guillaume
on 14 Mar 2016
The problem is that you're using t both as an index and as your time. Ideally, the two should be decoupled, and you'd have an index going from 1 to numel(t) with t a vector of times (e.g 0:100 for example).
t = 0:100;
h1 = [h0, zeros(size(t))]; %h1(1) is initial condition
PC2_RD = [PC_RD, zeros(size(t))]; %PC_RD is initial condition
%... etc. for all other arrays. They all should have numel(t)+1 elements
for iter = 1:numel(t)
PC2_RD(iter+1) = PC2_RD(iter) * (1+(.015*dt_1)^2); %is dt_1 supposed to change during the iteration?
%...
rho_a(iter+1) = 1.2*exp(((-2.9e-5)*h1(iter)^1.15))
%...
end
See Also
Categories
Find more on Matrix Indexing 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!