Is there an easy way to make numerical simulations of the ODE of the form dx1/dt=x1(2-x1-x2), dx2/dt=x2(3-x1-x2-x3) for any xn?

I have tried doing for 'n=5' and here is my code; dx = @(t,x) [x(1); x(1)*(2-x(1)-x(2)); x(2); x(2)*(3-x(1)-x(2)-x(3)); x(3); x(3)*(3-x(2)-x(3)-x(4)); x(4); x(4)*(3-x(3)-x(4)-x(5)); x(5); x(5)*(2-x(4)-x(5))]; tspan=[0 15]; x0=[0 2 0 3 0 4 0 6 0 8]; [t,x] = ode45(@(t,x) dx(t,x), tspan, x0); figure(1) plot(t, x)
Can someone please check if this code is correct, if not, how can I improve if and for any 'n'?. Thanks in advance.

 採用された回答

n = 5;
tspan=[0 15];
x0 = [2 3 4 6 8];
[t,x] = ode45(@(t,x) derivatives(t,x,n), tspan, x0);
function dx = derivatives(t,x,n)
dx = zeros(n,1);
dx(1) = x(1)*(2-x(1)-x(2));
for i = 2:n-1
dx(i)=x(i)*(3-x(i-1)-x(i)-x(i+1));
end
dx(n) = x(n)*(2-x(n-1)-x(n));
end
Best wishes
Torsten.

3 件のコメント

Editor
Editor 2018 年 4 月 5 日
編集済み: Editor 2018 年 4 月 5 日
Well, thank you very much Torsten for the handful solution to this problem. However, how can I plot the same in 2D and 3D? I'm really grateful for your help.
Torsten
Torsten 2018 年 4 月 5 日
All examples given here are with corresponding plot commands:
https://de.mathworks.com/help/matlab/ref/ode45.html
Best wishes
Torsten.
Editor
Editor 2018 年 4 月 5 日
Thanks so much Torsen. Completely satisfied. Good day.
Regards
Avulundiah

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

タグ

質問済み:

2018 年 4 月 5 日

コメント済み:

2018 年 4 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by