Array indices must be positive integers or logical values.

1 回表示 (過去 30 日間)
Ali Deniz
Ali Deniz 2021 年 10 月 16 日
回答済み: Image Analyst 2021 年 10 月 18 日
I am trying to solve an equation by using Runge-Kutta Euler Method. Why do I get "Array indices must be positive integers or logical
values." error?
%Euler Method
%parameters
g=9.81;
rho=1.2;
s=0.00011;
m=0.023;
Cd=0.9;
%Initial Condition
V0=0;
V=0;
%I choose dt as
dt=2;
%time interval
t0=0;
ts=10;
t=0;
i=0;
while dt<ts;
d1=g-0.5.*rho.*V0.^2*(s/m).*Cd;
phiAvg=d1;
V(i)=V0+dt.*phiAvg;
V0=V;
t(i)=t;
i=i+1;
t=t+dt;
end
plot(V,t)

採用された回答

KSSV
KSSV 2021 年 10 月 16 日
%Euler Method
%parameters
g=9.81;
rho=1.2;
s=0.00011;
m=0.023;
Cd=0.9;
%Initial Condition
V0=0;
V=zeros([],1);
%I choose dt as
dt=0.2;
%time interval
t0=0;
ts=10;
t=0;
i=0;
while t<ts
i=i+1;
d1=g-0.5.*rho.*V0.^2*(s/m).*Cd;
phiAvg=d1;
V(i)=V0+dt.*phiAvg ;
V0=V(i);
% t(i)=t;
t=t+dt;
end
plot(V)
  3 件のコメント
dpb
dpb 2021 年 10 月 16 日
" I must get V(0)=9.81..."
We can't always get what we want. MATLAB arrays are unalterably and inviolatably 1-based, NOT zero-based.
You must create a secondary variable to plot against to have a zero origin graph.
Ali Deniz
Ali Deniz 2021 年 10 月 16 日
Okay, thank you.

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

その他の回答 (3 件)

dpb
dpb 2021 年 10 月 16 日
In
...
i=0;
while dt<ts;
d1=g-0.5.*rho.*V0.^2*(s/m).*Cd;
phiAvg=d1;
V(i)=V0+dt.*phiAvg;
V0=V;
t(i)=t;
...
what is i first time through the loop?
Using the debugger is very helpful in such cases...
  1 件のコメント
Ali Deniz
Ali Deniz 2021 年 10 月 16 日
I want to get a graph so I wanted to store values in V(i). For example V(0)=9.81, V(2)=19.62 ... when t=0, t=2 ....

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


John D'Errico
John D'Errico 2021 年 10 月 16 日
編集済み: John D'Errico 2021 年 10 月 16 日
It does not matter that you WANT V(0) to be something. MATLAB does not support zero based indexing. PERIOD.
However, nothing stops you from starting the vector at V(1). And then when you index into the vector, just use V(ind + 1). Now when ind == 0, there is no problem.
If you want to do a plot?
t = 0:1:10; % or whatever it should be
plot(t,V(t+1))

Image Analyst
Image Analyst 2021 年 10 月 18 日

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by