Info

此问题已关闭。 请重新打开它进行编辑或回答。

Array indices must be positive integers or logical values error

1 次查看(过去 30 天)
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.

回答(1 个)

Image Analyst
Image Analyst 2020-2-25
编辑:Image Analyst 2020-2-25
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!

Translated by