Fixing code to plot an ODE

1 回表示 (過去 30 日間)
frozentangled
frozentangled 2020 年 12 月 24 日
コメント済み: Mischa Kim 2020 年 12 月 26 日
Hi guys,
I was just coding to plot an ODE, but ran into some problems dispalying the result
The code are as follows
function dxdt = ODE(t, x, alpha, beta)
dxdt = diag(alpha - beta*[x(1);x(2);x(3)])*[x(1);x(2);x(3)];
for n=1:200
p=.01*n;
alpha=[.1; .5; .3];
beta=[.2 .3 .4; p 1.5 .6; .2 .5 .8];
time = [0 100];
initial=[0.5; 0.2; 0.3];
[t,y] = ode45(@(t,y) ODE(t,y,alpha,beta), time, initial);
F1=y(100,1);
F2=y(100,2);
F3=y(100,3);
end
figure(1)
plot(p,F1);
hold on
plot(p,F2);
plot(p,F3);
hold off
Could you tell me what's off?
Many thanks in advance

採用された回答

Mischa Kim
Mischa Kim 2020 年 12 月 24 日
Hi, you are almost there. This should get you started:
hold on
for n=1:200
p=.01*n;
alpha=[.1; .5; .3];
beta=[.2 .3 .4; p 1.5 .6; .2 .5 .8];
time = [0 100];
initial=[0.5; 0.2; 0.3];
[t,y] = ode45(@(t,y) ODE(t,y,alpha,beta), time, initial);
F1=y(end,1); % assuming you are trying to access last elements
F2=y(end,2);
F3=y(end,3);
plot(p,F1,'ro'); % a cleaner way would be to save the values...
hold on % ...for Fi in a matrix and then plot outside...
plot(p,F2,'ko'); % ...of the for loop
plot(p,F3,'bo');
end
hold off
function dxdt = ODE(t, x, alpha, beta)
dxdt = diag(alpha - beta*[x(1);x(2);x(3)])*[x(1);x(2);x(3)];
end
  2 件のコメント
frozentangled
frozentangled 2020 年 12 月 24 日
It worked. Thanks! I am most grateful
I hope you have a great holiday season!
Mischa Kim
Mischa Kim 2020 年 12 月 26 日
Have a joyful holiday season, as well.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by