Keep getting the "Array indices must be positive integers or logical values." error

3 ビュー (過去 30 日間)
Tom Oud
Tom Oud 2018 年 12 月 12 日
コメント済み: Tom Oud 2018 年 12 月 13 日
Trying to simulate a water rocket using matlab, however I can't get my for loop to work properly.
This is what I have so far:
close all
clear all
P0 = 6e5; %Pa
Pout = 1e5; %Pa
V0 = 2; %l
V = 1.5; %l
gamma = 1.4;
rho_w = 998; %kg/m^3
%Pin = P0*(V/V0).^-gamma;
dt = 0.01;
t = 1:dt:100;
Ve(1) = 0; %m/s
Pin(1) = 6e5; %Pa
v0 = 0; %m/s
v = 0; %m/s
M0 = 0.1; %kg
M = M0 + (V0-V)*rho_w;
for i = 1:100
Pin(i+1) = Pin(i) - P0(V/V0).^-gamma;
Ve(i+1) = sqrt(2*(Pin(i) - P0)/rho_w);
%v = Ve*log(M0/M) + v0;
end
Really hoping one of you can enlighten me, because I'm completely lost here.
  3 件のコメント
Image Analyst
Image Analyst 2018 年 12 月 12 日
V/V0 = 1.5/2, not 0/0
madhan's Answer below lets the code run without error. No guarantee about the equations correctness though - I didn't try to verify them.
Guillaume
Guillaume 2018 年 12 月 12 日
Oh! I didn't see that there were both V and V0 variables (uppercase) as well as v and v0 (lowercase).
That's really begging for trouble! You can be sure at some point the wrong case will be used. Never distinguish variable names just by casing.

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

採用された回答

madhan ravi
madhan ravi 2018 年 12 月 12 日
Pin(i+1) = Pin(i) - P0*(V/V0).^-gamma;
^----missed it
  12 件のコメント
Tom Oud
Tom Oud 2018 年 12 月 12 日
Of course!
P0 = 6e5; %Pa
Pout = 1e5; %Pa
V0 = 2; %l
V = 1.5; %l
gamma = 1.4;
rho_w = 998; %kg/m^3
dt = 0.001;
t = 1:dt:10;
Ve(1) = 0; %m/s
Pin(1) = 6e5; %Pa
v0 = 0; %m/s
v = 0; %m/s
M0 = 0.1; %kg
M = M0 + (V0-V)*rho_w;
for i = 1:numel(t)
k = t(i);
Pin(i+1) = Pin(i) - P0*(V/V0).^-gamma;
Ve(i+1) = sqrt(2*(Pin(i) - Pout)/rho_w);
if Pin(i) < P0
Ve(i+1) = Ve(i);
end
%v = Ve*log(M0/M) + v0;
end
Tom Oud
Tom Oud 2018 年 12 月 13 日
Turns out I interpreted my results wrong. Decreasing the timestep now just seems to add more steps in total, instead of making the increment smaller.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by