Info

This question is closed. Reopen it to edit or answer.

Array indices must be positive integers or logical values error

1 view (last 30 days)
I need to calculate the velocity, and height of a rocket for each iteration from acceleration. However the code will only run a_i, and u_i, and wont give out h_i claiming that the array indices must be positive integers or logical values. The code below is how I am trying to solve it.
for n=linspace(0,50,7500)
syms a_i u_1 h_i
g_i=g_0;
a_0=T_0/m_0-g_0*cosd(Theta_0)-(rho_0*(u_0^2)*Cd_On(1)*A)/(2*m_0);
u_i=zeros(1,7500);
h_i=zeros(1,7500);
a_i=[a_0 (T_t./m_t)-g_i.*cosd(Theta_0)-(rho_0.*(u_i(1-n).^2).*Cd_On*A)./(2.*m_t)];
u_i=u_i(1-n)+(a_i+a_i(1+n)).*(dt)/2;
h_i=h_i(1-n)+u_i(n).*(dt);
end
I have tried fixing it by modifying the code to the code below, but it can't even run a_i.
for i=linspace(0,50,7500)
g_i=g_0;
a_y_0=(T_0./m_0)-(g_0.*cosd(Theta_0))-(rho_0.*(u_0^2).*Cd_On(1)*A/(2.*m_0));
u_y_i=zeros(1,7500);
h_i=zeros(1,7500);
a_y_i=[a_y_0 (T_t./m_t)-g_i.*cosd(Theta_0)-(rho_0.*(u_y_i(i+dt).^2).*Cd_On*A)./(2.*m_t)];
u_i=u_i(1-n)+(a_i+a_i(1+n)).*(dt)/2;
h_i=h_i(1-n)+u_i(n).*(dt);
end
Thanks for the help.

Answers (1)

Image Analyst
Image Analyst on 25 Feb 2020
Edited: Image Analyst on 25 Feb 2020
Try this:
values = linspace(0,50,7500);
for k = 1 : length(values)
n = values(k);
% syms a_i u_1 h_i
g_i=g_0;
a_0=T_0/m_0-g_0*cosd(Theta_0)-(rho_0*(u_0^2)*Cd_On(1)*A)/(2*m_0);
u_i=zeros(1,7500);
h_i=zeros(1,7500);
a_i=[a_0 (T_t./m_t)-g_i.*cosd(Theta_0)-(rho_0.*(u_i(1-n).^2).*Cd_On*A)./(2.*m_t)];
u_i=u_i(1-n)+(a_i+a_i(1+n)).*(dt)/2;
h_i=h_i(1-n)+u_i(n).*(dt);
end
But we don't know what you want to do. You're overwriting h_i, a_i, etc. on every iteration. You need to think about this some more.

Community Treasure Hunt

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

Start Hunting!