Error iterating with fractions

3 ビュー (過去 30 日間)
Andrew Poissant
Andrew Poissant 2017 年 4 月 4 日
編集済み: Stephen23 2017 年 4 月 5 日
I want to do something pretty simple but am not sure how to do because I can't iterate using fractions. What I want is to solve for x(i), y(i), and z(i). All three are dependent on the previous value. I have attached an attempt to solve this below but naturally I can't iterate using fractions. My tf value is 3.5272 so naturally I want more than 3 or 4 iterations. Any ideas? I just want to solve the three equations for a time step of 0.1 and eventually plot it. I get an error in the line where I define x(i) saying "subscript must be either real positive integers or logicals."
theta = 50; % deg
phi = 0; % deg
u = 16; % m/s
u_x = u*cosd(theta)*cosd(phi);
u_y = u*sind(theta)*cosd(phi);
u_z = u*sind(phi);
x(1) = 0;
y(1) = 0;
z(1) = 60;
tf = 3.5272;
for i = 1.1:0.1:roundn(tf,-1)
x(i) = u_x*i + 0.5*ax*i^2 + x(i-0.1);
y(i) = u_y*i + 0.5*ay*i^2 + y(i-0.1);
z(i) = -u_z*i + 0.5*az*i^2 + z(i-0.1);
end

採用された回答

Stephen23
Stephen23 2017 年 4 月 4 日
編集済み: Stephen23 2017 年 4 月 5 日
Fractions cannot be used as indices. Put the fractional values in a vector and make the indices integers:
vec = 1.1:0.1:roundn(tf,-1);
for k = 2:numel(vec)
val = vec(k);
x(k) = +u_x*val + 0.5*ax*val^2 + x(k-1);
y(k) = +u_y*val + 0.5*ay*val^2 + y(k-1);
z(k) = -u_z*val + 0.5*az*val^2 + z(k-1);
end
[edited based on comments below]
  4 件のコメント
Les Beckham
Les Beckham 2017 年 4 月 4 日
I suspect that is what Stephen intended. You will also need to change the for loop to start at 2 instead of 1 (so you don't try to index into x with a zero).
Andrew Poissant
Andrew Poissant 2017 年 4 月 4 日
Got it, thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by