Error Array indices must be positive integers or logical values. when doing euler's method.

2 ビュー (過去 30 日間)
Kaitlyn Plutt
Kaitlyn Plutt 2021 年 9 月 26 日
回答済み: Shanmukha Voggu 2021 年 9 月 29 日
I am tyring to do euler's method with the logistic function dx/dt= xr(1-x/L). Where r=0.65 and L=5.4, intial value is x(0)=6 and t (0,30), and h=0.5. This is my code as of now( I have tried a bunch of different codes giving me the same error).
h=1/2;
N=60;
x(0)=6;
r= 0.65;
L=5.4;
dx= @(x)(r*x)*(1-x/L)
for n=0:N
t(n+1)=t(n)+h;
x(n+1)=x(n)+h*dx(x(n));
end
%I also tried it with
h=1/2;
N=60;
x(0)=6;
r= 0.65;
L=5.4;
dx= @(t,x)(r*x)*(1-x/L)
for n=0:N
t(n+1)=t(n)+h;
x(n+1)=x(n)+h*dx(t(n), x(n));
end

回答 (1 件)

Shanmukha Voggu
Shanmukha Voggu 2021 年 9 月 29 日
Hi Kaitlyn,
The error you are facing is due to accessing the zeroth index of the vector
t=[14 6 3] % creating a simple vector
t = 1×3
14 6 3
firstElement=t(1) % first element of the array is accessed by index "1"
firstElement = 14
t(0) %produces error because zeroth index is not available
Array indices must be positive integers or logical values.
Refer to this for more information

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by